-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_old.py
110 lines (78 loc) · 2.75 KB
/
main_old.py
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
import utime
class Motor:
def __init__(self, port):
self.motor = eval("hub_runtime.hub.port." + port + ".motor")
def start(self, voltage):
self.motor.run_at_speed(voltage)
def stop(self):
self.motor.brake()
def resetEncoder(self):
self.motor.preset(0)
utime.sleep_ms(2)
def getEncoder(self):
return self.motor.get()[1]
def runDegrees(self, degrees, speed):
self.motor.run_for_degrees(degrees, speed)
class MotorPair:
def __init__(self, portL, portR):
self.rMotor = eval("hub_runtime.hub.port." + portR + ".motor")
self.lMotor = eval("hub_runtime.hub.port." + portL + ".motor")
def startTank(self, lSpeed, rSpeed):
self.rMotor.run_at_speed(rSpeed)
self.lMotor.run_at_speed(-(lSpeed))
def stop(self):
self.rMotor.brake()
self.lMotor.brake()
def start(self, steering, speed):
steering = round(steering)
if(steering == 0):
self.rMotor.run_at_speed(speed)
self.lMotor.run_at_speed(-(speed))
elif(steering > 0):
v = round(speed - (steering * 2 / 100)* speed)
self.lMotor.run_at_speed(-(speed))
self.rMotor.run_at_speed(v)
elif(steering < 0):
v = round(speed + (steering *2 / 100)* speed)
self.rMotor.run_at_speed(speed)
self.lMotor.run_at_speed(-(v))
else:
raise ValueError("MotorPair.start.steering != int")
class ColorSensor:
def __init__(self, port):
self.cS = eval("hub_runtime.hub.port." + port + ".device")
def getValue(self):
return self.cS.get()[0]
def getColor(self):
return self.cS.get()[1]
class Hub:
def __init__(self):
self.hub = eval("hub_runtime.hub")
def batteryCapacity(self):
return self.hub.battery.capacity_left()
def batteryTemp(self):
return self.hub.battery.temperature()
def beep(self, volume):
self.hub.sound.volume(volume)
self.hub.sound.beep()
def powerOFF(self):
self.hub.power_off()
def restart(self):
self.hub.reset()
def update(self):
self.hub.config.update()
def hubStatus(self):
return self.hub.status()
def temperature(self):
return self.hub.temperature()
def buttonCheck(self, btn):
return eval("hub_runtime.hub.button." + btn + ".is_pressed()")
def getGyroAngle(self):
return self.hub.motion.yaw_pitch_roll()[0]
def getAllGyro(self):
return self.hub.motion.yaw_pitch_roll()
def resetGyro(self):
self.hub.motion.yaw_pitch_roll(0)
utime.sleep_ms(2)
def display(self, smth):
self.hub.display.show(smth)