forked from abhijitmajumdar/Quadcopter_simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad_sim.py
79 lines (71 loc) · 3.55 KB
/
quad_sim.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
75
76
77
78
79
import numpy as np
import quadcopter,gui,controller
import signal
import sys
import argparse
# Constants
TIME_SCALING = 1.0 # Any positive number(Smaller is faster). 1.0->Real Time, 0.0->Run as fast as possible
QUAD_DYNAMICS_UPDATE = 0.002 # seconds
CONTROLLER_DYNAMICS_UPDATE = 0.005 # seconds
run = True
def Single_Point2Point():
# Set goals to go to
#GOALS = [(1,1,1),(10,10,4),(-1,-1,2),(-1,1,4)]
#YAWS = [0,3.14,-1.54,1.54]
# Define the quadcopters
QUADCOPTER={'q1':{'position':[1,0,4],'orientation':[0,0,0],'L':0.3,'r':0.1,'prop_size':[10,4.5],'weight':1.2}}
# Controller parameters
CONTROLLER_PARAMETERS = {'Motor_limits':[4000,9000],
'Tilt_limits':[-10,10],
'Yaw_Control_Limits':[-900,900],
'Z_XY_offset':500,
'Linear_PID':{'P':[300,300,7000],'I':[0.04,0.04,4.5],'D':[450,450,5000]},
'Linear_To_Angular_Scaler':[1,1,0],
'Yaw_Rate_Scaler':0.18,
'Angular_PID':{'P':[22000,22000,1500],'I':[0,0,1.2],'D':[12000,12000,0]},
}
BOAT = {'position': np.array([-10,-10,0]),'L':0.2} #1.028 m/s
boat = quadcopter.Boat(BOAT)
boat.start_thread(dt=QUAD_DYNAMICS_UPDATE,time_scaling=TIME_SCALING)
# Catch Ctrl+C to stop threads
signal.signal(signal.SIGINT, signal_handler)
# Make objects for quadcopter, gui and controller
quad = quadcopter.Quadcopter(QUADCOPTER)
gui_object = gui.GUI(quads=QUADCOPTER, boat=boat)
ctrl = controller.Controller_PID_Point2Point(quad.get_state,quad.get_time,quad.set_motor_speeds,params=CONTROLLER_PARAMETERS,quad_identifier='q1')
# Start the threads
quad.start_thread(dt=QUAD_DYNAMICS_UPDATE,time_scaling=TIME_SCALING)
ctrl.start_thread(update_rate=CONTROLLER_DYNAMICS_UPDATE,time_scaling=TIME_SCALING)
# Update the GUI while switching between destination poitions
while(run==True):
goal = (boat.get_position()[0], boat.get_position()[1], 1.5)
y = 3.14
ctrl.update_target(goal)
ctrl.update_yaw_target(y)
for i in range(10):
gui_object.quads['q1']['position'] = quad.get_position('q1')
gui_object.quads['q1']['orientation'] = quad.get_orientation('q1')
gui_object.boat.boat['position'] = boat.get_position()
gui_object.update()
quad.stop_thread()
ctrl.stop_thread()
boat.stop_thread()
def parse_args():
parser = argparse.ArgumentParser(description="Quadcopter Simulator")
parser.add_argument("--sim", help='single_p2p, multi_p2p or single_velocity', default='single_p2p')
parser.add_argument("--time_scale", type=float, default=-1.0, help='Time scaling factor. 0.0:fastest,1.0:realtime,>1:slow, ex: --time_scale 0.1')
parser.add_argument("--quad_update_time", type=float, default=0.0, help='delta time for quadcopter dynamics update(seconds), ex: --quad_update_time 0.002')
parser.add_argument("--controller_update_time", type=float, default=0.0, help='delta time for controller update(seconds), ex: --controller_update_time 0.005')
return parser.parse_args()
def signal_handler(signal, frame):
global run
run = False
print('Stopping')
sys.exit(0)
if __name__ == "__main__":
args = parse_args()
if args.time_scale>=0: TIME_SCALING = args.time_scale
if args.quad_update_time>0: QUAD_DYNAMICS_UPDATE = args.quad_update_time
if args.controller_update_time>0: CONTROLLER_DYNAMICS_UPDATE = args.controller_update_time
if args.sim == 'single_p2p':
Single_Point2Point()