summaryrefslogtreecommitdiff
path: root/robot/problems/introduction/rotateback/naloga11_rotateBack.py
blob: 2677f051868fb43e4fb8e3952a28149545311fa8 (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
#!/usr/bin/python
# coding=utf-8

print ("""
Naloga 11: Robot naj drži smer - če ga obrnemo iz začetne smeri, naj se zasuka nazaj v prvotno smer. Ob pritisku na stikalo, naj se resetira (smer nastavi na 0).
""")
import sys
sys.path.append('/home/user/codeq')
import time
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])