@@ -20,18 +20,11 @@ uArmClass::uArmClass()
2020
2121}
2222
23- void uArmClass::readServoOffset (){
24- // Serial.println(g_linear_servo_rot_intercept);
25- // Serial.println(g_linear_servo_left_intercept);
26- // Serial.println(g_linear_servo_right_intercept);
27- // Serial.println(g_linear_servo_hand_intercept);
28- //
29- // Serial.println(g_linear_servo_rot_slope);
30- // Serial.println(g_linear_servo_left_slope);
31- // Serial.println(g_linear_servo_right_slope);
32- // Serial.println(g_linear_servo_hand_slope);
33- }
34-
23+ /* Use BUZZER for Alert
24+ * times - how many times
25+ * runTime - how long one time last when BUZZER speak
26+ * stopTime - Close BUZZER time
27+ */
3528void uArmClass::alert (byte times, byte runTime, byte stopTime)
3629{
3730 for (int ct=0 ; ct < times; ct++)
@@ -44,47 +37,49 @@ void uArmClass::alert(byte times, byte runTime, byte stopTime)
4437}
4538
4639
47- /* The code below is written by jerry song */
48-
40+ /* Write the angle to Servo
41+ * servo_number - SERVO_ROT_NUM, SERVO_LEFT_NUM, SERVO_RIGHT_NUM, SERVO_HAND_ROT_NUM
42+ * servo_angle - Servo target angle
43+ * writeWithoffset - True: with Offset, False: without Offset
44+ */
4945void uArmClass::writeServoAngle (byte servo_number, double servo_angle, boolean writeWithoffset)
5046{
51- // Serial.println(servo_angle);
5247 attachServo (servo_number);
5348 servo_angle = writeWithoffset ? round (inputToReal (servo_number,servo_angle)) : round (servo_angle);
5449 servo_angle = constrain (servo_angle,0 ,180 );
5550 switch (servo_number)
5651 {
5752 case SERVO_ROT_NUM : g_servo_rot.write (servo_angle);
58- // cur_rot = readAngle(SERVO_ROT_NUM);
5953 cur_rot = servo_angle;
6054 break ;
6155 case SERVO_LEFT_NUM : g_servo_left.write (servo_angle);
62- // cur_left = readAngle(SERVO_LEFT_NUM);
6356 cur_left = servo_angle;
6457 break ;
6558 case SERVO_RIGHT_NUM : g_servo_right.write (servo_angle);
66- // cur_right = readAngle(SERVO_RIGHT_NUM);
6759 cur_right = servo_angle;
6860 break ;
6961 case SERVO_HAND_ROT_NUM : g_servo_hand_rot.write (servo_angle);
70- // cur_hand = readAngle(SERVO_HAND_ROT_NUM);
7162 cur_hand = servo_angle;
7263 break ;
7364 default : break ;
7465 }
7566}
7667
68+
69+ /* Write the left Servo & Right Servo in the same time (Avoid demage the Servo)
70+ * servo_left_angle - left servo target angle
71+ * servo_right_angle - right servo target angle
72+ * writeWithoffset - True: with Offset, False: without Offset
73+ */
7774void uArmClass::writeLeftRightServoAngle (double servo_left_angle, double servo_right_angle, boolean writeWithoffset)
7875{
7976 servo_left_angle = constrain (servo_left_angle,0 ,150 );
8077 servo_right_angle = constrain (servo_right_angle,0 ,120 );
8178 servo_left_angle = writeWithoffset ? round (inputToReal (SERVO_LEFT_NUM ,servo_left_angle)) : round (servo_left_angle);
8279 servo_right_angle = writeWithoffset ? round (inputToReal (SERVO_RIGHT_NUM ,servo_right_angle)) : round (servo_right_angle);
83- if (servo_left_angle + servo_right_angle > 180 )
80+ if (servo_left_angle + servo_right_angle > 180 ) // if left angle & right angle exceed 180 degree, it might be caused damage
8481 {
85- // servo_right_angle = 160 - servo_left_angle;
8682 alert (1 , 10 , 0 );
87- // delay(10);
8883 return ;
8984 }
9085 attachServo (SERVO_LEFT_NUM );
@@ -93,6 +88,8 @@ void uArmClass::writeLeftRightServoAngle(double servo_left_angle, double servo_r
9388 g_servo_right.write (servo_right_angle);
9489}
9590
91+ /* Warning, if you attach left servo & right servo without a movement, it might be caused a demage
92+ */
9693void uArmClass::attachAll ()
9794{
9895 attachServo (SERVO_ROT_NUM );
@@ -101,6 +98,9 @@ void uArmClass::attachAll()
10198 attachServo (SERVO_HAND_ROT_NUM );
10299}
103100
101+ /* Attach Servo by given servo number, SERVO_ROT_NUM, SERVO_LEFT_NUM, SERVO_RIGHT_NUM
102+ * if servo has not been attached, attach the servo, and read the current Angle
103+ */
104104void uArmClass::attachServo (byte servo_number)
105105{
106106 switch (servo_number) {
@@ -131,6 +131,8 @@ void uArmClass::attachServo(byte servo_number)
131131 }
132132}
133133
134+ /* Detach All servo, you could move the arm
135+ */
134136void uArmClass::detachAll ()
135137{
136138 g_servo_rot.detach ();
@@ -139,45 +141,53 @@ void uArmClass::detachAll()
139141 g_servo_hand_rot.detach ();
140142}
141143
144+ /* get a input angle with servo Offset
145+ */
142146byte uArmClass::inputToReal (byte servo_num,byte input_angle)
143147{
144148 return (byte)constrain (round ((input_angle + readServoOffset (servo_num))),0 ,180 );
145149}
146150
151+ /* Read the servo offset from EEPROM, From OFFSET_START_ADDRESS, each offset occupy 4 bytes in rom
152+ */
147153double uArmClass::readServoOffset (byte servo_num)
148154{
149155 double linear_offset = 0 .0f ;
150156 EEPROM .get (OFFSET_START_ADDRESS + servo_num * sizeof (linear_offset), linear_offset);
151157 return linear_offset;
152158}
153159
154- /* * read Linear Offset from EEPROM
160+ /* * read Linear Offset from EEPROM,
161+ ** From LINEAR_INTERCEPT_START_ADDRESS & LINEAR_SLOPE_START_ADDRESS, each offset occupy 4 bytes in rom
155162 **/
156163void uArmClass::readLinearOffset (byte servo_num, double & intercept_val, double & slope_val){
157164 EEPROM .get (LINEAR_INTERCEPT_START_ADDRESS + servo_num * sizeof (intercept_val), intercept_val);
158165 EEPROM .get (LINEAR_SLOPE_START_ADDRESS + servo_num * sizeof (slope_val), slope_val);
159166}
160167
168+ /* * Convert the Analog to Servo Angle, by this formatter
169+ ** angle = intercept + slope * analog
170+ **/
161171double uArmClass::analogToAngle (int input_analog, byte servo_num, boolean withOffset)
162172{
163173 double intercept = 0 .0f ;
164174 double slope = 0 .0f ;
165175 readLinearOffset (servo_num, intercept, slope);
166-
167- // readLinearOffset(servo_num, LINEAR_SLOPE);
168176 double angle = intercept + slope*input_analog;
169- // Serial.println(input_analog);
170- // Serial.println(intercept);
171- // Serial.println(slope);
172- // Serial.println(angle);
173177 return withOffset ? angle - readServoOffset (servo_num) : angle;
174178}
175179
180+ /* * read Angle by servo_num, SERVO_ROT_NUM, SERVO_LEFT_NUM, SERVO_RIGHT_NUM, SERVO_HAND_ROT_NUM
181+ ** without offset
182+ **/
176183double uArmClass::readAngle (byte servo_num)
177184{
178185 return readAngle (servo_num, false );
179186}
180187
188+ /* * read Angle by servo_num, SERVO_ROT_NUM, SERVO_LEFT_NUM, SERVO_RIGHT_NUM, SERVO_HAND_ROT_NUM
189+ ** withOffset - True: with Offset, False: Without Offset
190+ **/
181191double uArmClass::readAngle (byte servo_num, boolean withOffset)
182192{
183193 switch (servo_num)
@@ -206,6 +216,8 @@ double uArmClass::readAngle(byte servo_num, boolean withOffset)
206216
207217/* Action control */
208218
219+ /* * Calculate the angles from given coordinate x, y, z to theta_1, theta_2, theta_3
220+ **/
209221void uArmClass::calAngles (double x, double y, double z, double & theta_1, double & theta_2, double & theta_3)
210222{
211223 if (z > (MATH_L1 + MATH_L3 + TopOffset))
@@ -319,25 +331,16 @@ void uArmClass::calAngles(double x, double y, double z, double& theta_1, double&
319331 {theta_3 = uarm.readAngle (SERVO_RIGHT_NUM ); }
320332}
321333
334+ /* * This is an old control method to uArm.
335+ ** Using uarm's Stretch and height
336+ ** Stretch from 0 to 195
337+ ** Height from -180 to 150
338+ **/
322339void uArmClass::writeStretch (double armStretch, double armHeight){
323340 if (EEPROM .read (CALIBRATION_STRETCH_FLAG ) != CALIBRATION_STRETCH_FLAG ) {
324341 alert (3 , 200 , 200 );
325342 return ;
326343 }
327-
328- // Serial.println(armStretch);
329- // Serial.println(armHeight);
330-
331- // if(!digitalRead(LIMIT_SW))//limit switch protection
332- // {
333- // // alert(1, 10, 0);
334- // if(armHeight < heightLst)
335- // {
336- // armHeight = heightLst;
337- // }
338- // }
339-
340-
341344 double offsetL = 0 ;
342345 double offsetR = 0 ;
343346 EEPROM .get (OFFSET_STRETCH_START_ADDRESS + 1 * sizeof (offsetL), offsetL);
@@ -352,28 +355,23 @@ void uArmClass::writeStretch(double armStretch, double armHeight){
352355 int angleR =(int )(angleB + offsetR - 4 );// int angleR =angleB + 40 + offsetR;
353356 int angleL =(int )(angleA + offsetL + 16 );// int angleL =25 + angleA + offsetL;
354357 angleL = constrain (angleL, 5 + offsetL, 145 + offsetL);
355- // writeServoAngle(SERVO_LEFT_NUM, angleL,1);
356- // writeServoAngle(SERVO_RIGHT_NUM,angleR,1);
357358 writeLeftRightServoAngle (angleL,angleR,true );
358359}
359360
360-
361- // void uArmClass::calStretch(double theta_2, double theta_3, double & l_length_get, double & l_height_get)
362- // {
363- // l_length_get = cos(theta_2)*MATH_L3 + cos(theta_3)*MATH_L4;
364- // l_height_get = sin(theta_2)*MATH_L3 - sin(theta_3)*MATH_L4;
365- // }
366-
361+ /* * Calculate the X Y Z by given theta_1, theta_2, theta_3
362+ **
363+ **/
367364void uArmClass::calXYZ (double theta_1, double theta_2, double theta_3)
368365{
369-
370366 double l5 = (MATH_L2 + MATH_L3 *cos (theta_2 / MATH_TRANS ) + MATH_L4 *cos (theta_3 / MATH_TRANS ));
371367
372368 g_cal_x = -cos (abs (theta_1 / MATH_TRANS ))*l5;
373369 g_cal_y = -sin (abs (theta_1 / MATH_TRANS ))*l5;
374370 g_cal_z = MATH_L1 + MATH_L3 *sin (abs (theta_2 / MATH_TRANS )) - MATH_L4 *sin (abs (theta_3 / MATH_TRANS ));
375371}
376372
373+ /* * Overload calXYZ()
374+ **/
377375void uArmClass::calXYZ ()
378376{
379377 calXYZ (
@@ -382,6 +380,8 @@ void uArmClass::calXYZ()
382380 uarm.analogToAngle (analogRead (SERVO_RIGHT_ANALOG_PIN ),SERVO_RIGHT_NUM ,false ));
383381}
384382
383+ /* * Action Control: Genernate the position array
384+ **/
385385void uArmClass::interpolate (double start_val, double end_val, double (&interp_vals)[INTERP_INTVLS], byte ease_type) {
386386 double delta = end_val - start_val;
387387 for (byte f = 0 ; f < INTERP_INTVLS ; f++) {
@@ -521,20 +521,24 @@ void uArmClass::moveToOpts(double x, double y, double z, double hand_angle, byte
521521
522522double uArmClass::calYonly (double theta_1, double theta_2, double theta_3)
523523{
524- // g_l3_1_2 = MATH_L3 * cos(theta_2 / MATH_TRANS);
525- // g_l4_1_2 = MATH_L4*cos(theta_3 / MATH_TRANS);
526524 double l5_2 = (MATH_L2 + MATH_L3 *cos (theta_2 / MATH_TRANS ) + MATH_L4 *cos (theta_3 / MATH_TRANS ));
527525
528526 return -sin (abs (theta_1 / MATH_TRANS ))*l5_2;
529527}
530528
529+ /* *
530+ Control the Gripper Catch
531+ **/
531532void uArmClass::gripperCatch ()
532533{
533534 pinMode (GRIPPER , OUTPUT );
534535 digitalWrite (GRIPPER , LOW ); // gripper catch
535536 g_gripper_reset = true ;
536537}
537538
539+ /* *
540+ Control the Gripper Release
541+ **/
538542void uArmClass::gripperRelease ()
539543{
540544 if (g_gripper_reset)
@@ -545,7 +549,9 @@ void uArmClass::gripperRelease()
545549 }
546550}
547551
548- /* Action Control */
552+ /* *
553+ Turn on the Pump
554+ **/
549555void uArmClass::pumpOn ()
550556{
551557
@@ -555,6 +561,9 @@ void uArmClass::pumpOn()
555561 digitalWrite (PUMP_EN , HIGH );
556562}
557563
564+ /* *
565+ Turn off the Pump
566+ **/
558567void uArmClass::pumpOff ()
559568{
560569 pinMode (PUMP_EN , OUTPUT );
0 commit comments