Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DC motor driver hat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Software
DC motor driver hat
Commits
be65be1d
Commit
be65be1d
authored
7 months ago
by
Samuel Tardieu
Browse files
Options
Downloads
Patches
Plain Diff
Store encoders into a shared global structure
parent
9801007f
No related branches found
No related tags found
1 merge request
!6
Pid
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
controller/src/logic/mod.rs
+12
-7
12 additions, 7 deletions
controller/src/logic/mod.rs
with
12 additions
and
7 deletions
controller/src/logic/mod.rs
+
12
−
7
View file @
be65be1d
...
@@ -4,6 +4,7 @@ use crate::{
...
@@ -4,6 +4,7 @@ use crate::{
};
};
use
embassy_executor
::
Spawner
;
use
embassy_executor
::
Spawner
;
use
embassy_stm32
::{
peripherals
::
WWDG
,
time
::
hz
};
use
embassy_stm32
::{
peripherals
::
WWDG
,
time
::
hz
};
use
embassy_sync
::
once_lock
::
OnceLock
;
use
futures
::
FutureExt
;
use
futures
::
FutureExt
;
use
heapless
::
Vec
;
use
heapless
::
Vec
;
use
i2c2_target
::{
I2C2
,
MESSAGE_SIZE
};
use
i2c2_target
::{
I2C2
,
MESSAGE_SIZE
};
...
@@ -14,15 +15,18 @@ mod watchdog;
...
@@ -14,15 +15,18 @@ mod watchdog;
pub
const
I2C_ADDRESS
:
u8
=
0x57
;
pub
const
I2C_ADDRESS
:
u8
=
0x57
;
static
ENCODERS
:
OnceLock
<
Encoders
<
'static
>>
=
OnceLock
::
new
();
struct
State
{
struct
State
{
_i2c
:
I2C2
,
_i2c
:
I2C2
,
encoders
:
Encoders
<
'static
>
,
encoders
:
&
'static
Encoders
<
'static
>
,
tick_count
:
Relative
,
tick_count
:
Relative
,
}
}
impl
State
{
impl
State
{
fn
new
(
i2c2
:
I2C2
,
encoders
:
Encoders
<
'static
>
)
->
Self
{
async
fn
new
(
i2c2
:
I2C2
)
->
Self
{
let
tick_count
=
Relative
::
new
(
&
encoders
);
let
encoders
=
ENCODERS
.get
()
.await
;
let
tick_count
=
Relative
::
new
(
encoders
);
Self
{
Self
{
_i2c
:
i2c2
,
_i2c
:
i2c2
,
encoders
,
encoders
,
...
@@ -39,14 +43,15 @@ pub fn start_i2c_target(
...
@@ -39,14 +43,15 @@ pub fn start_i2c_target(
encoders
:
Encoders
<
'static
>
,
encoders
:
Encoders
<
'static
>
,
_wwdg
:
WWDG
,
_wwdg
:
WWDG
,
)
{
)
{
spawner
.spawn
(
i2c_engine
(
i2c2
,
encoders
))
.unwrap
();
ENCODERS
.get_or_init
(||
encoders
);
spawner
.spawn
(
i2c_engine
(
i2c2
))
.unwrap
();
spawner
.spawn
(
watchdog
::
watchdog
())
.unwrap
();
spawner
.spawn
(
watchdog
::
watchdog
())
.unwrap
();
spawner
.spawn
(
motor_control
::
motor_control
(
motors
))
.unwrap
();
spawner
.spawn
(
motor_control
::
motor_control
(
motors
))
.unwrap
();
}
}
#[embassy_executor::task]
#[embassy_executor::task]
async
fn
i2c_engine
(
i2c2
:
I2C2
,
encoders
:
Encoders
<
'static
>
)
{
async
fn
i2c_engine
(
i2c2
:
I2C2
)
{
let
state
=
State
::
new
(
i2c2
,
encoders
)
;
let
state
=
State
::
new
(
i2c2
)
.await
;
I2C2
::
start
(
&
i2c_callback
,
state
)
.await
;
I2C2
::
start
(
&
i2c_callback
,
state
)
.await
;
}
}
...
@@ -150,7 +155,7 @@ fn process_command(
...
@@ -150,7 +155,7 @@ fn process_command(
response
.push
(
watchdog
::
get_shutdown_timeout
())
.unwrap
();
response
.push
(
watchdog
::
get_shutdown_timeout
())
.unwrap
();
}
}
[
CMD_ENCODER_TICKS
]
=>
{
[
CMD_ENCODER_TICKS
]
=>
{
let
(
left
,
right
)
=
state
.tick_count
.ticks
(
&
state
.encoders
);
let
(
left
,
right
)
=
state
.tick_count
.ticks
(
state
.encoders
);
let
(
left
,
right
)
=
(
left
.to_le_bytes
(),
right
.to_le_bytes
());
let
(
left
,
right
)
=
(
left
.to_le_bytes
(),
right
.to_le_bytes
());
response
.push
(
left
[
0
])
.unwrap
();
response
.push
(
left
[
0
])
.unwrap
();
response
.push
(
left
[
1
])
.unwrap
();
response
.push
(
left
[
1
])
.unwrap
();
...
...
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