timeToMove<\/strong> (en milisegundos).<\/p>\n\/\/\r\n\/\/ Walbi, the walking biped\r\n\/\/\r\n\/\/ a project by The Inner Geek \/ Pedro Tavares Silva\r\n\/\/\r\n\/\/ Tutorial at https:\/\/releasetheinnergeek.wordpress.com\/\r\n\/\/\r\n\/\/ Hardware: - Arduino Nano\r\n\/\/ - LewanSoul LX16-A serial servos x10\r\n\/\/ - LewanSoul Debug Board\r\n\/\/\r\n\/\/ GNU General Public License v2.0\r\n\/\/\r\n\/\/ This program reads and displays the angle of each servo of the robot\r\n\/\/\r\n\r\n#include <SoftwareSerial.h>\r\n\r\nSoftwareSerial mySerial(10, 11); \/\/ 10 = Arduino soft RX, 11 = Arduino soft TX\r\n\r\nString message = \"\";\r\n\r\nclass CommandFeedback\r\n{\r\n public:\r\n byte servoId;\r\n byte command;\r\n byte parameter[7];\r\n byte numPar;\r\n boolean messageComplete = false;\r\n int angle(void) {\r\n return (int(this->parameter[1]) * 256 + int(this->parameter[0]));\r\n };\r\n};\r\n\r\nCommandFeedback response;\r\n\r\nvoid setup() {\r\n\r\n Serial.begin(115200); \/\/ hardware serial - USB cable from Arduino to PC running Arduino IDE Serial Monitor\r\n while (!Serial) {}\r\n\r\n Serial.println(\"\\n\\nHardware serial ready\");\r\n\r\n mySerial.begin(115200); \/\/ SoftwareSerial - connects Arduino to Debug Board serial pins (RX->TX, TX->RX, GND->GND)\r\n\r\n Serial.println(\"PROGRAM VERSION 1.1.5\\n\\nSoftware serial ready\\n\\nSend 'R' to read servos\");\r\n\r\n}\r\n\r\n\/\/ ========== SEND COMMAND =====================================\r\n\/\/ =============================================================\r\n\r\nvoid sendReadCommandToServos(byte servoID)\r\n{\r\n byte command[6];\r\n\r\n command [0] = 0x55;\r\n command [1] = 0x55;\r\n command [2] = servoID;\r\n command [3] = 3;\r\n command [4] = 28; \/\/ SERVO_POS_READ\r\n command [5] = (~(servoID + 3 + 28)) & 0xff;\r\n\r\n mySerial.write(command, sizeof(command));\r\n}\r\n\r\nvoid process() {\r\n\r\n if (sizeof(message)) {\r\n\r\n switch (message.length()) {\r\n case 1: \/\/ discard message if first byte is invalid\r\n if (message[0] != 85) {\r\n message = \"\";\r\n \/\/ Serial.println(\"Discarder invalid byte\");\r\n } else\r\n \/\/ Serial.println(\"First byte ok\");\r\n break;\r\n case 2: \/\/ discard message if second byte is invalid\r\n if (message[1] != 85) {\r\n message = \"\";\r\n \/\/ Serial.println(\"Discarded two invalid bytes\");\r\n } else\r\n \/\/ Serial.println(\"Second byte ok\");\r\n break;\r\n case 3:\r\n case 4:\r\n case 5:\r\n case 6: \/\/ not enough data, wait for more data\r\n \/\/ Serial.println(\"Waiting for more data\");\r\n break;\r\n default: \/\/ 7 and above\r\n \/\/ Serial.println(\"Analyzing...\");\r\n if (message.length() == (byte(message[3]) + 3)) { \/\/ servo message complete\r\n \/\/ Serial.println(\"Got enough data.\");\r\n if (false) {\/\/ will check checksum in next version\r\n \/\/ checksum = (~(id + length + cmd + [5..len-1])) & 0xff\r\n message = \"\";\r\n Serial.println(\"Bad checksum. Discarded data.\");\r\n } else {\r\n response.servoId = int(message[2]);\r\n response.command = int(message[4]);\r\n\r\n for (int i = 0; i < (int(message[3]) - 3); i++)\r\n response.parameter[i] = message[5 + i];\r\n\r\n response.numPar = message[3] - 3;\r\n response.messageComplete = true;\r\n message = \"\";\r\n\r\n\/\/ Serial.println(\"Good data. Loaded into object.\");\r\n }\r\n } else {\r\n \/\/ Serial.print(\"Not enough data. Needed \");\r\n \/\/ Serial.print(int(message[3]) + 3);\r\n \/\/ Serial.print(\" bytes and got \");\r\n \/\/ Serial.println(message.length());\r\n }\r\n }\r\n }\r\n}\r\n\r\nboolean listen(int timeOut)\r\n{\r\n int wait = 0;\r\n char incomming;\r\n\r\n do {\r\n if (mySerial.available()) {\r\n incomming = mySerial.read();\r\n if (incomming == char(-1))\r\n Serial.println(\"ERROR reading SoftwareSerial\");\r\n else {\r\n message += incomming;\r\n \/\/ Serial.print(\"\\nmessage is now \");\r\n \/\/ Serial.print(message.length());\r\n \/\/ Serial.println(\" chars long\\n\");\r\n \/\/ Serial.print(\"Going to process string <\");\r\n \/\/ Serial.print(message);\r\n \/\/ Serial.println(\">\");\r\n process();\r\n }\r\n }\r\n wait++;\r\n delay(1);\r\n } while ((wait < timeOut) and (!response.messageComplete));\r\n\r\n if (wait == timeOut) {\r\n Serial.println(\"ERROR: timed-out\");\r\n return (false);\r\n } else\r\n return (true);\r\n}\r\n\r\nvoid loop() {\r\n\r\n byte incomingByte;\r\n\r\n if (Serial.available() > 0) { \/\/ wait for command R\r\n\r\n incomingByte = Serial.read();\r\n\r\n if (char(incomingByte) == 'R') {\r\n\r\n for (int i = 1; i <= 10; i++) {\r\n\r\n\r\n for (int a = 1; a <= 10; a++) {\r\n sendReadCommandToServos(i); \/\/read servo 1\r\n if (listen(20))\r\n break;\r\n }\r\n\r\n if (response.messageComplete) {\r\n Serial.print(\"Servo \");\r\n Serial.print(response.servoId);\r\n Serial.print(\" angle: \");\r\n Serial.println(response.angle());\r\n response.messageComplete = false;\r\n }\r\n\r\n }\r\n }\r\n else {\r\n Serial.print(\"\\n\\nInvalid command received: \");\r\n Serial.print(\"(\" + String(char(incomingByte)) + \")\");\r\n }\r\n\r\n }\r\n\r\n}<\/pre>\n\/\/\r\n\/\/ Walbi, the walking biped\r\n\/\/\r\n\/\/ a project by The Inner Geek \/ Pedro Tavares Silva\r\n\/\/\r\n\/\/ Tutorial at https:\/\/releasetheinnergeek.wordpress.com\/\r\n\/\/\r\n\/\/ Hardware: - Arduino Nano\r\n\/\/ - LewanSoul LX16-A serial servos x10\r\n\/\/ - LewanSoul Debug Board\r\n\/\/\r\n\/\/ GNU General Public License v2.0\r\n\/\/\r\n\/\/ This program replays a series os poses that can be recorded with Walbi_record.ino and \r\n\/\/ stored in the poseAngles array.\r\n\/\/\r\n\r\n#include <SoftwareSerial.h>\r\nSoftwareSerial mySerial(10, 11); \/\/ pin 10 = Arduino soft RX (connects to debug board TX)\r\n \/\/ pin 11 = Arduino soft TX (debub board RX)\r\n\r\n\/\/\r\n\/\/ This code by LewanSoul to control LX16-A serial servos (not provided as library)\r\n\/\/\r\n\r\n#define GET_LOW_BYTE(A) (byte)((A))\r\n#define GET_HIGH_BYTE(A) (byte)((A) >> 8)\r\n#define BYTE_TO_HW(A, B) ((((unsigned int)(A)) << 8) | (byte)(B))\r\n#define LOBOT_SERVO_FRAME_HEADER 0x55\r\n#define LOBOT_SERVO_MOVE_TIME_WRITE 1\r\n\r\nbyte buf[10];\r\n\r\nbyte LobotCheckSum()\r\n{\r\n byte i;\r\n unsigned int temp = 0;\r\n for (i = 2; i < buf[3] + 2; i++) {\r\n temp += buf[i];\r\n }\r\n temp = ~temp;\r\n i = (byte)temp;\r\n return i;\r\n}\r\n\r\nvoid moveServo(byte id, unsigned int position, unsigned int time)\r\n{\r\n buf[0] = buf[1] = LOBOT_SERVO_FRAME_HEADER;\r\n buf[2] = id;\r\n buf[3] = 7;\r\n buf[4] = 1; \/\/LOBOT_SERVO_MOVE_TIME_WRITE\r\n buf[5] = GET_LOW_BYTE(position);\r\n buf[6] = GET_HIGH_BYTE(position);\r\n buf[7] = GET_LOW_BYTE(time);\r\n buf[8] = GET_HIGH_BYTE(time);\r\n buf[9] = LobotCheckSum();\r\n mySerial.write(buf, 10);\r\n}\r\n\r\n\/\/\r\n\/\/ End of code by LewanSoul\r\n\/\/\r\n\r\nvoid setup() {\r\n\r\n Serial.begin(115200); \/\/ hardware serial - USB cable from Arduino to PC running Arduino IDE Serial Monitor\r\n while (!Serial) {}\r\n\r\n mySerial.begin(115200); \/\/ SoftwareSerial - connects Arduino to Debug Board serial pins (RX->TX, TX->RX, GND->GND)\r\n\r\n Serial.println(\"Walby play VERSION 1.0.0\");\r\n}\r\n\r\nint timeToMove = 1500;\r\n\r\nconst byte numPoses = 5; \/\/ replaying a sequence of 5 poses, using 3 different poses recorded previously\r\n\r\nunsigned int duration [numPoses] = {timeToMove, \/\/ how many miliseconds the robot has to move from pose 0 > 1\r\n timeToMove, \/\/ from pose 1 > 2\r\n timeToMove, \/\/ ...\r\n timeToMove,\r\n timeToMove\r\n };\r\n\r\nboolean runOnce = true; \/\/ when the program starts the robot goes immediatly to the first pose\r\n\r\nbyte pose [numPoses] = {0, 1, 2, 1, 0}; \/\/ this is the order the poses will be played back\r\n\r\nunsigned int poseAngles [3] [10] = {\r\n {565, 576, 538, 505, 357, 418, 489, 489, 641, 479}, \/\/ each pose has the angle value for each of the 10 servos\r\n {372, 575, 894, 504, 533, 605, 499, 164, 641, 292},\r\n {294, 577, 1004, 505, 586, 672, 495, 48, 640, 234}\r\n};\r\n\r\nvoid loop() {\r\n\r\n byte incomingByte;\r\n\r\n if (runOnce)\r\n {\r\n for (int b = 0; b < 10; b++)\r\n moveServo( b + 1, poseAngles [0] [b], 500 - b );\r\n Serial.println(\"Starting posture\");\r\n runOnce = false;\r\n }\r\n\r\n Serial.println(\"Enter the number of the last pose to replay (first is zero, last is 4)\");\r\n do {} while (Serial.available() == 0);\r\n\r\n incomingByte = Serial.read() - '0';\r\n\r\n if ((incomingByte >= 0) and (incomingByte < numPoses))\r\n {\r\n Serial.print(\"\\nPlaying movements 0 to \");\r\n Serial.println(incomingByte);\r\n for (int a = 0; a < incomingByte + 1; a++)\r\n {\r\n for (int b = 0; b < 10; b++)\r\n moveServo(b + 1, poseAngles[ pose [a] ] [b], duration [a] - b );\r\n Serial.print(\"Pose: \");\r\n Serial.println( pose [a] );\r\n delay( duration [a] - 10 );\r\n };\r\n } else\r\n Serial.println(\"\\nInvalid command!\");\r\n\r\n}<\/pre>\n