Hello,
I'm working on a strong pan/tilt for my camera. For manual use (using a 2 joysticks-controller) i would like to control pan and tilt simultaneously.
Here's the code :
void loop(){
pHB = analogRead(HB); //Reads the inputs
pGD = analogRead(GD);
//Direction processing and "map" of my joystick position, in order to get time. (used next for uptime and downtime of the pulse, so the bigger the number, slower is the motor.)
if (pHB > 512){dirY = true;delayTimey = map(pHB, 550, 1024, 1000,5);}
if (pHB <= 512){dirY = false;delayTimey = map(pHB, 450, 0, 1000,5);}
if (pGD > 512){dirX = true;delayTimex = map(pGD, 550, 1024, 1000,5);}
if (pGD <= 512){dirX = false;delayTimex = map(pGD, 450, 0, 1000,5);}
//Stop joystick position
if(pHB > 450 && pHB <550) {stopy = true;}else{stopy = false;}
if(pGD > 450 && pGD <550) {stopx = true;}else{stopx = false;}
//Sets the direction to the pin
digitalWrite(Y_DIR, dirY);
digitalWrite(X_DIR, dirX);
//Writes pulses to the driver.
if(stopx == 0){digitalWrite(X_STP, HIGH);}
delayMicroseconds(delayTimex);
digitalWrite(X_STP, LOW);
delayMicroseconds(delayTimex);
if(stopy == 0){digitalWrite(Y_STP, HIGH);}
delayMicroseconds(delayTimey);
digitalWrite(Y_STP, LOW);
delayMicroseconds(delayTimey);
}
All is working quite fine, but I have an issue with the speed control : speed of the stepper is not really "linear".
I'm aware that my code is very looptime-sensitive, so I tried to equilibrate treatment for X and Y motors.
When I control one motor at a time (joystick fully pushed), motor tops at ~50% of its speed.
When I control two motors simultaneously, (joystick fully pushed in a corner), both motors reach 100% of their speed.
A video is the best explanation :
Troubleshooting :
-Battery voltage is ok, even under load.
-Drivers are ok.
-Joysticks are on a good 0-1023 zone, midpoint 500-510.
I think the map() function could be the culprit, I think it could slow something somewhere, but it's out of my reach.
What do you guys think ?
Thanks for you help.
Ethan