-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharmDriver.h
121 lines (107 loc) · 4.53 KB
/
armDriver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <Adafruit_PWMServoDriver.h>
/*
* Use Adafruit_PWMServoDriver to control multiple servos.
* Pin : use I2C , SDA 21 SCL 22
*
*/
const float ARM_MOVEMENT_STEP = 0.3;
class ArmManager {
private:
// static const uint8_t this->numServos = 8;
static const uint16_t SERVO_MIN_PULSE_WIDTH = 500;
static const uint16_t SERVO_MAX_PULSE_WIDTH = 2500;
Adafruit_PWMServoDriver pwm;
uint8_t numServos;
uint8_t *servoTargetAngles;
float *servoCurrentAngles;
uint8_t *servoMinAngles;
uint8_t *servoMaxAngles;
void setServoAngle(uint8_t servoNum, float angle) {
if (servoNum < this->numServos) {
pwm.writeMicroseconds(
servoNum,
map(angle, 0, 180,
SERVO_MIN_PULSE_WIDTH, SERVO_MAX_PULSE_WIDTH));
}
}
public:
ArmManager(const uint8_t numServos, const uint8_t servoMinAngles[], const uint8_t servoMaxAngles[]) {
// Initialize the Adafruit_PWMServoDriver object
this->numServos = numServos;
this->pwm.begin();
this->pwm.setOscillatorFrequency(27000000);
this->pwm.setPWMFreq(50); // Set the PWM frequency to 50Hz
this->servoTargetAngles = new uint8_t[numServos];
this->servoCurrentAngles = new float[numServos];
this->servoMinAngles = new uint8_t[numServos];
this->servoMaxAngles = new uint8_t[numServos];
// Set servoMinAngles and servoMaxAngles
for (uint8_t i = 0; i < numServos; i++) {
this->servoMinAngles[i] = servoMinAngles[i];
this->servoMaxAngles[i] = servoMaxAngles[i];
this->servoTargetAngles[i] = 90;
this->servoCurrentAngles[i] = 80;
Serial.printf("this->servoTargetAngles[%d] are ", i);
Serial.println(this->servoTargetAngles[i]);
Serial.printf("this->servoCurrentAngles[%d] are ", i);
Serial.println(this->servoCurrentAngles[i]);
}
}
void setServoTargetAngle(uint8_t servoNum, uint8_t targetAngle) {
if (servoNum < this->numServos) {
this->servoTargetAngles[servoNum] = constrain(targetAngle, servoMinAngles[servoNum],
servoMaxAngles[servoNum]);
}
}
void changeServoTargetAngle(uint8_t servoNum, int8_t biasAngle) {
if (servoNum < this->numServos) {
this->servoTargetAngles[servoNum] = constrain(servoTargetAngles[servoNum] + biasAngle,
servoMinAngles[servoNum], servoMaxAngles[servoNum]);
}
}
// Get the current angles of all servos, update currentAngles into passed array
void getCurrentAngles(float currentAngles[]) {
for (uint8_t i = 0; i < this->numServos; i++) {
currentAngles[i] = servoCurrentAngles[i];
}
}
void moveArm() {
for (uint8_t i = 0; i < this->numServos; i++) {
if (abs(this->servoCurrentAngles[i] - this->servoTargetAngles[i]) >= ARM_MOVEMENT_STEP) {
float step = (this->servoTargetAngles[i] > this->servoCurrentAngles[i]) ? ARM_MOVEMENT_STEP
: -ARM_MOVEMENT_STEP;
this->servoCurrentAngles[i] += step;
// this line will occur some delay to let device work not properly
// please make sure you had connect to PCA9685 pwm driver.
setServoAngle(i, this->servoCurrentAngles[i]);
// Serial.printf("this->servoTargetAngles[%d] are ",i);
// Serial.println(this->servoTargetAngles[i]);
// Serial.printf("this->servoCurrentAngles[%d] are ",i);
// Serial.println(this->servoCurrentAngles[i]);
}
}
}
void printStatus() {
Serial.println("Target:");
for (uint8_t i = 0; i < this->numServos; i++) {
Serial.print(this->servoTargetAngles[i]);
Serial.print(",");
}
Serial.println();
Serial.println("Current:");
for (uint8_t i = 0; i < this->numServos; i++) {
Serial.print(this->servoCurrentAngles[i]);
Serial.print(",");
}
Serial.println();
}
// void init()
// {
// for (uint8_t i = 0; i < this->numServos; i++)
// {
// this->servoTargetAngles[i] = initAngles[i];
// this->servoCurrentAngles[i] = initAngles[i];
// this->setServoAngle(i, this->servoCurrentAngles[i]);
// }
// }
};