views.py 3.86 KB
Newer Older
Cédric Bonhomme's avatar
Cédric Bonhomme committed
1 2 3 4 5
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# ***** BEGIN LICENSE BLOCK *****
# This file is part of EV3WebController.
6
# Copyright (c) 2014-2015 Cédric Bonhomme.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
7 8 9 10 11 12 13
# All rights reserved.
#
#
#
# ***** END LICENSE BLOCK *****

__author__ = "Cedric Bonhomme"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
14
__version__ = "$Revision: 0.5 $"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
15
__date__ = "$Date: 2014/12/15$"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
16
__revision__ = "$Date: 2015/11/08 $"
17
__copyright__ = "Copyright (c) 2014-2015 Cédric Bonhomme"
Cédric Bonhomme's avatar
Cédric Bonhomme committed
18 19
__license__ = ""

20
import time
Cédric Bonhomme's avatar
Cédric Bonhomme committed
21
from flask import render_template, request, session, url_for, redirect
Cédric Bonhomme's avatar
Cédric Bonhomme committed
22

23 24
from ev3.ev3dev import Motor

Cédric Bonhomme's avatar
Cédric Bonhomme committed
25
from web.decorators import to_response
26
from web.lib import movements
Cédric Bonhomme's avatar
Cédric Bonhomme committed
27
from web import app
28
from web import right_wheel, left_wheel, button, ir_sensor, color_sensor
Cédric Bonhomme's avatar
Cédric Bonhomme committed
29 30 31 32 33


@app.errorhandler(403)
def authentication_failed(e):
    flash('You do not have enough rights.', 'danger')
Cédric Bonhomme's avatar
Cédric Bonhomme committed
34
    return redirect(url_for('index'))
Cédric Bonhomme's avatar
Cédric Bonhomme committed
35

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

Cédric Bonhomme's avatar
Cédric Bonhomme committed
37 38 39
@app.errorhandler(401)
def authentication_required(e):
    flash('Authenticated required.', 'info')
Cédric Bonhomme's avatar
Cédric Bonhomme committed
40
    return redirect(url_for('index'))
Cédric Bonhomme's avatar
Cédric Bonhomme committed
41 42 43


@app.route('/move/<direction>', methods=['GET'])
Cédric Bonhomme's avatar
Cédric Bonhomme committed
44
@app.route('/move/<direction>/<int:speed>', methods=['GET'])
Cédric Bonhomme's avatar
Cédric Bonhomme committed
45
@to_response
46
def move(direction="forward", speed=800):
Cédric Bonhomme's avatar
Cédric Bonhomme committed
47
    """
48 49
    This endpoint manages the different 'move action': 'forward', 'backward',
    'left', 'right' and 'stop'.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
50 51 52 53 54 55 56 57 58
    """
    result = {
                "action": "move",
                "direction": direction,
                "message": "OK"
            }
    return_code = 200

    if direction == 'forward':
59 60
        nb_blocks = request.args.get("blocks", None)
        if None is not nb_blocks:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
61
            position = int(nb_blocks) * -1020
62 63
            result["message"] = movements.run_position_limited(left_wheel,
                                                        right_wheel, position)
64
        else:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
65 66
            left_wheel.run_forever(speed * -1, regulation_mode=False)
            right_wheel.run_forever(speed * -1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
67 68

    elif direction == 'backward':
69 70
        nb_blocks = request.args.get("blocks", None)
        if None is not nb_blocks:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
71
            position = int(nb_blocks) * 1020
72 73
            result["message"] = movements.run_position_limited(left_wheel,
                                                        right_wheel, position)
74
        else:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
75
            print(speed)
76 77
            left_wheel.run_forever(speed * 1, regulation_mode=False)
            right_wheel.run_forever(speed * 1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
78 79

    elif direction == 'left':
Cédric Bonhomme's avatar
Cédric Bonhomme committed
80
        speed = 600
81 82
        forever = request.args.get("forever", None)
        if None is forever:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
83
            movements.rotate(left_wheel, right_wheel, -310, 310, 90, 0)
84 85 86
        else:
            left_wheel.run_forever(speed * -1, regulation_mode=False)
            right_wheel.run_forever(speed, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
87 88

    elif direction == 'right':
Cédric Bonhomme's avatar
Cédric Bonhomme committed
89
        speed = 600
90 91
        forever = request.args.get("forever", None)
        if None is forever:
Cédric Bonhomme's avatar
Cédric Bonhomme committed
92
            movements.rotate(left_wheel, right_wheel, 310, -310, 0, 90)
93 94 95
        else:
            left_wheel.run_forever(speed, regulation_mode=False)
            right_wheel.run_forever(speed * -1, regulation_mode=False)
Cédric Bonhomme's avatar
Cédric Bonhomme committed
96 97 98 99 100 101 102 103 104 105 106 107 108

    elif direction == 'stop':
        left_wheel.stop()
        right_wheel.stop()

    else:
        result["message"], return_code = "Unknown direction", 400


    return result, return_code


@app.route('/sensor/<sensor_name>', methods=['GET'])
109
@to_response
Cédric Bonhomme's avatar
Cédric Bonhomme committed
110 111
def sensor(sensor_name=""):
    """
112
    Returns the value of the selected sensor.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
113 114 115
    """
    if sensor_name == "ir_sensor":
        return {"distance": ir_sensor.prox}
116 117 118
    elif sensor_name == "color_sensor":
        return {"rgb": color_sensor.rgb,
                "mode": color_sensor.mode}
Cédric Bonhomme's avatar
Cédric Bonhomme committed
119 120 121 122 123 124
    elif sensor_name == "button":
        pass
    else:
        return {"message": "Unknown sensor"}, 400


Cédric Bonhomme's avatar
Cédric Bonhomme committed
125 126
@app.route('/', methods=['GET'])
def index():
127 128 129
    """
    Graphical Web interface to command the robot.
    """
Cédric Bonhomme's avatar
Cédric Bonhomme committed
130
    return render_template('index.html')