movements.py 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# ***** BEGIN LICENSE BLOCK *****
# This file is part of EV3WebController.
# Copyright (c) 2014-2015 Cédric Bonhomme.
# All rights reserved.
#
#
#
# ***** END LICENSE BLOCK *****

__author__ = "Cedric Bonhomme"
14
__version__ = "$Revision: 0.3 $"
15
__date__ = "$Date: 2015/11/05$"
16
__revision__ = "$Date: 2015/11/10 $"
17 18 19
__copyright__ = "Copyright (c) 2014-2015 Cédric Bonhomme"
__license__ = ""

20
import time
21 22
from ev3.ev3dev import Motor

23
from web import button, color_sensor
24 25 26 27 28 29 30

def stop(motorA, motorB):
    """
    Stop the motors.
    """
    motorA.stop()
    motorB.stop()
31
    time.sleep(0.6)
32

33 34 35 36
def check_stop_condition(motorA, motorB):
    """
    Wait for the completion of the command before sending the result.
    """
Cédric Bonhomme's avatar
Cédric Bonhomme committed
37
    result_message = []
38 39 40 41
    while "running" in motorA.state.split(" ") and \
                        "running" in motorB.state.split(" "):
        time.sleep(0.1)
        if button.is_pushed:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
42
            # stop the motor
43
            stop(motorA, motorB)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
44
            # go a few centimers backward
Cédric Bonhomme's avatar
Cédric Bonhomme committed
45
            motorA.position = 0
46 47
            motorA.run_position_limited(position_sp=220, speed_sp=350,
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=20,
Cédric Bonhomme's avatar
Cédric Bonhomme committed
48 49
                   ramp_down_sp=1000)
            motorB.position = 0
50 51
            motorB.run_position_limited(position_sp=220, speed_sp=350,
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=20,
Cédric Bonhomme's avatar
Cédric Bonhomme committed
52
                   amp_down_sp=1000)
53 54 55
            while "running" in motorA.state.split(" ") and \
                                "running" in motorB.state.split(" "):
                time.sleep(0.1)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
56
            result_message.append("hit_wall")
57 58
        if color_sensor.colors[color_sensor.color] == "red":
            stop(motorA, motorB)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
59
            result_message.append("in_target")
60 61
    else:
        time.sleep(0.5)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
62
    return ";".join(result_message) if len(result_message) != 0 else "OK"
63

Cédric Bonhomme's avatar
Cédric Bonhomme committed
64

65 66 67 68 69
def run_position_limited(motorA, motorB, position):
    """
    Run for a limitied position.
    """
    motorA.position = 0
70 71
    motorA.run_position_limited(position_sp=position, speed_sp=350,
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=20,
72 73
                   ramp_down_sp=1000)
    motorB.position = 0
74 75
    motorB.run_position_limited(position_sp=position, speed_sp=350,
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=20,
76
                   amp_down_sp=1000)
77 78
    return check_stop_condition(motorA, motorB)

Cédric Bonhomme's avatar
Cédric Bonhomme committed
79
def rotate(motorA, motorB, position1, position2, initial_position1, initial_position2):
80 81 82
    """
    Rotate.
    """
Cédric Bonhomme's avatar
Cédric Bonhomme committed
83
    motorA.position = initial_position1
Cédric Bonhomme's avatar
Cédric Bonhomme committed
84
    motorA.run_position_limited(position_sp=position1, speed_sp=600,
85 86
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=1000,
                   ramp_down_sp=1000)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
87
    motorB.position= initial_position2
Cédric Bonhomme's avatar
Cédric Bonhomme committed
88
    motorB.run_position_limited(position_sp=position2, speed_sp=600,
89 90 91
                   stop_mode=Motor.STOP_MODE.BRAKE, ramp_up_sp=1000,
                   amp_down_sp=1000)
    return check_stop_condition(motorA, motorB)