Skip to content

Commit 73e1626

Browse files
authored
Merge pull request #10 from mirte-robot/v0.1.1-dev
V0.1.1 dev
2 parents 4c6069b + 4825482 commit 73e1626

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

mirte_robot/linetrace.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@
1616
# Global shared memory objects (TODO: check if we need shared memory, why is server working?)
1717
stepper = multiprocessing.Value('b', True)
1818
do_step = multiprocessing.Value('b', False)
19+
running = multiprocessing.Value('b', False)
1920

20-
def stop_mirte():
21-
process.terminate()
21+
# the stop be stopped when:
22+
# 1) the code finishes
23+
# 2) the user stopped the process
24+
# 3) the websocket connection is closed
25+
def stop_mirte(terminate = True):
26+
global running
27+
if terminate:
28+
process.terminate()
29+
running.value = False
2230

23-
def load_mirte_module(stepper, do_step):
31+
def load_mirte_module(stepper, do_step, running):
2432

2533
def trace_lines(frame, event, arg):
2634
global do_step
@@ -43,7 +51,7 @@ def traceit(frame, event, arg):
4351
# Send the PID to the web interface and give it some time to call strace on this process
4452
# to see the output of this script
4553
server.send_message_to_all("pid:" + str(os.getpid()))
46-
time.sleep(0.1)
54+
time.sleep(0.2) #TODO: let client send signal when strace is started
4755

4856
sys.settrace(traceit)
4957
# rospy.init_node() for some reason needs to be called from __main__ when importing in the regular way.
@@ -61,24 +69,32 @@ def traceit(frame, event, arg):
6169
# Sending the linetrace 0 to the client
6270
server.send_message_to_all("0")
6371

72+
stop_mirte(False)
73+
6474
process = multiprocessing.Process(target = load_mirte_module, args=(stepper, do_step))
6575

6676
def start_mirte():
6777
global process
68-
process = multiprocessing.Process(target = load_mirte_module, args=(stepper, do_step))
78+
# process should already have been killed after stop, or disconnect
79+
# but, just in case make sure to stop this
80+
if process.is_alive():
81+
process.terminate()
82+
process = multiprocessing.Process(target = load_mirte_module, args=(stepper, do_step, running))
6983
process.start()
7084

7185
def client_left(client, server):
7286
stop_mirte()
7387

7488
def message_received(client, server, message):
75-
global stepper, do_step
89+
global stepper, do_step, running
90+
7691
if message == "b": #break (pause)
7792
stepper.value = True
7893
if message == "c": #continue (play)
7994
stepper.value = False
80-
if not process.is_alive():
95+
if not running.value:
8196
start_mirte()
97+
running.value = True
8298
if message == "s": #step (step)
8399
do_step.value = True
84100
if message == "e": #exit (stop)

mirte_robot/robot.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def __init__(self):
5050
# the controller is needed, it will be enabled.
5151
self.stop_controller_service = rospy.ServiceProxy('stop', Empty, persistent=True)
5252
self.start_controller_service = rospy.ServiceProxy('start', Empty, persistent=True)
53-
#self.stop_controller_service()
5453

5554
# Service for motor speed
5655
self.motors = {}
@@ -113,13 +112,20 @@ def __init__(self):
113112
for sensor in encoder_sensors:
114113
self.encoder_services[sensor] = rospy.ServiceProxy('/mirte/get_encoder_' + encoder_sensors[sensor]["name"], GetEncoder, persistent=True)
115114

116-
# Services for keypad sensores
115+
# Services for keypad sensors
117116
if rospy.has_param("/mirte/keypad"):
118117
keypad_sensors = rospy.get_param("/mirte/keypad")
119118
self.keypad_services = {}
120119
for sensor in keypad_sensors:
121120
self.keypad_services[sensor] = rospy.ServiceProxy('/mirte/get_keypad_' + keypad_sensors[sensor]["name"], GetKeypad, persistent=True)
122121

122+
# Services for color sensors
123+
if rospy.has_param("/mirte/color"):
124+
color_sensors = rospy.get_param("/mirte/color")
125+
self.color_services = {}
126+
for sensor in color_sensors:
127+
self.color_services[sensor] = rospy.ServiceProxy('/mirte/get_color_' + color_sensors[sensor]["name"], GetColorHSL, persistent=True)
128+
123129
self.get_pin_value_service = rospy.ServiceProxy('/mirte/get_pin_value', GetPinValue, persistent=True)
124130
self.set_pin_value_service = rospy.ServiceProxy('/mirte/set_pin_value', SetPinValue, persistent=True)
125131

@@ -210,6 +216,19 @@ def getKeypad(self, keypad):
210216
value = self.keypad_services[keypad]()
211217
return value.data
212218

219+
def getColor(self, sensor):
220+
"""Gets the value of the color sensor.
221+
222+
Parameters:
223+
sensor (str): The name of the sensor as defined in the configuration.
224+
225+
Returns:
226+
{h, s, l}: Hue (0-360), Saturation (0-1), Lightness.
227+
"""
228+
229+
value = self.color_services[sensor]()
230+
return {'h': value.color.h, 's': value.color.s, 'l': value.color.l }
231+
213232
def getAnalogPinValue(self, pin):
214233
"""Gets the input value of an analog pin.
215234
@@ -330,6 +349,24 @@ def setMotorSpeed(self, motor, value):
330349
motor = self.motor_services[motor](value)
331350
return motor.status
332351

352+
def setMotorControl(self, status):
353+
"""Enables/disables the motor controller. This is enabled on boot, but can
354+
be disabled/enabled at runtime. This makes the ROS control node pause,
355+
so it will not respond to Twist messages anymore when disabled.
356+
357+
Parameters:
358+
status (bool): To which status the motor controller should be set.
359+
360+
Returns:
361+
none
362+
"""
363+
364+
if (status):
365+
self.start_controller_service()
366+
else:
367+
self.stop_controller_service()
368+
return
369+
333370
def stop(self):
334371
"""Stops all DC motors defined in the configuration
335372

0 commit comments

Comments
 (0)