Skip to content

Conversation

@jilaypandya
Copy link
Member

@jilaypandya jilaypandya commented Feb 8, 2024

This PR is the reference implementation for this RFC: #68547

This commit introduces api for stepper motor control where stepper motors are childs of a stepper motor controller
This PR is created in response to the following review.

Rather than going for the bus:motor approach as done in this PR, this PR goes for the child binding approach.

Device Tree Looks largely the same in both approaches.

&spi-controller { status = "okay"; tmc5041: tmc5041@0 { status = "okay"; compatible = "trinamic,tmc5041"; reg = <0>; #address-cells = <1>; #size-cells = <0>; motor_controller_0: motor_controller_0 { status = "okay"; micro-step-res = <256>; stall-guard-setting = <12>; }; motor_controller_1: motor_controller_1 { status = "okay"; stall-guard-setting = <34>; }; }; }; 

Bindings
motor-bindings

Example trinamic,tmc5041.yaml

description: TRINAMIC TMC5041 compatible: "trinamic,tmc5041" include: [spi-device.yaml] properties: "#address-cells": default: 1 const: 1 "#size-cells": default: 0 const: 0 child-binding: include: - name: stepper-motor-controller.yaml property-allowlist: - micro-step-res - reg properties: stall-guard-setting: type: int default: 0 description: | The signed value controls StallGuard2 level for stall output and sets the optimum measurement range for readout. A lower value gives a higher sensitivity. Zero is the starting value working with most motors. For instance, in case of tmc5041, range is -64 to +63 A higher value makes StallGuard2 less sensitive and requires more torque to indicate a stall. 
@jilaypandya jilaypandya requested a review from erwango as a code owner February 8, 2024 17:15
@zephyrbot zephyrbot added the area: Devicetree Binding PR modifies or adds a Device Tree binding label Feb 8, 2024
@zephyrbot zephyrbot requested a review from galak February 8, 2024 17:16
@erwango erwango requested a review from gmarull February 8, 2024 17:16
@jilaypandya jilaypandya force-pushed the feature/introduce-stepper-motor-controller-api branch from f109e36 to 015041f Compare February 8, 2024 17:42
@kartben kartben requested a review from fabiobaltieri February 8, 2024 19:42
@jilaypandya jilaypandya force-pushed the feature/introduce-stepper-motor-controller-api branch 3 times, most recently from b75fd14 to 33f0828 Compare February 9, 2024 08:47
Copy link
Member

Choose a reason for hiding this comment

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

is this mean to reset the internal position data to zero? unclear to me from the description

Copy link
Member Author

@jilaypandya jilaypandya Feb 9, 2024

Choose a reason for hiding this comment

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

The idea is to reset all the registers to some default values. These register values could be entered through DTS, for now i have kinda hardcoded most of them in tmc5041_motor_reset. Use case could also be extended to reset some internal values as well

Copy link
Member

Choose a reason for hiding this comment

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

sounds more like a "set limits" than "calibration", different name maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, you are right the calibration function basically sets the end limits currently. probably something like detect_end_limits would be more apt.

Copy link
Member

Choose a reason for hiding this comment

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

not quite following, when is this going to get called?

Copy link
Member Author

Choose a reason for hiding this comment

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

A callback calibrate_func would have to be registered using this function. Once registered, upon calling stepper_motor_calibratethe pre-registered calibrate_funcwould be called. The approach is comparable to https://docs.zephyrproject.org/latest/hardware/peripherals/gpio.html#c.gpio_init_callback

Copy link
Member

Choose a reason for hiding this comment

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

@fabiobaltieri : The idea behind this function is to perform referencing actions. Every time the motor controller (say, in this case, the trinamic 5041) starts up, the position is unknown. Ideally, one would travel to both the end stops, and say, on reaching the lower end stop, set the coordinate system, by resetting the current position to 0. Since, this is very hardware dependent (cause, one could either travel to both the end stops and detect a block, or using limit switches for detecting the end-stop), this referencing routine (here called as the ´calibrate´ function) is provided.

Copy link
Member

Choose a reason for hiding this comment

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

sorry I still don't understand the whole mechanics of this, reading the driver code now, tmc5041_motor_set_position can set MOTOR_POSITION_MIN and MOTOR_POSITION_MAX, then tmc5041_motor_register_calibrate_func registers a callback but that only gets called from tmc5041_motor_calibrate, and all three are API functions so they are called by application? Or maybe a motion control algorithm, anyway I don't quite see the point, seems like a very complicated way of getting back the data that the application knew in the first place, or maybe it's just a bad example and there's cases where you read back the information from the controller? but then why bother with the callback, just return the data directly from the calibrate function

Comment on lines 182 to 198
Copy link
Member

Choose a reason for hiding this comment

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

I don't think these should be exposed, if the device has some device specific properties that are worth exposing beyond the API, then the driver should just implement some extra driver specific functions. There's few instances of that in the code base, check sensors for examples.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep. That's correct, these could be moved

Comment on lines 36 to 48
Copy link
Member

Choose a reason for hiding this comment

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

Does this information really needs to be here? I'd imagine that this is could be set in the devicetree, configured at init time and then everything else just talks in either steps or mm.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a provision to set these in device tree as well. These are here in order to change microsteps during runtime.

Comment on lines 202 to 203
Copy link
Member

Choose a reason for hiding this comment

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

I see some position control stuff, would it make sense for this API to be used for servos as well? Just thinking that the name may be more generic and not include stepper in it? Maybe motor_control or something

Copy link
Member

Choose a reason for hiding this comment

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

It'd be cool to have a "dumb" enable/position/step gpio based driver as a sample, just as a minimum example and for something we folks can play at home with :-)

Copy link
Member Author

@jilaypandya jilaypandya Feb 10, 2024

Choose a reason for hiding this comment

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

yep for sure, I'll add one for the well known ULN2003 driver from arduino kit

Copy link
Member Author

Choose a reason for hiding this comment

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

@fabiobaltieri I have added a driver for controlling stepper motor with gpios. Currently i tested it with 4 pins. One could expand this driver though in the future.

@fabiobaltieri fabiobaltieri added the RFC Request For Comments: want input from the community label Feb 9, 2024
@fabiobaltieri
Copy link
Member

@jilaypandya tagging as RFC since this is introducing a new API and should probably go through the whole https://docs.zephyrproject.org/latest/contribute/proposals_and_rfcs.html#rfcs dance, but let's hear more comments before bringing it to the arch wg.

@fabiobaltieri fabiobaltieri assigned carlescufi and unassigned galak Feb 9, 2024
@jilaypandya jilaypandya force-pushed the feature/introduce-stepper-motor-controller-api branch from 0ba0283 to f7613ac Compare February 10, 2024 17:01
@zephyrbot zephyrbot added the area: Samples Samples label Feb 13, 2024
@zephyrbot zephyrbot requested review from kartben and nashif February 13, 2024 11:43
@jilaypandya jilaypandya force-pushed the feature/introduce-stepper-motor-controller-api branch 2 times, most recently from b5620ab to 6bb534d Compare February 13, 2024 12:27
Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

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

just a couple styling nitpicks from me, great work

@dipakgmx dipakgmx dismissed stale reviews from bjarki-andreasen and faxe1008 via 053f958 August 24, 2024 12:16
@dipakgmx dipakgmx force-pushed the feature/introduce-stepper-motor-controller-api branch from d706862 to 053f958 Compare August 24, 2024 12:16
fabiobaltieri
fabiobaltieri previously approved these changes Aug 24, 2024
jilaypandya and others added 5 commits August 24, 2024 14:53
This commit introduces api for stepper motor controllers Signed-off-by: Dipak Shetty <dipak.shetty@zeiss.com> Signed-off-by: Florian Guhl <florian.guhl@zeiss.com> Signed-off-by: Jilay Pandya <jilay.pandya@zeiss.com> Signed-off-by: Fabian Blatz <fabianblatz@gmail.com>
This commit introduces a basic gpio stepper driver Signed-off-by: Jilay Pandya <jilay.pandya@zeiss.com> Signed-off-by: Fabian Blatz <fabianblatz@gmail.com>
Added stepper motor controller documentation entry to peripherals. Signed-off-by: Fabian Blatz <fabianblatz@gmail.com> Signed-off-by: Jilay Pandya <jilay.pandya@zeiss.com>
This commit introduces preliminary build all test for stepper driver Signed-off-by: Jilay Pandya <jilay.pandya@zeiss.com>
This commit adds stepper api and drivers to maintainers.yml Signed-off-by: Jilay Pandya <jilay.pandya@zeiss.com>
@jilaypandya jilaypandya force-pushed the feature/introduce-stepper-motor-controller-api branch from 2e6cd17 to 452dedc Compare August 24, 2024 12:54
@DaAwesomeP
Copy link

I am unfortunately running up against a deadline and am not going to get a review in a reasonable amount of time, so please don't wait for me! Great work everyone; this is really exciting!

Copy link
Contributor

@wkhadgar wkhadgar left a comment

Choose a reason for hiding this comment

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

LGTM! Just some really minor tweaks

@jilaypandya
Copy link
Member Author

compliance checks should turn green, once this PR #77517 is merged

@gmarull gmarull dismissed their stale review August 26, 2024 07:08

main concerns addressed, will let other reviews follow

@gmarull gmarull removed the Architecture Review Discussion in the Architecture WG required label Aug 26, 2024
@carlescufi carlescufi merged commit 19203c2 into zephyrproject-rtos:main Aug 27, 2024
@jilaypandya jilaypandya deleted the feature/introduce-stepper-motor-controller-api branch August 27, 2024 12:09
@jilaypandya jilaypandya changed the title stepper_motor_controller: introduce api stepper_controller: introduce api Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Devicetree Binding PR modifies or adds a Device Tree binding area: Process area: Samples Samples area: Stepper RFC Request For Comments: want input from the community