Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 0e4d6e9

Browse files
author
astronomer80
committed
Clean code
1 parent 37d0ba1 commit 0e4d6e9

File tree

6 files changed

+105
-69
lines changed

6 files changed

+105
-69
lines changed

examples/ciaoBraccio/ciaoBraccio.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ Servo wrist_ver;
4545
Servo gripper;
4646

4747
void setup() {
48-
//Initialization functions for Ciao
48+
//Initialization function for Ciao
4949
Ciao.begin();
50-
//Initialization functions for Braccio
51-
Braccio.begin(MEDIUM);
50+
//Initialization function for Braccio
51+
//You should set begin(SOFT_START_DISABLED) if you are using the Arm Robot shield V1.6
52+
Braccio.begin();
5253
}
5354

5455
/**

examples/simpleMovements/simpleMovements.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void setup() {
2929
//Wrist vertical (M4): 180 degrees
3030
//Wrist rotation (M5): 90 degrees
3131
//gripper (M6): 10 degrees
32-
Braccio.begin(MEDIUM);
32+
Braccio.begin();
3333
}
3434

3535
void loop() {

examples/takethesponge/takethesponge.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void setup() {
3030
//Wrist vertical (M4): 180 degrees
3131
//Wrist rotation (M5): 90 degrees
3232
//gripper (M6): 10 degrees
33-
Braccio.begin(MEDIUM);
33+
Braccio.begin();
3434
}
3535

3636
void loop() {

examples/testBraccio90/testBraccio90.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void setup() {
3232
//Wrist vertical (M4): 180 degrees
3333
//Wrist rotation (M5): 90 degrees
3434
//gripper (M6): 10 degrees
35-
Braccio.begin(MEDIUM);
35+
Braccio.begin();
3636
}
3737

3838
void loop() {

src/Braccio.cpp

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919

2020
#include "Braccio.h"
2121

22-
#define LOW_LIMIT_TIMEOUT 2000
23-
#define HIGH_LIMIT_TIMEOUT 6000
24-
25-
2622
extern Servo base;
2723
extern Servo shoulder;
2824
extern Servo elbow;
@@ -48,11 +44,15 @@ _Braccio::_Braccio() {
4844
* Braccio initialization and set intial position
4945
* Modifing this function you can set up the initial position of all the
5046
* servo motors of the Braccio
47+
* @param soft_start_level: SOFT_START_DISABLED, SOFT_START_SLOW, SOFT_START_MEDIUM, SOFT_START_FAST
48+
* You should set begin(SOFT_START_DISABLED) if you are using the Arm Robot shield V1.6
49+
* SOFT_START_DISABLED disable the movement
5150
*/
52-
unsigned int _Braccio::begin(int val) {
53-
54-
pinMode(12,OUTPUT);
55-
digitalWrite(12,LOW);
51+
unsigned int _Braccio::begin(int soft_start_level) {
52+
if(soft_start_level!=SOFT_START_DISABLED){
53+
pinMode(SOFT_START_CONTROL_PIN,OUTPUT);
54+
digitalWrite(SOFT_START_CONTROL_PIN,LOW);
55+
}
5656

5757
// initialization pin Servo motors
5858
base.attach(11);
@@ -78,61 +78,66 @@ unsigned int _Braccio::begin(int val) {
7878
step_gripper = 73;
7979

8080

81-
long int tmp=millis();
82-
// soft start implementation
83-
if (val==FAST){
81+
if(soft_start_level!=SOFT_START_DISABLED)
82+
_softStart(soft_start_level);
83+
return 1;
84+
}
8485

85-
while(millis()-tmp < LOW_LIMIT_TIMEOUT){
86-
digitalWrite(12,HIGH);
87-
delayMicroseconds(125);
88-
digitalWrite(12,LOW);
89-
delayMicroseconds(50);
90-
}
86+
/*
87+
Default implementation
88+
*/
89+
unsigned int _Braccio::begin() {
90+
//SOFT_START_SLOW is the default value
91+
return begin(SOFT_START_SLOW);
92+
}
93+
94+
/*
95+
Software implementation of the PWM for the SOFT_START_CONTROL_PIN,HIGH
96+
@param high_time: the time in the logic level high
97+
@param low_time: the time in the logic level low
98+
*/
99+
void _Braccio::_softwarePWM(int high_time, int low_time){
100+
digitalWrite(SOFT_START_CONTROL_PIN,HIGH);
101+
delayMicroseconds(high_time);
102+
digitalWrite(SOFT_START_CONTROL_PIN,LOW);
103+
delayMicroseconds(low_time);
104+
}
105+
106+
/*
107+
This function, used only with the Braccio Shield V4 and greater,
108+
turn ON the Braccio softly and slowly.
109+
The SOFT_START_CONTROL_PIN is used as a software PWM
110+
@parameter soft_start_level: The soft start level: FAST, MEDIUM, SLOW
111+
*/
112+
void _Braccio::_softStart(int soft_start_level){
113+
long int tmp=millis();
114+
if (soft_start_level==SOFT_START_FAST){
115+
while(millis()-tmp < LOW_LIMIT_TIMEOUT)
116+
_softwarePWM(125, 50);
91117

92-
while(millis()-tmp < HIGH_LIMIT_TIMEOUT){
93-
digitalWrite(12,HIGH);
94-
delayMicroseconds(125);
95-
digitalWrite(12,LOW);
96-
delayMicroseconds(105);
97-
}
118+
while(millis()-tmp < HIGH_LIMIT_TIMEOUT)
119+
_softwarePWM(125, 105);
98120

99-
}else if (val==MEDIUM){
100-
while(millis()-tmp < LOW_LIMIT_TIMEOUT){
101-
digitalWrite(12,HIGH);
102-
delayMicroseconds(15);
103-
digitalWrite(12,LOW);
104-
delayMicroseconds(125);
105-
}
106-
107-
while(millis()-tmp < HIGH_LIMIT_TIMEOUT){
108-
digitalWrite(12,HIGH);
109-
delayMicroseconds(15);
110-
digitalWrite(12,LOW);
111-
delayMicroseconds(145);
112-
}
121+
}else if (soft_start_level==SOFT_START_MEDIUM){
122+
while(millis()-tmp < LOW_LIMIT_TIMEOUT)
123+
_softwarePWM(15, 125);
113124

125+
while(millis()-tmp < HIGH_LIMIT_TIMEOUT)
126+
_softwarePWM(15, 145);
114127

115-
}else if (val==SLOW){
116-
while(millis()-tmp < LOW_LIMIT_TIMEOUT){
117-
digitalWrite(12,HIGH);
118-
delayMicroseconds(5);
119-
digitalWrite(12,LOW);
120-
delayMicroseconds(155);
121-
}
128+
}else if (soft_start_level==SOFT_START_SLOW){
129+
while(millis()-tmp < LOW_LIMIT_TIMEOUT)
130+
_softwarePWM(15, 155);
122131

123-
while(millis()-tmp < HIGH_LIMIT_TIMEOUT){
124-
digitalWrite(12,HIGH);
125-
delayMicroseconds(5);
126-
digitalWrite(12,LOW);
127-
delayMicroseconds(165);
128-
}
132+
while(millis()-tmp < HIGH_LIMIT_TIMEOUT)
133+
_softwarePWM(5, 165);
129134
}
130-
digitalWrite(12,HIGH);
131-
return 1;
135+
136+
digitalWrite(SOFT_START_CONTROL_PIN,HIGH);
132137
}
133138

134139
/**
135-
* This functions allow you to control all the servo motors in the Braccio setting the funcion parameters.
140+
* This functions allow you to control all the servo motors
136141
*
137142
* @param stepDelay The delay between each servo movement
138143
* @param vBase next base servo motor degree
@@ -144,7 +149,7 @@ unsigned int _Braccio::begin(int val) {
144149
*/
145150
int _Braccio::ServoMovement(int stepDelay, int vBase, int vShoulder, int vElbow,int vWrist_ver, int vWrist_rot, int vgripper) {
146151

147-
// Check values
152+
// Check values, to avoid dangerous positions for the Braccio
148153
if (stepDelay > 30) stepDelay = 30;
149154
if (stepDelay < 10) stepDelay = 10;
150155
if (vBase < 0) vBase=0;
@@ -245,9 +250,8 @@ int _Braccio::ServoMovement(int stepDelay, int vBase, int vShoulder, int vElbow,
245250
step_gripper--;
246251
}
247252
}
248-
249253

250-
//The delay between each movement
254+
//delay between each movement
251255
delay(stepDelay);
252256

253257
//It checks if all the servo motors are in the desired position
@@ -264,7 +268,5 @@ int _Braccio::ServoMovement(int stepDelay, int vBase, int vShoulder, int vElbow,
264268
} else {
265269
exit = 1;
266270
}
267-
268271
}
269-
270272
}

src/Braccio.h

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@
2323
#include <Arduino.h>
2424
#include <Servo.h>
2525

26-
#define FAST 2
27-
#define MEDIUM 3
28-
#define SLOW 4
26+
// You should set begin(SOFT_START_DISABLED) if you are using the Arm Robot shield V1.6
27+
#define SOFT_START_DISABLED 0
28+
//You can set different values for the softstart. SOFT_START_SLOW is the default value
29+
#define SOFT_START_FAST 2
30+
#define SOFT_START_MEDIUM 3
31+
#define SOFT_START_SLOW 4
2932

33+
//The software PWM is connected to PIN 12. You cannot use the pin 12 if you are using
34+
//a Braccio shield V4 or newer
35+
#define SOFT_START_CONTROL_PIN 12
36+
37+
//Low and High Limit Timeout for the Software PWM
38+
#define LOW_LIMIT_TIMEOUT 2000
39+
#define HIGH_LIMIT_TIMEOUT 6000
3040

3141
class _Braccio {
3242

@@ -38,12 +48,35 @@ class _Braccio {
3848
* Modifing this function you can set up the initial position of all the
3949
* servo motors of the Braccio
4050
*/
41-
unsigned int begin(int val);
42-
51+
unsigned int begin();
52+
53+
/**
54+
* @param soft_start_level: the softstart_level: SOFT_START_DISABLED, SOFT_START_SLOW, SOFT_START_MEDIUM, SOFT_START_FAST
55+
* You should set begin(SOFT_START_DISABLED) if you are using the Arm Robot shield V1.6
56+
*/
57+
unsigned int begin(int soft_start_level);
58+
4359
/**
4460
* This functions allow you to control all the servo motors in the Braccio
4561
*/
4662
int ServoMovement(int delay, int Vbase,int Vshoulder, int Velbow, int Vwrist_ver, int Vwrist_rot, int Vgripper);
63+
64+
65+
private:
66+
/*
67+
* This function, used only with the Braccio Shield V4 and greater,
68+
* turn ON the Braccio softly and slowly.
69+
* The SOFT_START_CONTROL_PIN is used as a software PWM
70+
* @parameter soft_start_level: The soft start level: FAST, MEDIUM, SLOW
71+
*/
72+
void _softStart(int soft_start_level);
73+
74+
/*
75+
* Software implementation of the PWM for the SOFT_START_CONTROL_PIN,HIGH
76+
* @param high_time: the time in the logic level high
77+
* @param low_time: the time in the logic level low
78+
*/
79+
void _softwarePWM(int high_time, int low_time);
4780

4881

4982
};

0 commit comments

Comments
 (0)