Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Open-Cube FW
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Open Cube
Open-Cube FW
Commits
c7c1f377
Commit
c7c1f377
authored
1 week ago
by
Václav Jelínek
Browse files
Options
Downloads
Patches
Plain Diff
Disable NXT UTZ data read when sensor is disconnected
parent
5b8a4791
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/nxt/ultra.py
+36
-29
36 additions, 29 deletions
lib/nxt/ultra.py
lib/robot.py
+0
-1
0 additions, 1 deletion
lib/robot.py
with
36 additions
and
30 deletions
lib/nxt/ultra.py
+
36
−
29
View file @
c7c1f377
...
...
@@ -3,7 +3,7 @@ import utime
import
rp2
from
lib.hw_defs.pins
import
UTZ_I2C_SCK_PIN
,
UTZ_I2C_SDA_PIN
from
lib.robot_consts
import
I2C_NXT_UTZ_FREQ
from
lib.robot_consts
import
I2C_NXT_UTZ_FREQ
,
I2C_NXT_UTZ_BUS
NXT_UTZ_I2C_ADDR
=
0x01
...
...
@@ -85,18 +85,21 @@ class UltrasonicSensor:
self
.
sm0
.
irq
(
self
.
handler
)
self
.
sm0
.
active
(
1
)
else
:
self
.
i2c
=
I2C
(
id
=
0
,
scl
=
Pin
(
UTZ_I2C_SCK_PIN
),
sda
=
Pin
(
UTZ_I2C_SDA_PIN
),
freq
=
(
I2C_NXT_UTZ_FREQ
))
self
.
i2c
=
I2C
(
id
=
I2C_NXT_UTZ_BUS
,
scl
=
Pin
(
UTZ_I2C_SCK_PIN
),
sda
=
Pin
(
UTZ_I2C_SDA_PIN
),
freq
=
(
I2C_NXT_UTZ_FREQ
))
# Initialize return buffer
if
only_first_target
==
True
:
self
.
read_buffer
=
[
255
]
else
:
self
.
read_buffer
=
[
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
]
self
.
running
=
True
# If periodic is on, create timer to call callback every 100ms
if
(
periodic
==
True
):
self
.
timer
=
Timer
()
self
.
timer
.
init
(
period
=
100
,
mode
=
Timer
.
PERIODIC
,
callback
=
self
.
callback
)
self
.
read_data_from_sensor
()
def
deinit
(
self
):
if
(
self
.
use_nonblocking
==
True
):
...
...
@@ -104,37 +107,41 @@ class UltrasonicSensor:
self
.
sm0
.
active
(
0
)
self
.
sm1
.
irq
(
handler
=
None
)
self
.
sm1
.
active
(
0
)
else
:
self
.
i2c
=
None
if
self
.
periodic
==
True
:
self
.
timer
.
deinit
()
def
read_data_from_sensor
(
self
):
try
:
# Write byte 0x42 to read data from sensor
buff
=
bytearray
([
0x42
])
self
.
i2c
.
writeto
(
NXT_UTZ_I2C_ADDR
,
buff
)
# Because HW glitch in ultrasonic we need another clock pulse
self
.
help_pin
=
Pin
(
17
,
mode
=
Pin
.
OPEN_DRAIN
,
value
=
1
)
self
.
help_pin
.
toggle
(
)
utime
.
sleep_us
(
self
.
clock_pulse
)
self
.
help_pin
.
toggle
(
)
# Reinitialize I2C
self
.
i2c
=
I2C
(
id
=
0
,
scl
=
Pin
(
UTZ_I2C_SCK_PIN
),
sda
=
Pin
(
UTZ_I2C_SDA_PIN
),
freq
=
(
I2C_NXT_UTZ_FREQ
))
# Read 8 targets
if
(
self
.
oft
==
False
):
x
=
self
.
i2c
.
readfrom
(
NXT_UTZ_I2C_ADDR
,
8
)
self
.
read_buffer
=
list
(
x
)
# Read first target
else
:
x
=
self
.
i2c
.
readfrom
(
NXT_UTZ_I2C_ADDR
,
1
)
self
.
read_buffer
=
list
(
x
)
if
self
.
running
:
try
:
# Write byte 0x42 to read data from sensor
buff
=
bytearray
([
0x42
]
)
self
.
i2c
.
writeto
(
NXT_UTZ_I2C_ADDR
,
buff
)
# Because HW glitch in ultrasonic we need another clock pulse
self
.
help_pin
=
Pin
(
UTZ_I2C_SCK_PIN
,
mode
=
Pin
.
OPEN_DRAIN
,
value
=
1
)
self
.
help_pin
.
toggle
(
)
utime
.
sleep_us
(
self
.
clock_pulse
)
self
.
help_pin
.
toggle
()
# Reinitialize I2C
self
.
i2c
=
I2C
(
id
=
I2C_NXT_UTZ_BUS
,
scl
=
Pin
(
UTZ_I2C_SCK_PIN
),
sda
=
Pin
(
UTZ_I2C_SDA_PIN
),
freq
=
(
I2C_NXT_UTZ_FREQ
))
# Read 8 targets
if
(
self
.
oft
==
False
):
x
=
self
.
i2c
.
readfrom
(
NXT_UTZ_I2C_ADDR
,
8
)
self
.
read_buffer
=
list
(
x
)
# Read first target
else
:
x
=
self
.
i2c
.
readfrom
(
NXT_UTZ_I2C_ADDR
,
1
)
self
.
read_buffer
=
list
(
x
)
# If I2c throws an error use except
except
Exception
as
e
:
print
(
e
)
print
(
"
Error during I2C communication with ultrasonic sensor
"
)
# If I2c throws an error use except
except
Exception
as
e
:
self
.
running
=
False
print
(
e
)
print
(
"
Error during I2C communication with ultrasonic sensor
"
)
# Return measured distance
def
distance
(
self
):
...
...
This diff is collapsed.
Click to expand it.
lib/robot.py
+
0
−
1
View file @
c7c1f377
...
...
@@ -220,7 +220,6 @@ class Sensors:
if
sensor_type
is
not
None
:
if
sensor_type
==
Sensor
.
NXT_ULTRASONIC
:
if
isinstance
(
self
.
ultra_nxt
,
EmptyObject
):
self
.
buttons
.
set_pin
(
I2C_STRONG_PULL_RPIN_BIT
,
1
)
self
.
ultra_nxt
=
nxt
.
UltrasonicSensor
(
periodic
=
True
,
use_nonblocking
=
False
)
elif
sensor_type
==
Sensor
.
GYRO_ACC
:
if
isinstance
(
self
.
gyro_acc
,
EmptyObject
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment