smartknob
smartknob copied to clipboard
Haptic effect examples in firmware
Awesome project with an abundance of information! But as we speak there is not much documentation (yet) from a firmware perspective. Going through the code and understanding what's happening is quite a task since there are many functionalities built-in.
I was wondering if it would be possible to include some more basic examples of purely the haptic part. It would be great to have an isolated example of this, so it's relatively easy to play with these effects and a brushless motor, even if you don't have the full smart-knob setup. This would enable more people to spin-off their own projects and different applications.
That's a great idea. In the meantime, generally speaking, the core haptic feedback code is fairly well contained within motor_task.cpp: https://github.com/scottbez1/smartknob/blob/e35319af6d4223d03087177da39b63936b2836fb/firmware/src/motor_task.cpp
It could definitely use some cleanup to make things more clear, but there's a big chunk of code that handles one-time calibration (to automatically measure the number of poles in the motor and the physical orientation of the motor's phases relative to the angle sensor) and then the rest is the main loop which checks for messages requesting to change which haptic mode it should be in or whether it should play a haptic "click", and otherwise calculates the motor torque based on the current position and haptic configuration. That has a few sections:
- a check for whether the knob is "idle" (not moving for a while) and close to a detent center, and if so, it will slowly adjust the frame of reference so that the knob is considered exactly in the middle of that detent (to avoid powering the motor continuously trying to correct for that slight error)
- calculation of torque to apply, and adjustment of the current state based on the angle (e.g. incrementing the current position if rotated past a detent)
- updating the motor driver with the desired torque, and publishing the latest position information to other tasks (like the display task)