Arduino-FOC
Arduino-FOC copied to clipboard
Alignment and Cogging Example has initiation issue.
Describe the bug There is a bug in the 'alignment and cogging test' example. The motor fails to initialize properly in the start of the test function.
The function void testAlignmentAndCogging(int direction) starts off with:
motor.move(0);
_delay(200);
This is the first thing that the code does after initializing (Which puts the field at -90 (I think)). So there is a jump in the position of the motor with this. In theory the _delay(200) will allow the system to settle down, and then it initializes this value.
However, this uses open loop control. And the open loop control has a 'velocity limit'. So, just one call to motor.move(0) will not drive the system all the way to 0. It drives it to some arbitrary point depending on the settings etc. And now the initial sensor position is invalid.
If you set velocity limit to an absurdly large value, then the open loop controller will drive the motor to the 'zero' position in just one call, and the example code works as expected. (Or just comment out the velocity limiting aspect of the open loop controller. Why is that in there anyway?)
I realize this is a small bug in an example, but I spent several hours trying to understand why I kept getting this weird offset.
Hey @schwghrt, You're right that this is a bug on our part. This code has been contributed at some point and refactored several times so these kinds of errors happen easily :)
We'll make sure to update the example.
Yeah, the move open_loop_angle mode is not really the best approach for forcing the motor to go to certain position in the quickest possible way. As you well noticed the move function has to be called iteratively and it will force the motor to move to the desired position with desired max velocity. The reason why max velocity is important is because the motor cannot move from one position the other instantaneously. In the open loop mode we consider that anything that we set to the motor, the motor will be able to execute. If we ask the motor to move from 0 to 100 radians, the motor will not be able to execute it in one call of the move function ( in one instant), so we say how fast do we need the motor to go, with the max velocity variable. And then in each call of move function send to the motor the next step in the good direction given the constraints on the velocity( making the movement feasible). These max velocity values will change for each motor and for each applied max voltage as well.
In the example sketch i suppose the move(0) command worked well before because before this call the motor shaft position was already at 0 so the initial jump was instantaneous. But if for some reason this is not the case you could have some issues as you experienced. We should refactoe this example not to use motor.move() anymore but the function motor.setPhaseVoltage which will remove some ambiguity.