I have a robotic arm with 6Dof. I want to make a project with two senario. One is manual by moving the servo with a joystick and second is when i do that record and play back the motion. I have an external power supply for the motors, arduino adk mega powerd by usb from pc, one thumbstick joystick, a keypad and an sd card module for storing the feedback maped in to degrees values.
When i record the motion of the servo first problem is that the servo slow down his speed by far.(but the angle values record correctly).
when i read the txt from the sd i want to read it column by column for each servo (for example column 1 of values for motor 1 , column 2 for the motor 2...etc), and i dont know how to do it.
ill post you my code so i appreciate any help cause i cant find a solution in the internet (i dont want someone to solve my hole problem i want to advise me - guide me) thanks you very much for your time:
(motion1 is created to have the acceleration of the speed doesnt have any problem)
manual case the motion1 .. motion2 etc runs independently. when i pressed record button i run the motion function and the record function so i can run and record. but servo slows down his speed a lot.
void loop() {
char customKey = customKeypad.getKey();
if (customKey=='1') {
counter=1;}
if (counter==1){
motion1();
}
else{
servo1.detach();
}
if (customKey == '2') {
counter=2;}
if (counter==2){
motion2();
} else {
servo2.detach();
}
void motion1() {
servo1.attach(5);
value1 = analogRead(pin0);
value2 = analogRead(pin1);
feedback1 = map(value2, 90, 405, 0, 150);
//Serial.println(feedback);
value1 = map(value1, 0, 1023, 1, 29);
if (value1 <= 17 && value1 >= 13) {
value1 = 15;
}
forservo1 = forservo1 + (value1 - 15);
if (forservo1 < 1) {
forservo1 = 1;
}
if (forservo1 > 1023) {
forservo1 = 1023;
}
angle1 = map(forservo1, 1, 1023, 0, 150);
servo1.write(angle1);
delay(x);
}
void record() {
servo1.attach(5);
servo2.attach(6);
// servo3.attach(3);
// servo4.attach(4);
// servo5.attach(5);
// servo6.attach(6);
// make a string for assembling the data to log:
String dataString = "";
// Read pot value and append to the string
// Map to range of 0-180 for servo
//val = map(analogRead(analogPin), 0, 1023, 0, 180);
dataString += String(feedback1);
dataString += String('\t');
dataString += String(feedback2);
// dataString += String('\t');
// dataString += String(feedback3);
// dataString += String('\t');
// dataString += String(feedback4);
// dataString += String('\t');
// dataString += String(feedback5);
// dataString += String('\t');
// dataString += String(feedback6);
// Write to the servo
// Delay to allow servo to settle in position
// myservo.write(val);
// delay(15);
//
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("servopos.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening servopos.txt");
}
}
void playback() {
servo1.attach(5);
servo2.attach(6);
// servo3.attach(3);
//servo4.attach(4);
// servo5.attach(5);
//servo6.attach(6);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("servopos.txt");
// If the file is available, read it
if (dataFile) {
while (dataFile.available()) {
// Write one line to buffer
buffer = dataFile.readStringUntil('\n');
// Print to serial monitor
// Serial.println(buffer);
// // Convert string to integer and position servo
long int x1=dataFile.parseInt();
long int x2=dataFile.parseInt();
// long int x3=dataFile.parseInt();
//
// long int x4=dataFile.parseInt();
// long int x5=dataFile.parseInt();
//
// long int x6=dataFile.parseInt();
Serial.print(x1);
Serial.print(" ");
Serial.print(x2);
//Serial.println(" ");
// Serial.print(x3);
// Serial.print(" ");
// Serial.print(x4);
// Serial.println(" ");
// Serial.print(x5);
// Serial.print(" ");
// Serial.print(x6);
// Serial.println(" ");
if (x1>0){
servo1.write(x1);
delay(10);
}
if (x2>0){
servo2.write(x2);
delay(10);
}
// if (x3>0){
// servo1.write(x3);
// delay(10);
// }
// if (x4>0){
// servo1.write(x4);
// delay(10);
// }
// if (x5>0){
// servo1.write(x5);
// delay(10);
// }
// if (x6>0){
// servo1.write(x6);
// delay(10);
// }
}
dataFile.close();
runOnce = 0;
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening servopos.txt");
}
}