1
|
|
|
import logging |
2
|
|
|
|
3
|
|
|
from curio import sleep, Queue |
4
|
|
|
from bricknil import attach, start |
5
|
|
|
from bricknil.hub import PoweredUpRemote, BoostHub |
6
|
|
|
from bricknil.sensor import InternalMotor, RemoteButtons, LED, Button |
7
|
|
|
from bricknil.process import Process |
8
|
|
|
from bricknil.const import Color |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
@attach(LED, name='led') |
12
|
|
|
@attach(RemoteButtons, name='btns_right', capabilities=['sense_press']) |
13
|
|
|
@attach(RemoteButtons, name='btns_left', capabilities=['sense_press']) |
14
|
|
|
class Remote(PoweredUpRemote): |
15
|
|
|
|
16
|
|
|
async def btns_left_change(self): |
17
|
|
|
if self.btns_left.plus_pressed(): |
18
|
|
|
await self.tell_robot.put('forward') |
19
|
|
|
elif self.btns_left.minus_pressed(): |
20
|
|
|
await self.tell_robot.put('backward') |
21
|
|
|
else: |
22
|
|
|
await self.tell_robot.put('stop') |
23
|
|
|
|
24
|
|
|
async def btns_right_change(self): |
25
|
|
|
if self.btns_right.plus_pressed(): |
26
|
|
|
await self.tell_robot.put('right') |
27
|
|
|
elif self.btns_right.minus_pressed(): |
28
|
|
|
await self.tell_robot.put('left') |
29
|
|
|
else: |
30
|
|
|
await self.tell_robot.put('stop') |
31
|
|
|
|
32
|
|
|
async def run(self): |
33
|
|
|
self.message('Running') |
34
|
|
|
# Set the remote LED to green to show we're ready |
35
|
|
|
await self.led.set_color(Color.green) |
36
|
|
|
while True: |
37
|
|
|
await sleep(10) # Keep the remote running |
38
|
|
|
|
39
|
|
|
@attach(LED, name='led') |
40
|
|
|
@attach(InternalMotor, name='motor_right', port=InternalMotor.Port.B) |
41
|
|
|
@attach(InternalMotor, name='motor_left', port=InternalMotor.Port.A) |
42
|
|
|
class Robot(BoostHub): |
43
|
|
|
|
44
|
|
|
async def run(self): |
45
|
|
|
self.message("Running") |
46
|
|
|
speed = 30 |
47
|
|
|
|
48
|
|
|
# Set the robot LED to green to show we're ready |
49
|
|
|
await self.led.set_color(Color.green) |
50
|
|
|
while True: |
51
|
|
|
msg = await self.listen_remote.get() |
52
|
|
|
await self.listen_remote.task_done() |
53
|
|
|
if msg=='forward': |
54
|
|
|
self.message('going forward') |
55
|
|
|
await self.motor_left.set_speed(speed) |
56
|
|
|
await self.motor_right.set_speed(speed) |
57
|
|
|
elif msg=='backward': |
58
|
|
|
self.message('going backward') |
59
|
|
|
await self.motor_left.set_speed(-speed) |
60
|
|
|
await self.motor_right.set_speed(-speed) |
61
|
|
|
elif msg=='stop': |
62
|
|
|
self.message('stop') |
63
|
|
|
await self.motor_left.set_speed(0) |
64
|
|
|
await self.motor_right.set_speed(0) |
65
|
|
|
elif msg=='left': |
66
|
|
|
self.message('left') |
67
|
|
|
await self.motor_left.set_speed(-speed) |
68
|
|
|
await self.motor_right.set_speed(speed) |
69
|
|
|
elif msg=='right': |
70
|
|
|
self.message('right') |
71
|
|
|
await self.motor_left.set_speed(speed) |
72
|
|
|
await self.motor_right.set_speed(-speed) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
async def system(): |
76
|
|
|
robot = Robot('Vernie') |
77
|
|
|
remote = Remote('remote') |
78
|
|
|
|
79
|
|
|
# Define a message passing queue from the remote to the robot |
80
|
|
|
remote.tell_robot = Queue() |
81
|
|
|
robot.listen_remote = remote.tell_robot |
82
|
|
|
|
83
|
|
|
if __name__ == '__main__': |
84
|
|
|
logging.basicConfig(level=logging.INFO) |
85
|
|
|
start(system) |
86
|
|
|
|