-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdominate_template.py
More file actions
149 lines (126 loc) · 5.1 KB
/
dominate_template.py
File metadata and controls
149 lines (126 loc) · 5.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Python modules
# import time
import logging
# import argparse
import json
import time
# import socket
from threading import Thread
# from smbus import SMBus
# Local modules
from head.spine.core import get_spine
from head.spine.ourlogging import setup_logging
from head.simulator.physics_engine import PhysicsEngine
from head.simulator.sim_navx import SimNavX
from head.simulator.sim_camera import SimCamera
from head.timer import Timer
from head.navigation.navx_python.navx import get_navx
from head.units import *
from head.navigation.tank import TankDrive
# from head.navigation.mecanum import MecanumDrive
setup_logging(__file__)
logger = logging.getLogger(__name__)
class get_robot:
def __init__(self, sim=False, navx=False, camera=False):
self.sim = sim
self.navx = navx
self.camera = camera
def __enter__(self):
self.gs = get_spine(devices=["fakearduino"], sim=self.sim)
self.r = Robot(s=self.gs.__enter__(), sim=self.sim, navx=navx, camera=camera)
return self.r
def __exit__(self, type, value, traceback):
self.gs.__exit__(type, value, traceback)
self.r.finish()
class Robot:
def __init__(self, s, sim, navx, camera):
self.s = s
self.sim = sim
self.timer = Timer(sim)
with open("/Robot/robot.json") as robot_json:
self.robot_config = json.loads(robot_json.read())
# Check if navx is connected and create object if it exists
# bus = SMBus(1)
self.navx = None
if navx:
if sim:
self.navx = SimNavX()
else:
try:
bus.write_quick(0x32)
self.navx = get_navx()
except:
logger.error("NavX not found on I2C")
raise Exception("NavX not found on I2C")
self.camera = None
if camera:
camera_options = {}
for option in camera.split(';'):
parameter_split = option.split("=")
camera_options[parameter_split[0]] = parameter_split[1]
if sim:
self.camera = SimCamera(**camera_options)
else:
self.camera = VideoStream(**camera_options)
if sim:
self.sim_init()
def sim_init(self):
self.physics_engine = PhysicsEngine(self.robot_config)
appendage_dict = self.s.get_appendage_dict()
self.physics_engine._set_starting_hal(appendage_dict)
if self.navx is not None:
self.physics_engine.add_navx(self.navx)
'''
if self.camera is not None:
self.physics_engine.add_camera(self.camera)
'''
self.sim_thread = Thread(target=self.simulate, name="Simulation Thread", args=())
self.sim_thread.start()
def start(self):
fwd = self.s.get_appendage("fwd")
tank = TankDrive(fwd)
tank.rotate_at_angular_velocity_for_time(AngularVelocity(1, AngularVelocity.rps), Time(4, Time.s))
# mecanum = MecanumDrive(fwd, Unit(self.robot_config['dynamics']['max_velocity'], Velocity.inch_s))
# mecanum.drive_velocity_cartesian(Unit(0, Velocity.inch_s), Unit(0, Velocity.inch_s),
# Unit(1, AngularVelocity.rps))
# self.timer.sleep(Unit(4, Time.s))
def simulate(self):
self.sim_stopped = False
'''
# Set up the sockets
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((socket.gethostname(), 5000))
self.sock.listen(1)
client, addr = self.sock.accept()
'''
while(not self.sim_stopped):
t = self.timer.get()
self.physics_engine._on_increment_time(t)
x, y, theta = self.physics_engine.get_position()
print("T: {0:f}, X: {1:f}, Y: {2:f}, Theta: {3:f}".format(t.to(Time.s),
x.to(Length.inch),
y.to(Length.inch),
theta.to(Angle.degree)))
# hal_data_update = self.physics_engine.get_hal_data_update()
# client.write(json.dumps(hal_data_update) + "\n")
time.sleep(0.01)
def sim_stop(self):
self.sim_stopped = True
def finish(self):
if self.sim:
self.sim_stop()
self.sim_thread.join(5)
if __name__ == "__main__":
# Collect command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--simulate", required=False, action="store_true",
help="Turns on simulation mode")
ap.add_argument("-n", "--navx", required=False, action="store_true",
help="Turns on attempting to use navx")
ap.add_argment("-c", "--camera", required=False,
help="Turns on attempting to use Camera")
args = vars(ap.parse_args())
if 'camera' is not args:
args['camera'] = False
with get_robot(sim=args['simulate'], navx=args['navx'], camera=args['camera']) as bot:
bot.start()