-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunCartPend.py
74 lines (63 loc) · 2.01 KB
/
runCartPend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Main program to control program flow
"""
import control.pidController as pid
import animation.cartPendAnimator as anim
import sim.cartPendSim as sim
import utils.logger as logger
import time
logger.setHeaders(
['Time', 'Theta', 'ThetaDot', 'ThetaDDot', \
'CartPos', 'CartPosDot', 'CartPosDDot'])
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print("Cart Pendulum Simulator Program\n")
print("Available Control Methods:\n")
print("\t[0]: No Active Control")
print("\t[1]: PID Control")
print("\t[2]: LQR Control")
controlOption = input("Select a control method: ")
def nullController(pendAngle, timestep):
return 0;
if (int(controlOption) == 0):
controller = nullController
controllerName = "NullControl"
print("Null Controller Selected, will not attempt to control pendulum")
elif (int(controlOption) == 1):
controller = pid.control
controllerName = "PID"
print("PID Control Selected")
else:
controller = nullController
controllerName = "NullControl"
print("Invalid selection, will not attempt to control pendulum")
runAnim = input("\nRun animated simulation? [y/n]: ")
if (runAnim == 'y' or runAnim == 'Y'):
runAnim = 1
print("Running Animation")
anim.init()
maxTime = 0
else:
runAnim = 0
print("Animation will not display")
maxTime = float(input("Enter time to simulate in seconds: "));
kDt = 1/100;
t = 0
theta = 0
cartPos = 0
while((runAnim and not(anim.detectedCloseButton()))or(not(runAnim) and t <= maxTime)):
sim.updateSim()
theta = sim.getTheta()
sim.setForce(controller(theta, kDt))
if(runAnim):
anim.draw(sim.x, sim.theta)
time.sleep(kDt)
logger.log(
[t, sim.theta, sim.thetaDot, sim.thetaDDot, \
sim.x, sim.xDot, sim.xDDot])
t+=kDt
if(runAnim):
anim.close()
logger.publish(controllerName, True)
print("Log File Saved, Ending Program")
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")