Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/servo: reimplement with high level interface #18392

Merged
merged 1 commit into from
Feb 22, 2023

Conversation

maribu
Copy link
Member

@maribu maribu commented Aug 2, 2022

Contribution description

The previous servo driver didn't provide any benefit over using PWM directly, as users controlled the servo in terms of PWM duty cycles. This changes the interface to provide a high level interface that abstracts the gory PWM details.

In addition, a SAUL layer and auto-initialization is provided.

Testing procedure

The test application provides access to the servo driver via the saul shell command.

> saul
2022-08-02 22:12:31,826 # saul
2022-08-02 22:12:31,827 # ID	Class		Name
2022-08-02 22:12:31,830 # #0	ACT_SWITCH	LD1(green)
2022-08-02 22:12:31,832 # #1	ACT_SWITCH	LD2(blue)
2022-08-02 22:12:31,834 # #2	ACT_SWITCH	LD3(red)
2022-08-02 22:12:31,837 # #3	SENSE_BTN	B1(User button)
2022-08-02 22:12:31,838 # #4	ACT_SERVO	servo
> saul write 4 0
2022-08-02 22:12:41,443 # saul write 4 0
2022-08-02 22:12:41,445 # Writing to device #4 - servo
2022-08-02 22:12:41,447 # Data:	             0 
2022-08-02 22:12:41,450 # [servo] setting 0 to 2949 (0 / 255)
2022-08-02 22:12:41,453 # data successfully written to device #4
> saul write 4 256
2022-08-02 22:12:45,343 # saul write 4 256
2022-08-02 22:12:45,346 # Writing to device #4 - servo
2022-08-02 22:12:45,347 # Data:	           256 
2022-08-02 22:12:45,351 # [servo] setting 0 to 6865 (255 / 255)
2022-08-02 22:12:45,354 # data successfully written to device #4

Each write resulted in the MG90S servo that I connected to move to the corresponding position.

Issues/PRs references

@github-actions github-actions bot added Area: drivers Area: Device drivers Area: sys Area: System Area: tests Area: tests and testing framework labels Aug 2, 2022
maribu added a commit to maribu/RIOT that referenced this pull request Aug 2, 2022
This is a verbatim copy of the PWM config of `boards/nucleo-f746zg`.
However, those boards are almost identical. I successfully tested
the configuration via RIOT-OS#18392
@maribu maribu added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Aug 2, 2022
maribu added a commit to maribu/RIOT that referenced this pull request Aug 3, 2022
This is a verbatim copy of the PWM config of `boards/nucleo-f746zg`.
However, those boards are almost identical. I successfully tested
the configuration via RIOT-OS#18392
maribu added a commit to maribu/RIOT that referenced this pull request Aug 4, 2022
This is a verbatim copy of the PWM config of `boards/nucleo-f746zg`.
However, those boards are almost identical. I successfully tested
the configuration via RIOT-OS#18392
@maribu
Copy link
Member Author

maribu commented Aug 4, 2022

I updated the code to calculate the duty cycle at runtime, as the actual PWM frequency is not known on compile time. E.g. on the Arduino UNO the actual PWM frequency is 61 Hz (if I recall correctly). With that, the actual extension and the requested one differs by 22 % if using the nominal frequency instead of the actual one.

(Yes, this works fine on the Arduino UNO when rebased on top of #18148 :))

Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's a much better API.
I don't quite understand the reason for use of XFA - why not use the same pattern as the other drivers?

drivers/include/servo.h Outdated Show resolved Hide resolved
drivers/include/servo.h Outdated Show resolved Hide resolved
drivers/include/servo.h Outdated Show resolved Hide resolved
drivers/servo/servo.c Outdated Show resolved Hide resolved
@maribu
Copy link
Member Author

maribu commented Oct 18, 2022

I don't quite understand the reason for use of XFA - why not use the same pattern as the other drivers?

Likely I should have done the conversion as a follow up. Note that there are two things that are changed: First, it allocates device states and params as an arrays and uses an index has handle instead of a pointer. Second, it uses XFA to implement the arrays.

The use of arrays has the following advantages:

  • It safes RAM, as the current pattern of copying the params stuff into the state is no longer needed. (The alternative would be to add a pointer from the state to params, which would increase the state size by one pointer and add one level of pointer traversal to accessing params.)
  • And most importantly: It fixes the fundamentally broken sensor and actuator auto initialization.
    • Auto-initialization does happen if used with SAUL, but then there is no way to access the device state and use it without the SAUL API.
    • When not using SAUL, currently no way of auto-initialization is possible
    • Using indexes solves the issue of obtaining the device pointer when initialization happens in auto_init

The use of XFA has the advantage that a user can easily add additional devices. E.g. let's say board foo already has 3 LEDs exposed via SAUL. Now we add additional LEDs via a shield. With XFA, we can just extend the LED array without having to mess with the board LED config.

Likely I should update an existing driver, maybe the SHT1x, to the XFA approach, first.

@github-actions github-actions bot added Area: SAUL Area: Sensor/Actuator Uber Layer and removed Area: sys Area: System labels Oct 24, 2022
@maribu
Copy link
Member Author

maribu commented Oct 24, 2022

I dropped the use an of index-based servo_t and of XFA for consistency. The servo shell command had to be removed as well, as co-existence with SAUL no longer is possible.

This can be added in a follow up. Let's fix the servo API first.

Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI has some comments, otherwise looks good

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 25, 2022
benpicco
benpicco previously approved these changes Oct 25, 2022
@riot-ci
Copy link

riot-ci commented Oct 25, 2022

Murdock results

✔️ PASSED

6dc2a60 drivers/servo: reimplement with high level interface

Success Failures Total Runtime
6865 0 6865 13m:10s

Artifacts

@github-actions github-actions bot added the Area: build system Area: Build system label Oct 26, 2022
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 21, 2023
@github-actions github-actions bot added the Area: Kconfig Area: Kconfig integration label Feb 21, 2023
@maribu
Copy link
Member Author

maribu commented Feb 21, 2023

I forgot the KConfig integration and added it now.

@benpicco
Copy link
Contributor

I forgot the KConfig integration and added it now.

pretty much impossible to get that right on the first try 😉

@benpicco
Copy link
Contributor

bors merge

@benpicco
Copy link
Contributor

bors cancel

@bors
Copy link
Contributor

bors bot commented Feb 21, 2023

Canceled.

@benpicco
Copy link
Contributor

bors merge

bors bot added a commit that referenced this pull request Feb 21, 2023
18392: drivers/servo: reimplement with high level interface r=benpicco a=maribu

### Contribution description

The previous servo driver didn't provide any benefit over using PWM directly, as users controlled the servo in terms of PWM duty cycles. This changes the interface to provide a high level interface that abstracts the gory PWM details.

In addition, a SAUL layer and auto-initialization is provided.

### Testing procedure

The test application provides access to the servo driver via the `saul` shell command.

```
> saul
2022-08-02 22:12:31,826 # saul
2022-08-02 22:12:31,827 # ID	Class		Name
2022-08-02 22:12:31,830 # #0	ACT_SWITCH	LD1(green)
2022-08-02 22:12:31,832 # #1	ACT_SWITCH	LD2(blue)
2022-08-02 22:12:31,834 # #2	ACT_SWITCH	LD3(red)
2022-08-02 22:12:31,837 # #3	SENSE_BTN	B1(User button)
2022-08-02 22:12:31,838 # #4	ACT_SERVO	servo
> saul write 4 0
2022-08-02 22:12:41,443 # saul write 4 0
2022-08-02 22:12:41,445 # Writing to device #4 - servo
2022-08-02 22:12:41,447 # Data:	             0 
2022-08-02 22:12:41,450 # [servo] setting 0 to 2949 (0 / 255)
2022-08-02 22:12:41,453 # data successfully written to device #4
> saul write 4 256
2022-08-02 22:12:45,343 # saul write 4 256
2022-08-02 22:12:45,346 # Writing to device #4 - servo
2022-08-02 22:12:45,347 # Data:	           256 
2022-08-02 22:12:45,351 # [servo] setting 0 to 6865 (255 / 255)
2022-08-02 22:12:45,354 # data successfully written to device #4
```

Each write resulted in the MG90S servo that I connected to move to the corresponding position.

### Issues/PRs references

19292: sys/phydat: Fix unit confusion r=benpicco a=maribu

### Contribution description

Previously, `UNIT_G` was used for g-force with the correct symbol `g`, `UNIT_GR` for gram (as in kilogram) with the incorrect symbol `G` (which would be correct for Gauss), and `UNIT_GS` for Gauss with symbol `Gs` (which is an alternative correct symbol).

To avoid confusion between G-Force, Gauss, and Gram the units have been renamed to `UNIT_G_FORCE`, `UNIT_GRAM`, and `UNIT_GAUSS`. In addition, gram now uses the correct symbol `g`; which sadly is the same as for g-force. But usually there is enough context to tell them apart.

### Testing procedure

Green CI

### Issues/PRs references

None

19294: sys/shell: don't include suit command by default r=benpicco a=benpicco



19295: gcoap: Finish the gcoap_get_resource_list_tl -> gcoap_get_resource_list renaming r=benpicco a=chrysn

### Contribution description

In #16688, an argument was added to the `gcoap_get_resource_list` function by creating a new function `gcoap_get_resource_list_tl` with a deprecation and roll-over plan.

This plan has not been acted on so far.

This PR shortens the original plan by just adding the argument to `gcoap_get_resource_list` and removing `gcoap_get_resource_list_tl` in a single go. The rationale for this deviation is that while it's a public API, its only two practical consumers are the (built-in) well-known/core implementation, and the (built-in) CoRE Resource Directory (cord) endpoint. Moreover, a further change to this API (switching over to `coap_block_slicer_t`) is expected to happen within this release cycle, which would take something like 4 total releases to get through otherwise, which is unrealistic for an API that there are no known external users of.

A second commit clean up ToDo items (in the changed function's documentation) that referred to a IETF draft that has long been abandoned by the CoRE WG.

### Testing procedure

Plain inspection and CI passing should suffice.

### AOB

There is a second analogous pair left over from #16688, `gcoap_req_send` / `gcoap_req_send_tl`. As that *is* expected to be used widely, I prefer not to mix these two concerns, and get the present one through without unnecessary hold-up.

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
Co-authored-by: Benjamin Valentin <benjamin.valentin@bht-berlin.de>
Co-authored-by: chrysn <chrysn@fsfe.org>
@bors
Copy link
Contributor

bors bot commented Feb 21, 2023

Build failed (retrying...):

bors bot added a commit that referenced this pull request Feb 21, 2023
18392: drivers/servo: reimplement with high level interface r=benpicco a=maribu

### Contribution description

The previous servo driver didn't provide any benefit over using PWM directly, as users controlled the servo in terms of PWM duty cycles. This changes the interface to provide a high level interface that abstracts the gory PWM details.

In addition, a SAUL layer and auto-initialization is provided.

### Testing procedure

The test application provides access to the servo driver via the `saul` shell command.

```
> saul
2022-08-02 22:12:31,826 # saul
2022-08-02 22:12:31,827 # ID	Class		Name
2022-08-02 22:12:31,830 # #0	ACT_SWITCH	LD1(green)
2022-08-02 22:12:31,832 # #1	ACT_SWITCH	LD2(blue)
2022-08-02 22:12:31,834 # #2	ACT_SWITCH	LD3(red)
2022-08-02 22:12:31,837 # #3	SENSE_BTN	B1(User button)
2022-08-02 22:12:31,838 # #4	ACT_SERVO	servo
> saul write 4 0
2022-08-02 22:12:41,443 # saul write 4 0
2022-08-02 22:12:41,445 # Writing to device #4 - servo
2022-08-02 22:12:41,447 # Data:	             0 
2022-08-02 22:12:41,450 # [servo] setting 0 to 2949 (0 / 255)
2022-08-02 22:12:41,453 # data successfully written to device #4
> saul write 4 256
2022-08-02 22:12:45,343 # saul write 4 256
2022-08-02 22:12:45,346 # Writing to device #4 - servo
2022-08-02 22:12:45,347 # Data:	           256 
2022-08-02 22:12:45,351 # [servo] setting 0 to 6865 (255 / 255)
2022-08-02 22:12:45,354 # data successfully written to device #4
```

Each write resulted in the MG90S servo that I connected to move to the corresponding position.

### Issues/PRs references

19292: sys/phydat: Fix unit confusion r=benpicco a=maribu

### Contribution description

Previously, `UNIT_G` was used for g-force with the correct symbol `g`, `UNIT_GR` for gram (as in kilogram) with the incorrect symbol `G` (which would be correct for Gauss), and `UNIT_GS` for Gauss with symbol `Gs` (which is an alternative correct symbol).

To avoid confusion between G-Force, Gauss, and Gram the units have been renamed to `UNIT_G_FORCE`, `UNIT_GRAM`, and `UNIT_GAUSS`. In addition, gram now uses the correct symbol `g`; which sadly is the same as for g-force. But usually there is enough context to tell them apart.

### Testing procedure

Green CI

### Issues/PRs references

None

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
@bors
Copy link
Contributor

bors bot commented Feb 21, 2023

This PR was included in a batch that was canceled, it will be automatically retried

bors bot added a commit that referenced this pull request Feb 22, 2023
18392: drivers/servo: reimplement with high level interface r=benpicco a=maribu

### Contribution description

The previous servo driver didn't provide any benefit over using PWM directly, as users controlled the servo in terms of PWM duty cycles. This changes the interface to provide a high level interface that abstracts the gory PWM details.

In addition, a SAUL layer and auto-initialization is provided.

### Testing procedure

The test application provides access to the servo driver via the `saul` shell command.

```
> saul
2022-08-02 22:12:31,826 # saul
2022-08-02 22:12:31,827 # ID	Class		Name
2022-08-02 22:12:31,830 # #0	ACT_SWITCH	LD1(green)
2022-08-02 22:12:31,832 # #1	ACT_SWITCH	LD2(blue)
2022-08-02 22:12:31,834 # #2	ACT_SWITCH	LD3(red)
2022-08-02 22:12:31,837 # #3	SENSE_BTN	B1(User button)
2022-08-02 22:12:31,838 # #4	ACT_SERVO	servo
> saul write 4 0
2022-08-02 22:12:41,443 # saul write 4 0
2022-08-02 22:12:41,445 # Writing to device #4 - servo
2022-08-02 22:12:41,447 # Data:	             0 
2022-08-02 22:12:41,450 # [servo] setting 0 to 2949 (0 / 255)
2022-08-02 22:12:41,453 # data successfully written to device #4
> saul write 4 256
2022-08-02 22:12:45,343 # saul write 4 256
2022-08-02 22:12:45,346 # Writing to device #4 - servo
2022-08-02 22:12:45,347 # Data:	           256 
2022-08-02 22:12:45,351 # [servo] setting 0 to 6865 (255 / 255)
2022-08-02 22:12:45,354 # data successfully written to device #4
```

Each write resulted in the MG90S servo that I connected to move to the corresponding position.

### Issues/PRs references

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
@bors
Copy link
Contributor

bors bot commented Feb 22, 2023

Build failed:

The previous servo driver didn't provide any benefit over using PWM
directly, as users controlled the servo in terms of PWM duty cycles.
This changes the interface to provide a high level interface that
abstracts the gory PWM details.

In addition, a SAUL layer and auto-initialization is provided.

Co-authored-by: benpicco <benpicco@googlemail.com>
@maribu
Copy link
Member Author

maribu commented Feb 22, 2023

bors retry

Now with a Makefile.ci, let's see what happens now.

@bors
Copy link
Contributor

bors bot commented Feb 22, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@maribu
Copy link
Member Author

maribu commented Feb 22, 2023

bors retry

@bors
Copy link
Contributor

bors bot commented Feb 22, 2023

Build succeeded:

@bors bors bot merged commit 41b54d8 into RIOT-OS:master Feb 22, 2023
@maribu maribu deleted the drivers/servo branch February 22, 2023 12:12
@maribu
Copy link
Member Author

maribu commented Feb 22, 2023

Thx :)

@MrKevinWeiss MrKevinWeiss added this to the Release 2023.04 milestone Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: drivers Area: Device drivers Area: Kconfig Area: Kconfig integration Area: SAUL Area: Sensor/Actuator Uber Layer Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants