Replies: 2 comments 3 replies
-
Hi Luni64. Thanks for this code! In order to understand it better I built a test setup and loaded it onto my Teensy. The motor spins, however the Pot on A0 doesn't change anything atm. pot1.update(); at the start of the set Speed, right before the if, now the Pot controls speed and direction. Is that the "right" way to do this, or would you suggest a more elegant way? Cheers Michael PS: How would the skript change if I needed to define a "dead space" of the stick? Like say the center is pot value 512 and the stick shouldn't so anything when the pot is between 462 and 562? As the motor would need to stop, Im not sure a simple "if" would be sufficient? I could see the skript asking "if pot value between 462 to 562 override speed to 0, else..." and then your above skript |
Beta Was this translation helpful? Give feedback.
-
To implement a deadband I'd change the void setSpeed(RotateControl& controller, ResponsiveAnalogRead& pot, int potCenter)
{
pot.update();
if (pot.hasChanged()) // only do something if the pot value changed
{
float overrideValue = (pot.getValue() - potCenter) / 512.0f; // transform [0..1024] to approximately [-1.0...+1.0]
constexpr float deadband = 0.1; // deadband 10%
if (fabsf(overrideValue) < deadband) // return 0 if we are in the deadband
overrideValue = 0.0f;
else // outside we need correct the value to start from 0
overrideValue -= overrideValue > 0 ? deadband : -deadband; // depending on the sign we need to add or substract the deadband value to start from 0 when leaving the deadband
controller.overrideSpeed(overrideValue);
}
} Regarding a limited range: void loop()
{
setSpeed(rc1, pot1, potCenter1);
// setSpeed(rc2, pot2, potCenter2);
// ...
int32_t pos = s1.getPosition();
if (pos > 1000 || pos < -2000) // some range
{
rc1.stop(); // decelerates to 0, make sure to have enough headroom for deceleration
// rc1.emergencyStop(); // hard stop, motor will probably lose steps
}
if (stopwatch > 200) // print stepper data every 200ms
{
stopwatch -= 200;
Serial.printf("s1 pos: %d \n", s1.getPosition());
digitalToggleFast(LED_BUILTIN);
}
} |
Beta Was this translation helpful? Give feedback.
-
Answer to an email question: How to use the override speed function to jog a motor
This simple example shows how to use a pot to jog a motor. The center value of the pot is assumed to be 450. Pot values from 451 to 1024 will rotate the motor clockwise (1024 corresponds to max speed) pot values from 449 down to 0 will rotate the motor in the other direction.
The example uses https://github.com/dxinteractive/ResponsiveAnalogRead to get smooth changes and a stable set value.
Beta Was this translation helpful? Give feedback.
All reactions