Code for controling motors like servos, steppers, and BLDC's using C++ on arduinos.
Code in master branch is standard libraries needed to be included. Actual library for motor control in the MotorLibraries branch, not the master branch
See example schematic for wiring configuration. Pin assignments can change as needed, the schematic is just an example of things that work. However Vcc, GND etc must still remain.
To do:
-
Clean up all the debugging Serial.print()'s. The ones in the PID must stay though because for some reason the algorithm likes the delay's that println() causes... something that needs to be looked into.
-
The while loop for the PID control needs to run in a time based scenerio:
while (time < 2seconds) {
calculatePID();
}
The reason for this is that if we only loop for certain positions:
while (abs(position - desiredPosition) > 5) {
calculatePID();
}
Like above the motor will stop short while the algorithm hasn't been tuned to stop that early. It'll stop looping as the motor overshoots and continues rotation, therefore we need a loop that halts once a certain amount of time has passed and we know that the motor has settled. See the actual loop SailbotEncodedBLDC.cpp for more information. A function for calculating the correct amount of time needs to be found, and this shouldn't be too hard of a task. Again, the block comment in the file has some ideas about how to go about this, but feel free to approach this problem as seen fit.
The problem with this is that this is not good code and more importantly if the motor needs to rotate a large amount of degrees, the function will stop too short.
- See comments in the servo library. Feedback would be nice, and some code is wrong.
a. Are we getting new Servos? If so all of the new servos bought need to find minimum and maximum degrees.