summaryrefslogtreecommitdiff
path: root/robot/problems/introduction/rotateback/common.py
blob: 10cca871b51717d4a7aa8fbfb61e817f5fa58f69 (plain)
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
# coding=utf-8

from python.util import has_token_sequence, string_almost_equal, \
    string_contains_number, get_tokens, get_numbers, get_exception_desc
from server.hints import Hint

id = 212
group = 'introduction'
number = 11
visible = True

solution = '''\
from ev3dev import *
from mindstorms_widgets import mindstorms_widgets

def sgn(x):
    return -1 if x<0 else (1 if x>0 else 0)

robot = mindstorms_widgets()
robot.connect_motor( 'left' )
robot.connect_motor( 'right' )
robot.connect_sensor( 'gyro' )
robot.connect_sensor( 'touch' )

smer = hitrost = 0
robot.reset_gyro()
robot.gyro_set_mode( 'angle' )
prev_smer = -1
while 1:
    smer = robot.gyro_sensor_measure()
    if smer!=prev_smer:
        print ("Kot:", smer)
        prev_smer = smer
    
    if robot.touch_sensor_measure():
        robot.reset_gyro()
        print ( robot.gyro_sensor_measure() )
    
    if abs(smer) < 10:
        hitrost = 10*sgn(smer)
    else:
        if abs(smer) > 100:
            hitrost = 100*sgn(smer)
        else:
            hitrost = smer
    
    if abs(smer) > 1:
        L, R = -hitrost, hitrost
    else:
        L = R = 0
    robot.move_tank('on', lr_power=[L,R])
'''

hint_type = {
    'mW_init': Hint('mW_init'),
    'connectMotorLeft': Hint('connectMotorLeft'),
    'connectMotorRight': Hint('connectMotorRight'),
    'connectGyro': Hint('connectGyro'),
    'connectTouch': Hint('connectTouch'),
    'resetGyro': Hint('resetGyro'),
    'setGyroMode': Hint('setGyroMode'),
    'gyroMeasure': Hint('gyroMeasure'),
    'ifTouch': Hint('ifTouch'),
    'moveTank': Hint('moveTank'),
    'lrPower': Hint('lrPower'),
    'while': Hint('while')
}

def hint( code):
    tokens = get_tokens(code)

    # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program
    if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']):
        return [{'id': 'mW_init'}]

    # if code does not include connect_motor statement, a student needs to learn about how to connect the motors
    if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']):
        return [{'id': 'connectMotorLeft'}]

    # if code does not include connect_motor statement, a student needs to learn about how to connect the motors
    if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']):
        return [{'id': 'connectMotorRight'}]

    if not (has_token_sequence(tokens, ['connect_gyro','(']) and 'gyro' in code):
        return [{'id': 'connectGyro'}]
    
    if not (has_token_sequence(tokens, ['connect_sensor','(']) and 'touch' in code):
        return [{'id': 'connectTouch'}]
    
    if not has_token_sequence(tokens, ['reset_gyro']):
        return [{'id': 'resetGyro'}]

    if not has_token_sequence(tokens, ['gyro_set_mode']):
        return [{'id': 'setGyroMode'}]

    if not has_token_sequence(tokens, ['gyro_sensor_measure']):
        return [{'id': 'gyroMeasure'}]

    if not has_token_sequence(tokens, ['if','touch_sensor_measure','(']):
        return [{'id': 'ifTouch'}]

    if not has_token_sequence(tokens, ['move_tank']):
        return [{'id': 'moveTank'}]
    
    if not 'lr_power' in code:
        return [{'id': 'lrPower'}]

    if not has_token_sequence(tokens, ['while']):
        return [{'id': 'while'}]

    return None