The robot now runs until the end of the command unless a wall is detected or...

The robot now runs until the end of the command unless a wall is detected or the red color is detected.
parent 3066b3fd
......@@ -20,7 +20,7 @@ __license__ = ""
import time
from ev3.ev3dev import Motor
from web import button
from web import button, color_sensor
def stop(motorA, motorB):
"""
......@@ -29,6 +29,21 @@ def stop(motorA, motorB):
motorA.stop()
motorB.stop()
def check_stop_condition(motorA, motorB):
"""
Wait for the completion of the command before sending the result.
"""
while "running" in motorA.state.split(" ") and \
"running" in motorB.state.split(" "):
time.sleep(0.1)
if button.is_pushed:
stop(motorA, motorB)
return "hit_wall"
if color_sensor.colors[color_sensor.color] == "red":
stop(motorA, motorB)
return "in_target"
return "OK"
def run_position_limited(motorA, motorB, position):
"""
Run for a limitied position.
......@@ -41,11 +56,18 @@ def run_position_limited(motorA, motorB, position):
motorB.run_position_limited(position_sp=position, speed_sp=800,
stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=1000,
amp_down_sp=1000)
# Wait for the completion of the command before sending the result
while "running" in motorA.state.split(" ") and \
"running" in motorB.state.split(" "):
time.sleep(0.1)
if button.is_pushed:
stop(motorA, motorB)
return "hit_wall"
return "OK"
return check_stop_condition(motorA, motorB)
def rotate(motorA, motorB, position1, position2):
"""
Rotate.
"""
motorA.position_sp = 0
motorA.run_position_limited(position_sp=position1, speed_sp=800,
stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=1000,
ramp_down_sp=1000)
motorB.position_sp = 90
motorB.run_position_limited(position_sp=position2, speed_sp=800,
stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=1000,
amp_down_sp=1000)
return check_stop_condition(motorA, motorB)
......@@ -50,7 +50,6 @@ def authentication_required(e):
@login_manager.user_loader
def load_user(id):
# Return an instance of the User model
#return models.User.objects(id=id).first()
pass
......@@ -59,6 +58,8 @@ def load_user(id):
@to_response
def move(direction="forward", speed=800):
"""
This endpoint manages the different 'move action': 'forward', 'backward',
'left', 'right' and 'stop'.
"""
result = {
"action": "move",
......@@ -89,13 +90,14 @@ def move(direction="forward", speed=800):
elif direction == 'left':
speed = 600
left_wheel.run_forever(speed, regulation_mode=False)
right_wheel.run_forever(speed * -1, regulation_mode=False)
movements.rotate(left_wheel, right_wheel, 90, -90)
#left_wheel.run_forever(speed, regulation_mode=False)
#right_wheel.run_forever(speed * -1, regulation_mode=False)
elif direction == 'right':
speed = 600
left_wheel.run_forever(speed * -1, regulation_mode=False)
right_wheel.run_forever(speed, regulation_mode=False)
#left_wheel.run_forever(speed * -1, regulation_mode=False)
#right_wheel.run_forever(speed, regulation_mode=False)
elif direction == 'stop':
left_wheel.stop()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment