Hi all,
I am building an excavator and I will like that when I set a position for my bucket via my joystick that it stays in that position even when there is a load attached to it that is causing it to move. In other words, when I set my angle on the servo, if the angle moves out of place due to a disturbance and not my joystick, I will like the arm to move back to that angle it was at originally.
Do I need to get feedback from the servo? and compare it somehow? What should I do to achieve this?
Below is my code for controlling the two servos with my joystick. It works by after i move the joystick to a position and release it the servo angle stays on that position untill i move the joystick again (i.e the centre position does not move it back to its original 90 degree position when the joystick is released)
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo[4]; //code used for attaching upto 4 servos
byte angle[4] = {90, 90, 90, 90}; // middle point for servo angle
byte potPin[4] = {A0, A1, A2, A3}; // input pins to attach your potentiometers
byte servoPin[4] = {7, 6, 10, 9}; // input pins to attach servos
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n < 4; n++) {
servo[n].attach(servoPin[n]);
}
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n < 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n < 4; n++) {
servo[n].write(angle[n]);
}
}
EDIT: I no longer am required to use servos instead, I must use linear actuators for the excavator according to my project supervisor. However these are very expensive, nevertheless instead of buying one, I am thinking of making them. The following link is where I am turning towardshttp://www.instructables.com/id/Quick-and-easy-electric-cylinder-prototype-.../. These linear actuators are to be inserted in a toy excavator as shown in the link http://www.amazon.com/Bruder-02439-Toys-Caterpillar-Excavator/dp/B00024QOUO/ref=sr_1_1?ie=UTF8&qid=1412193602&sr=8-1&keywords=bruder+excavator. I was thinking they should replace the existing hydraulic look alike actuators. My question is, can the mini DC motor be controlled with the Joystick on arduino? I figured it maybe just a case of switching polarities for up and down motions not too sure. Also does anyone have any other recommendation that I should take to move the arms and boom on a toy excavator? Also if there is any disturbance from the load that changes the position of the bucket is there a way to auto control the bucket back to its intended position (position control)