|
| 1 | +#include <EEPROM.h> |
| 2 | +#include <Wire.h> |
| 3 | +#include <uArm_library.h> |
| 4 | +#include <Servo.h> |
| 5 | + |
| 6 | + // define uarm class |
| 7 | + |
| 8 | +void hitPoint(int pointNumber); // predefine a hitPoint function |
| 9 | + |
| 10 | +void setup() { |
| 11 | + Wire.begin(); // join i2c bus (address optional for master) |
| 12 | + Serial.begin(9600); // start serial port at 9600 bps |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +int pointNumber = 7; // there are 7 points in total need to be hit |
| 17 | +int theta4Offset = 90; // the angle between the 4th servo and 1st servo |
| 18 | +double h1 = 10; // the height of the point |
| 19 | +double h2 = 5; // the height of the point above the hitting point (get ready to hit) |
| 20 | +double y1 = -22; // the distance along the y axis |
| 21 | + |
| 22 | +int timeDelay1 = 100; // time delay |
| 23 | +int timeDelayBetweenPoints = 500; // time delay between hitting two points |
| 24 | +int point1Delay = 1000; // before hitting the first point, uarm will move above it in advance |
| 25 | + |
| 26 | +double points[7][3] = { // define 7 points, each point has an unique location (x,y,z) |
| 27 | + { 7.1, y1, h1} , // point 1 |
| 28 | + { 4.5, y1, h1} , // point 2 |
| 29 | + { 2.2, y1, h1} , // point 3 |
| 30 | + { -0.3, y1, h1} , // point 4 |
| 31 | + { -3.6, y1, h1} , // point 5 |
| 32 | + { -6.3, y1, h1} , // point 6 |
| 33 | + { -9, y1, h1} , // point 7 |
| 34 | +}; |
| 35 | + |
| 36 | + |
| 37 | +void loop() { |
| 38 | + |
| 39 | + if(Serial.available()>0) |
| 40 | + { |
| 41 | + char readSerial = Serial.read(); // read serial input |
| 42 | + Serial.println(readSerial); |
| 43 | + |
| 44 | + switch (readSerial) |
| 45 | + { |
| 46 | + // case 1 : execute hitting process |
| 47 | + case '1': // input number 1 to strart hitting |
| 48 | + { |
| 49 | + hitPoints(); // hitPoints function shows below |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + // case 2: move to a home position (x,y,z) = (0,-20,10) |
| 54 | + case '2': |
| 55 | + { |
| 56 | + uarm.moveTo(0,-20,10); |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + // case 3: detach all servos that you can manually move uArm |
| 61 | + case '3': |
| 62 | + { |
| 63 | + uarm.detachAll(); |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + // case 4: play a piece of music if you have a toy piano |
| 68 | + case '4': |
| 69 | + { |
| 70 | + playMusic(); |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + // ignore other inputs |
| 76 | + default: |
| 77 | + break; |
| 78 | + } |
| 79 | + delay(50); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +// the whole hitting process |
| 85 | +void hitPoints() |
| 86 | +{ |
| 87 | + moveToPoint(1,true); // move to the point above the first point and get ready to hit |
| 88 | + delay(point1Delay); |
| 89 | + |
| 90 | + for (int i = 1; i<pointNumber; i ++) // execute hitting process from the first point to the last point |
| 91 | + { |
| 92 | + hitPoint(i); // each hit process |
| 93 | + delay(timeDelayBetweenPoints); |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +// each hitting procedure |
| 100 | +void hitPoint(int pointNumber) |
| 101 | +{ |
| 102 | + moveToPoint(pointNumber,true); // move to the point above the point, |
| 103 | + |
| 104 | + delay(timeDelay1); |
| 105 | + moveToPoint(pointNumber,false); // move downwards |
| 106 | + |
| 107 | + delay(timeDelay1); |
| 108 | + moveToPoint(pointNumber,true); // quick move upwards ( back ) |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +// move uarm to the point |
| 113 | +void moveToPoint(int pointNumber, boolean highPosition) |
| 114 | +{ |
| 115 | + if (highPosition == true){ // "true" means the end-effector will move to the point above hitting point. The height of this point is h2 above the real hitting point |
| 116 | + uarm.calAngles(points[pointNumber-1][0],points[pointNumber-1][1],points[pointNumber-1][2]+h2); // calculate the angle need to be executed by implement inverse kinematics |
| 117 | + } |
| 118 | + else{ |
| 119 | + uarm.calAngles(points[pointNumber-1][0],points[pointNumber-1][1],points[pointNumber-1][2]); |
| 120 | + } |
| 121 | + |
| 122 | + delay(10); |
| 123 | + uarm.writeAngle(uarm.getTheta1(),uarm.getTheta2(),uarm.getTheta3(),uarm.getTheta1()+theta4Offset); // execute calculated angles by getting each angle |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +// play a piece of music |
| 128 | +void playMusic() |
| 129 | +{ |
| 130 | + // the music is called |
| 131 | + int shortDelay = 200; |
| 132 | + int longDelay = 700; |
| 133 | + |
| 134 | + moveToPoint(1,true); |
| 135 | + delay(longDelay*2); |
| 136 | + |
| 137 | + hitPoint(1); |
| 138 | + delay(shortDelay); |
| 139 | + hitPoint(2); |
| 140 | + delay(longDelay); |
| 141 | + hitPoint(1); |
| 142 | + delay(shortDelay); |
| 143 | + hitPoint(2); |
| 144 | + delay(longDelay); |
| 145 | + hitPoint(1); |
| 146 | + delay(shortDelay); |
| 147 | + hitPoint(2); |
| 148 | + delay(shortDelay); |
| 149 | + hitPoint(3); |
| 150 | + delay(shortDelay); |
| 151 | + hitPoint(4); |
| 152 | + delay(shortDelay); |
| 153 | + hitPoint(5); |
| 154 | + delay(shortDelay*4); |
| 155 | + |
| 156 | + hitPoint(5); |
| 157 | + delay(shortDelay); |
| 158 | + hitPoint(3); |
| 159 | + delay(longDelay); |
| 160 | + |
| 161 | + hitPoint(5); |
| 162 | + delay(shortDelay); |
| 163 | + hitPoint(3); |
| 164 | + delay(longDelay); |
| 165 | + |
| 166 | + |
| 167 | + hitPoint(5); |
| 168 | + delay(shortDelay); |
| 169 | + hitPoint(4); |
| 170 | + delay(shortDelay); |
| 171 | + hitPoint(3); |
| 172 | + delay(shortDelay); |
| 173 | + hitPoint(2); |
| 174 | + delay(shortDelay); |
| 175 | + hitPoint(1); |
| 176 | + delay(shortDelay*8); |
| 177 | + |
| 178 | +} |
| 179 | + |
0 commit comments