mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-14 18:44:20 +00:00
snippets/ev3/ev3devsensor: initial examples
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
venv/
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
|
||||
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
|
||||
|
||||
// List of extensions which should be recommended for users of this workspace.
|
||||
"recommendations": [
|
||||
"lego-education.ev3-micropython"
|
||||
],
|
||||
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
|
||||
"unwantedRecommendations": [
|
||||
"ms-python.python"
|
||||
]
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Main Example",
|
||||
"type": "ev3devBrowser",
|
||||
"request": "launch",
|
||||
"program": "/home/robot/${workspaceRootFolderName}/main.py"
|
||||
},
|
||||
{
|
||||
"name": "Class Example",
|
||||
"type": "ev3devBrowser",
|
||||
"request": "launch",
|
||||
"program": "/home/robot/${workspaceRootFolderName}/class_example.py"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.eol": "\n",
|
||||
"debug.openDebug": "neverOpen",
|
||||
"python.linting.enabled": false
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env pybricks-micropython
|
||||
from pybricks.parameters import Port
|
||||
from pybricks.iodevices import Ev3devSensor
|
||||
|
||||
|
||||
class MySensor(Ev3devSensor):
|
||||
"""Example of extending the Ev3devSensor class."""
|
||||
|
||||
def __init__(self, port):
|
||||
"""Initialize the sensor."""
|
||||
|
||||
# Initialize the parent class.
|
||||
super().__init__(port)
|
||||
|
||||
# Get the sysfs path.
|
||||
self.path = '/sys/class/lego-sensor/sensor' + str(self.sensor_index)
|
||||
|
||||
def get_modes(self):
|
||||
"""Get a list of mode strings so we don't have to look them up."""
|
||||
|
||||
# The path of the modes file.
|
||||
modes_path = self.path + '/modes'
|
||||
|
||||
# Open the modes file.
|
||||
with open(modes_path, 'r') as m:
|
||||
|
||||
# Read the contents.
|
||||
contents = m.read()
|
||||
|
||||
# Strip the newline symbol, and split at every space symbol.
|
||||
return contents.strip().split(' ')
|
||||
|
||||
|
||||
# Initialize the sensor
|
||||
sensor = MySensor(Port.S3)
|
||||
|
||||
# Show where this sensor can be found
|
||||
print(sensor.path)
|
||||
|
||||
# Print the available modes
|
||||
modes = sensor.get_modes()
|
||||
print(modes)
|
||||
|
||||
# Read mode 0 of this sensor
|
||||
val = sensor.read(modes[0])
|
||||
print(val)
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env pybricks-micropython
|
||||
from pybricks.parameters import Port
|
||||
from pybricks.tools import wait
|
||||
from pybricks.iodevices import Ev3devSensor
|
||||
|
||||
# Initialize an Ev3devSensor.
|
||||
# In this example we use the
|
||||
# LEGO MINDSTORMS EV3 Color Sensor.
|
||||
sensor = Ev3devSensor(Port.S3)
|
||||
|
||||
while True:
|
||||
# Read the raw RGB values
|
||||
r, g, b = sensor.read('RGB-RAW')
|
||||
|
||||
# Print results
|
||||
print('R: {0}\t G: {1}\t B: {2}'.format(r, g, b))
|
||||
|
||||
# Wait
|
||||
wait(200)
|
||||
Reference in New Issue
Block a user