|
| 1 | +/* |
| 2 | +
|
| 3 | + braccioOfUnoWifi.ino |
| 4 | + |
| 5 | + Based on Arduino Uno WiFi Rest Server example |
| 6 | +
|
| 7 | + This example for the Arduino Uno WiFi shows how to |
| 8 | + control a TinkerKit Braccio through REST calls. |
| 9 | + You can create your how mobile app or your |
| 10 | + browser app to control the Braccio in wireless mode |
| 11 | + |
| 12 | + Note that with the Braccio shield version less than V4 |
| 13 | + you need to disconnect the pin A4 from the shield to the board |
| 14 | +
|
| 15 | + Possible commands created in this shetch: |
| 16 | +
|
| 17 | + * "/arduino/custom/base/value:80" -> Moves the base of the Braccio at 80 degrees |
| 18 | + * "/arduino/custom/shoulder/value:150" -> Moves the shoulder of the Braccio at 150 degrees |
| 19 | + * "/arduino/custom/elbow/value:45" -> Moves the elbow of the Braccio at 45 degrees |
| 20 | + * "/arduino/custom/wristv/value:10" -> Moves the wristv of the Braccio at 10 degrees |
| 21 | + * "/arduino/custom/wristr/value:120" -> Moves the wristr of the Braccio at 120 degrees |
| 22 | + * "/arduino/custom/gripper/value:73" -> Close the gripper |
| 23 | + * "/arduino/custom/ledon" -> Turn ON the LED 13 |
| 24 | + * "/arduino/custom/ledoff" -> Turn OFF the LED 13 |
| 25 | + * "/arduino/custom/servo:3/value:73" -> Moves the servo to the pin 3 at 73 degrees |
| 26 | + * "/arduino/custom/sayciao" -> Run the function sayciao(). The Braccio say "Ciao" with the gripper |
| 27 | + * "/arduino/custom/takesponge" -> Run the function takesponge(). The Braccio take the big sponge you can find in the its box |
| 28 | + * "/arduino/custom/showsponge" -> Run the function showsponge(). The Braccio show the sponge to the user |
| 29 | + * "/arduino/custom/throwsponge" -> Run the function throwsponge(). The Braccio throw away the sponge |
| 30 | +
|
| 31 | + This example code is part of the public domain |
| 32 | +
|
| 33 | + http://labs.arduino.org/RestServer+and+RestClient |
| 34 | + http://www.arduino.org/products/tinkerkit/arduino-tinkerkit-braccio |
| 35 | +
|
| 36 | +*/ |
| 37 | + |
| 38 | +#include <Wire.h> |
| 39 | +#include <ArduinoWiFi.h> |
| 40 | +#include <Servo.h> |
| 41 | +#include <Braccio.h> |
| 42 | + |
| 43 | +//Initial Value for each Motor |
| 44 | +int m1 = 0; |
| 45 | +int m2 = 45; |
| 46 | +int m3 = 180; |
| 47 | +int m4 = 180; |
| 48 | +int m5 = 90; |
| 49 | +int m6 = 0; |
| 50 | + |
| 51 | +boolean moveBraccio = false; |
| 52 | + |
| 53 | +Servo base; |
| 54 | +Servo shoulder; |
| 55 | +Servo elbow; |
| 56 | +Servo wrist_rot; |
| 57 | +Servo wrist_ver; |
| 58 | +Servo gripper; |
| 59 | + |
| 60 | + |
| 61 | +void setup() { |
| 62 | + //Intitialization of Braccio |
| 63 | + Braccio.begin(); |
| 64 | + //Intitialization of the Uno WiFi |
| 65 | + Wifi.begin(); |
| 66 | + Wifi.println("REST Server is up"); |
| 67 | +} |
| 68 | + |
| 69 | +void loop() { |
| 70 | + //Wait until the board receives HTTP commands |
| 71 | + while (Wifi.available()) { |
| 72 | + process(Wifi); |
| 73 | + } |
| 74 | + delay(50); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | +Parse Command from REST |
| 79 | +It parse a command like: /arduino/custom/base/value:45 |
| 80 | +@param command: The message to parse |
| 81 | +@param type: the key for parsing |
| 82 | +@return the value for the key |
| 83 | +*/ |
| 84 | +int parseCommand(String command, String type) { |
| 85 | + int typeIndex = command.indexOf(type); |
| 86 | + int dotsIndex = command.indexOf(':', typeIndex + type.length()); |
| 87 | + |
| 88 | + int idxtmp = dotsIndex + 4; |
| 89 | + if ((dotsIndex + 4) > command.length()) idxtmp = command.length(); |
| 90 | + String tmp = command.substring(dotsIndex + 1, idxtmp); |
| 91 | + |
| 92 | + return tmp.toInt(); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | +It process data from the HTTP protocol |
| 97 | +*/ |
| 98 | +void process(WifiData client) { |
| 99 | + // read the command |
| 100 | + String command = client.readString(); |
| 101 | + command.toUpperCase(); |
| 102 | + |
| 103 | + if(command.indexOf("CUSTOM")==-1){ |
| 104 | + client.println("Invalid command: " + command + ""); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + //The message from sender |
| 109 | + String message = command.substring(16); |
| 110 | + //client.println(message); //Debug |
| 111 | + |
| 112 | + /* |
| 113 | + For each message perform the proper command |
| 114 | + */ |
| 115 | + if (message == "LEDON") { |
| 116 | + //Turn ON Led 13 |
| 117 | + digitalWrite(13, HIGH); |
| 118 | + //Return message to the sender (Eg: the browser) |
| 119 | + client.println("alert('Led D13 ON');"); |
| 120 | + } |
| 121 | + else if (message == "LEDOFF") { |
| 122 | + digitalWrite(13, LOW); |
| 123 | + client.println("alert('Led D13 OFF');"); |
| 124 | + } |
| 125 | + //This command allow you to move a desired servo motor giving the |
| 126 | + //PWM pin where is connected |
| 127 | + //eg: http://192.168.240.1/arduino/custom/servo:3/value:45 or http://192.168.240.1/arduino/custom/base/value:45 |
| 128 | + else if (message.startsWith("SERVO")) { |
| 129 | + //Parse the message to retrive what is the servo to move |
| 130 | + int servo = parseCommand(message, "SERVO"); |
| 131 | + //Parse the message to retrive what is the value for the servo |
| 132 | + int value = parseCommand(message, "VALUE"); |
| 133 | + |
| 134 | + client.println("Message:" + String(message) + "SERVO: " + String(servo) + " " + String(value)); |
| 135 | + |
| 136 | + moveBraccio=true; |
| 137 | + } |
| 138 | + //http://192.168.240.1/arduino/custom/base:45 or http://192.168.240.1/arduino/custom/base/value:45 |
| 139 | + //Command for the base of the braccio (M1) |
| 140 | + else if (message.startsWith("BASE")) { |
| 141 | + m1 = parseCommand(message, "VALUE"); |
| 142 | + moveBraccio = true; |
| 143 | + client.println("BASE: " + String(m1)); |
| 144 | + } |
| 145 | + //Command for the shoulder of the braccio (M2) |
| 146 | + else if (message.startsWith("SHOULDER")) { |
| 147 | + m2 = parseCommand(message, "VALUE"); |
| 148 | + moveBraccio = true; |
| 149 | + client.println("SHOULDER: " + String(m2)); |
| 150 | + } |
| 151 | + //Command for the elbow of the braccio (M3) |
| 152 | + else if (message.startsWith("ELBOW")) { |
| 153 | + m3 = parseCommand(message, "VALUE"); |
| 154 | + moveBraccio = true; |
| 155 | + client.println("ELBOW: " + String(m3)); |
| 156 | + } |
| 157 | + //Command for the wrist of the braccio to move it up and down (M4) |
| 158 | + else if (message.startsWith("WRISTV")) { |
| 159 | + m4 = parseCommand(message, "VALUE"); |
| 160 | + moveBraccio = true; |
| 161 | + client.println("WRISTV: " + String(m4)); |
| 162 | + } |
| 163 | + //Command for the wrist of the braccio to rotate it (M5) |
| 164 | + else if (message.startsWith("WRISTR")) { |
| 165 | + m5 = parseCommand(message, "VALUE"); |
| 166 | + moveBraccio = true; |
| 167 | + client.println("WRISTR: " + String(m5)); |
| 168 | + } |
| 169 | + //Command for the GRIPPER of the braccio to open and close it (M6) |
| 170 | + else if (message.startsWith("GRIPPER")) { |
| 171 | + m6 = parseCommand(message, "VALUE"); |
| 172 | + moveBraccio = true; |
| 173 | + client.println("GRIPPER: " + String(m6)); |
| 174 | + } |
| 175 | + //Command to say "Ciao" |
| 176 | + else if (message.startsWith("SAYCIAO")) { |
| 177 | + sayCiao(); |
| 178 | + client.println("SAYCIAO: " + String(m6)); |
| 179 | + } |
| 180 | + //Command for take the sponge |
| 181 | + else if (message.startsWith("TAKESPONGE")) { |
| 182 | + takesponge(); |
| 183 | + client.println("TAKESPONGE: " + String(m6)); |
| 184 | + } |
| 185 | + //Command for show the sponge |
| 186 | + else if (message.startsWith("SHOWSPONGE")) { |
| 187 | + showsponge(); |
| 188 | + client.println("SHOWSPONGE: " + String(m6)); |
| 189 | + } |
| 190 | + //Command for throw away the sponge |
| 191 | + else if (message.startsWith("THROWSPONGE")) { |
| 192 | + throwsponge(); |
| 193 | + client.println("THROWSPONGE: " + String(m6)); |
| 194 | + } |
| 195 | + else |
| 196 | + client.println("command error: " + message); |
| 197 | + |
| 198 | + //if flag moveBraccio is true fire the movement |
| 199 | + if (moveBraccio) { |
| 200 | + //client.println("moveBraccio"); |
| 201 | + Braccio.ServoMovement(20, m1, m2, m3, m4, m5, m6); |
| 202 | + moveBraccio = false; |
| 203 | + } |
| 204 | + |
| 205 | + client.flush(); |
| 206 | +} |
| 207 | + |
| 208 | +/** |
| 209 | +The braccio Say 'Ciao' with the GRIPPER |
| 210 | +*/ |
| 211 | +void sayCiao() { |
| 212 | + Braccio.ServoMovement(20, 90, 0, 180, 160, 0, 15); |
| 213 | + |
| 214 | + for (int i = 0; i < 5; i++) { |
| 215 | + Braccio.ServoMovement(10, 90, 0, 180, 160, 0, 15); |
| 216 | + delay(500); |
| 217 | + |
| 218 | + Braccio.ServoMovement(10, 90, 0, 180, 160, 0, 73); |
| 219 | + delay(500); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | +Braccio take the Sponge |
| 225 | +*/ |
| 226 | +void takesponge() { |
| 227 | + //starting position |
| 228 | + //(step delay M1 , M2 , M3 , M4 , M5 , M6); |
| 229 | + Braccio.ServoMovement(20, 0, 45, 180, 180, 90, 0); |
| 230 | + |
| 231 | + //I move arm towards the sponge |
| 232 | + Braccio.ServoMovement(20, 0, 90, 180, 180, 90, 0); |
| 233 | + |
| 234 | + //the GRIPPER takes the sponge |
| 235 | + Braccio.ServoMovement(20, 0, 90, 180, 180, 90, 60 ); |
| 236 | + |
| 237 | + //up the sponge |
| 238 | + Braccio.ServoMovement(20, 0, 45, 180, 45, 0, 60); |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +/** |
| 243 | +Braccio show the sponge to the user |
| 244 | +*/ |
| 245 | +void showsponge() { |
| 246 | + for (int i = 0; i < 2; i++) { |
| 247 | + |
| 248 | + //(step delay M1 , M2 , M3 , M4 , M5 , M6 ); |
| 249 | + Braccio.ServoMovement(10, 0, 45, 180, 45, 180, 60); |
| 250 | + |
| 251 | + Braccio.ServoMovement(10, 0, 45, 180, 45, 0, 60); |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +/** |
| 256 | +Braccio throw away the sponge |
| 257 | +*/ |
| 258 | +void throwsponge() { |
| 259 | + //(step delay M1 , M2 , M3 , M4 , M5 , M6 ); |
| 260 | + Braccio.ServoMovement(20, 0, 45, 90, 45, 90, 60); |
| 261 | + |
| 262 | + Braccio.ServoMovement(5, 0, 45, 135, 90, 90, 60); |
| 263 | + |
| 264 | + Braccio.ServoMovement(5, 0, 90, 150, 90, 90, 0); |
| 265 | +} |
0 commit comments