Skip to content

Commit 7827c77

Browse files
committed
add rccar
1 parent 0b88c20 commit 7827c77

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3+
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4+
5+
// List of extensions which should be recommended for users of this workspace.
6+
"recommendations": [
7+
"lego-education.ev3-micropython"
8+
],
9+
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
10+
"unwantedRecommendations": [
11+
"ms-python.python"
12+
]
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Download and Run",
9+
"type": "ev3devBrowser",
10+
"request": "launch",
11+
"program": "/home/robot/${workspaceRootFolderName}/main.py"
12+
}
13+
]
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"files.eol": "\n",
4+
"debug.openDebug": "neverOpen",
5+
"python.linting.enabled": false
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# list input devices
2+
3+
class InputDevice():
4+
def __init__(self):
5+
self.name = ''
6+
self.handler = ''
7+
8+
def __str__(self):
9+
return '<Input Device: name=%s, handler=%s>' % (self.name, self.handler)
10+
11+
def setName(self, name):
12+
if len(name) >= 2 and name.startswith('"') and name.endswith('"'):
13+
name = name[1:len(name)-1]
14+
self.name = name
15+
16+
def setHandler(self, handlers):
17+
for handler in handlers.split(' '):
18+
if handler.startswith('event'):
19+
self.handler = handler
20+
21+
def listDevices():
22+
devices = []
23+
with open('/proc/bus/input/devices', 'r') as f:
24+
device = None
25+
while True:
26+
s = f.readline()
27+
if s == '':
28+
break
29+
s = s.strip()
30+
if s == '':
31+
devices.append(device)
32+
device = None
33+
else:
34+
if device is None:
35+
device = InputDevice()
36+
if s.startswith('N: Name='):
37+
device.setName(s[8:])
38+
elif s.startswith('H: Handlers='):
39+
device.setHandler(s[12:])
40+
return devices
41+
42+
def detectJoystick(joystickNames):
43+
for device in listDevices():
44+
for joystickName in joystickNames:
45+
if joystickName in device.name:
46+
return '/dev/input/%s' % device.handler
47+
return None

samples/micropython/rccar/main.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env pybricks-micropython
2+
3+
import struct, threading
4+
5+
from pybricks import ev3brick as brick
6+
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)
7+
from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
8+
from pybricks.tools import print, wait, StopWatch
9+
from pybricks.robotics import DriveBase
10+
from devices import detectJoystick
11+
12+
class Robot():
13+
def __init__(self):
14+
self.motor = Motor(Port.B)
15+
self.ultrasonic = UltrasonicSensor(Port.S4)
16+
self.active = True
17+
self.speed = 0
18+
self.colors = [None, Color.GREEN, Color.YELLOW, Color.RED]
19+
20+
def setSpeed(self, acc):
21+
if acc < 0:
22+
self.speed = max(-3, self.speed - 1)
23+
elif acc > 0:
24+
self.speed = min(3, self.speed + 1)
25+
else:
26+
self.speed = 0
27+
if self.speed != 0:
28+
self.motor.run(self.speed * 90)
29+
else:
30+
self.motor.stop()
31+
brick.light(self.colors[abs(self.speed)])
32+
33+
def inactive(self):
34+
self.active = False
35+
self.setSpeed(0)
36+
brick.sound.beep()
37+
38+
def autoStopLoop(robot):
39+
while robot.active:
40+
if robot.speed > 0 and robot.ultrasonic.distance() < 200:
41+
robot.setSpeed(0)
42+
wait(100)
43+
44+
def joystickLoop(robot, eventFile):
45+
FORMAT = 'llHHI'
46+
EVENT_SIZE = struct.calcsize(FORMAT)
47+
with open(eventFile, 'rb') as infile:
48+
while True:
49+
event = infile.read(EVENT_SIZE)
50+
_, _, t, c, v = struct.unpack(FORMAT, event)
51+
# button A, B:
52+
if t == 1 and v == 1:
53+
if c == 305:
54+
# press A:
55+
robot.setSpeed(1)
56+
elif c == 304:
57+
# press B:
58+
robot.setSpeed(-1)
59+
elif c == 307:
60+
# press X:
61+
return robot.inactive()
62+
elif t == 3:
63+
if c == 1:
64+
# Left stick & verticle:
65+
speed = 0
66+
if v < 32768:
67+
# up:
68+
speed = 1
69+
elif v > 32768:
70+
# down:
71+
speed = -1
72+
robot.setSpeed(speed)
73+
74+
def buttonLoop(robot):
75+
while True:
76+
if not any(brick.buttons()):
77+
wait(10)
78+
else:
79+
if Button.LEFT in brick.buttons():
80+
robot.setSpeed(-1)
81+
elif Button.RIGHT in brick.buttons():
82+
robot.setSpeed(1)
83+
elif Button.CENTER in brick.buttons():
84+
robot.setSpeed(0)
85+
elif Button.UP in brick.buttons():
86+
return robot.inactive()
87+
wait(500)
88+
89+
def main():
90+
brick.sound.beep()
91+
joystickEvent = detectJoystick(['Controller'])
92+
robot = Robot()
93+
t = threading.Thread(target=autoStopLoop, args=(robot,))
94+
t.start()
95+
if joystickEvent:
96+
joystickLoop(robot, joystickEvent)
97+
else:
98+
buttonLoop(robot)
99+
100+
main()

0 commit comments

Comments
 (0)