// Uduino Default Board #include Uduino uduino("uduinoBoard"); // Declare and name your object // Servo #include #define MAXSERVOS 8 Servo base; Servo shoulder; Servo elbow; Servo wrist_rot; Servo wrist_ver; Servo gripper; int step_base = 0; int step_shoulder = 45; int step_elbow = 180; int step_wrist_rot = 180; int step_wrist_ver = 90; int step_gripper = 10; void setup() { Serial.begin(9600); #if defined (__AVR_ATmega32U4__) // Leonardo while (!Serial) {} #elif defined(__PIC32MX__) delay(1000); #endif Serial.setTimeout(25565); uduino.addCommand("s", SetMode); uduino.addCommand("d", WritePinDigital); uduino.addCommand("a", WritePinAnalog); uduino.addCommand("rd", ReadDigitalPin); uduino.addCommand("r", ReadAnalogPin); uduino.addCommand("br", BundleReadPin); uduino.addCommand("b", ReadBundle); uduino.addInitFunction(DisconnectAllServos); uduino.addDisconnectedFunction(DisconnectAllServos); uduino.addCommand("Control", Control); pinMode(12,OUTPUT); digitalWrite(12,HIGH); base.attach(11); shoulder.attach(10); elbow.attach(9); wrist_rot.attach(6); wrist_ver.attach(5); gripper.attach(3);// put your setup code here, to run once: gripper.write(73); shoulder.write(90); elbow.write(85); wrist_ver.write(90); wrist_rot.write(90); base.write(70); } void ReadBundle() { char *arg = NULL; char *number = NULL; number = uduino.next(); int len = 0; if (number != NULL) len = atoi(number); for (int i = 0; i < len; i++) { uduino.launchCommand(arg); } } void SetMode() { int pinToMap = 100; //100 is never reached char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToMap = atoi(arg); } int type; arg = uduino.next(); if (arg != NULL) { type = atoi(arg); PinSetMode(pinToMap, type); } } void PinSetMode(int pin, int type) { //TODO : vérifier que ça, ça fonctionne if (type != 4) DisconnectServo(pin); switch (type) { case 0: // Output pinMode(pin, OUTPUT); break; case 1: // PWM pinMode(pin, OUTPUT); break; case 2: // Analog pinMode(pin, INPUT); break; case 3: // Input_Pullup pinMode(pin, INPUT_PULLUP); break; case 4: // Servo SetupServo(pin); break; } } void WritePinAnalog() { int pinToMap = 100; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToMap = atoi(arg); } int valueToWrite; arg = uduino.next(); if (arg != NULL) { valueToWrite = atoi(arg); if (ServoConnectedPin(pinToMap)) { UpdateServo(pinToMap, valueToWrite); } else { analogWrite(pinToMap, valueToWrite); } } } void WritePinDigital() { int pinToMap = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) pinToMap = atoi(arg); int writeValue; arg = uduino.next(); if (arg != NULL && pinToMap != -1) { writeValue = atoi(arg); digitalWrite(pinToMap, writeValue); } } void ReadAnalogPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); if (pinToRead != -1) printValue(pinToRead, analogRead(pinToRead)); } } void ReadDigitalPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); } if (pinToRead != -1) printValue(pinToRead, digitalRead(pinToRead)); } void BundleReadPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); if (pinToRead != -1) { if (pinToRead < 13) printValue(pinToRead, digitalRead(pinToRead)); else printValue(pinToRead, analogRead(pinToRead)); } } } String s1; String s2; String s3; String s4; String s5; String s6; Servo myservo; void loop() { uduino.update(); } void printValue(int pin, int targetValue) { uduino.print(pin); uduino.print(" "); //<- Todo : Change that with Uduino delimiter uduino.println(targetValue); // TODO : Here we could put bundle read multiple pins if(Bundle) { ... add delimiter // } ... } /* SERVO CODE */ Servo servos[MAXSERVOS]; int servoPinMap[MAXSERVOS]; /* void InitializeServos() { for (int i = 0; i < MAXSERVOS - 1; i++ ) { servoPinMap[i] = -1; servos[i].detach(); } } */ void SetupServo(int pin) { if (ServoConnectedPin(pin)) return; int nextIndex = GetAvailableIndexByPin(-1); if (nextIndex == -1) nextIndex = 0; servos[nextIndex].attach(pin); servoPinMap[nextIndex] = pin; } void DisconnectServo(int pin) { servos[GetAvailableIndexByPin(pin)].detach(); servoPinMap[GetAvailableIndexByPin(pin)] = 0; } bool ServoConnectedPin(int pin) { if (GetAvailableIndexByPin(pin) == -1) return false; else return true; } int GetAvailableIndexByPin(int pin) { for (int i = 0; i < MAXSERVOS - 1; i++ ) { if (servoPinMap[i] == pin) { return i; } else if (pin == -1 && servoPinMap[i] < 0) { return i; // return the first available index } } return -1; } void UpdateServo(int pin, int targetValue) { int index = GetAvailableIndexByPin(pin); servos[index].write(targetValue); delay(10); } void DisconnectAllServos() { for (int i = 0; i < MAXSERVOS; i++) { servos[i].detach(); digitalWrite(servoPinMap[i], LOW); servoPinMap[i] = -1; } } int prepos1; int prepos2; int prepos3; int pos4; int prepos4; int pos6; int prepos6; int pos5; int prepos5; void Control(int vBase, int vShoulder, int vElbow,int vWrist_ver, int vWrist_rot, int vgripper) { // Check values, to avoid dangerous positions for the Braccio int parameters = uduino.getNumberOfParameters(); if (parameters > 0) { vBase = uduino.charToInt(uduino.getParameter(0)); vShoulder = uduino.charToInt(uduino.getParameter(1)); vElbow = uduino.charToInt(uduino.getParameter(2)); vWrist_ver = uduino.charToInt(uduino.getParameter(3)); vWrist_rot = uduino.charToInt(uduino.getParameter(4)); vgripper = uduino.charToInt(uduino.getParameter(5)); } if (vBase < 0) vBase=0; if (vBase > 180) vBase=180; if (vShoulder < 15) vShoulder=15; if (vShoulder > 165) vShoulder=165; if (vElbow < 0) vElbow=0; if (vElbow > 180) vElbow=180; if (vWrist_ver < 0) vWrist_ver=0; if (vWrist_ver > 180) vWrist_ver=180; if (vWrist_rot > 180) vWrist_rot=180; if (vWrist_rot < 0) vWrist_rot=0; if (vgripper < 10) vgripper = 10; if (vgripper > 73) vgripper = 73; int exit = 1; //Until the all motors are in the desired position while (exit) { //For each servo motor if next degree is not the same of the previuos than do the movement if (vBase != step_base) { base.write(step_base); //One step ahead if (vBase > step_base) { step_base++; } //One step beyond if (vBase < step_base) { step_base--; } } if (vShoulder != step_shoulder) { shoulder.write(step_shoulder); //One step ahead if (vShoulder > step_shoulder) { step_shoulder++; } //One step beyond if (vShoulder < step_shoulder) { step_shoulder--; } } if (vElbow != step_elbow) { elbow.write(step_elbow); //One step ahead if (vElbow > step_elbow) { step_elbow++; } //One step beyond if (vElbow < step_elbow) { step_elbow--; } } if (vWrist_ver != step_wrist_rot) { wrist_rot.write(step_wrist_rot); //One step ahead if (vWrist_ver > step_wrist_rot) { step_wrist_rot++; } //One step beyond if (vWrist_ver < step_wrist_rot) { step_wrist_rot--; } } if (vWrist_rot != step_wrist_ver) { wrist_ver.write(step_wrist_ver); //One step ahead if (vWrist_rot > step_wrist_ver) { step_wrist_ver++; } //One step beyond if (vWrist_rot < step_wrist_ver) { step_wrist_ver--; } } if (vgripper != step_gripper) { gripper.write(step_gripper); if (vgripper > step_gripper) { step_gripper++; } //One step beyond if (vgripper < step_gripper) { step_gripper--; } } //delay between each movement delay(15); //It checks if all the servo motors are in the desired position if ((vBase == step_base) && (vShoulder == step_shoulder) && (vElbow == step_elbow) && (vWrist_ver == step_wrist_rot) && (vWrist_rot == step_wrist_ver) && (vgripper == step_gripper)) { step_base = vBase; step_shoulder = vShoulder; step_elbow = vElbow; step_wrist_rot = vWrist_ver; step_wrist_ver = vWrist_rot; step_gripper = vgripper; exit = 0; } else { exit = 1; } } }