-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinoRoboArm.h
136 lines (84 loc) · 3.21 KB
/
LinoRoboArm.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include <Adafruit_PWMServoDriver.h>
#include "Servo.h"
constexpr uint16_t MAX_PWM = 512; // Max PWM value
constexpr uint8_t JOINTS = 6; // Number of joints in the arm
/* ================================================================================================================================= */
class LinoRoboArm {
private:
Servo servos[JOINTS];
Adafruit_PWMServoDriver pwm;
public:
LinoRoboArm(uint16_t mins[], uint16_t maxs[]);
uint16_t GetJoints();
void SetServo(uint8_t ServoID, uint16_t val);
void SetServos(uint16_t val[]);
void moveServo(uint8_t ServoID, uint16_t val);
bool parseInput(String command);
String PrintPositions();
};
/* ================================================================================================================================= */
/**
* @brief LinoRoboArm constructor, initialize "Adafruit_PWMServoDriver" library
* @param mins Minimums degrees of the servors.
* @param maxs Maximums degrees of the servors.
* @return null
*/
LinoRoboArm::LinoRoboArm(uint16_t mins[], uint16_t maxs[]) {
for(uint8_t i = 0; i < JOINTS; i++)
servos[i] = Servo(mins[i], maxs[i]);
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(50);
}
/* ================================================================================================================================= */
/**
* @brief Get number of Joints in the Arm.
* @return Number of Joints.
*/
uint16_t LinoRoboArm::GetJoints() {
return JOINTS;
}
/**
* @brief Actuate single servo with the new position.
* @param servoID ID of the servo to configure.
* @param val New degree values.
* @return null
*/
void LinoRoboArm::SetServo(uint8_t ServoID, uint16_t val) {
servos[ServoID].SetServo(val);
moveServo(ServoID, val);
}
/**
* @brief Actuate all the servo with the new position.
* @param val Array of the new degree values.
* @return null
*/
void LinoRoboArm::SetServos(uint16_t val[]) {
for(uint8_t i = 0; i < JOINTS; i++)
SetServo(i, val[i]);
}
/* ================================================================================================================================= */
/**
* @brief Actuate the servo with the new position.
* @param val Array of the new degree values.
* @return null
*/
void LinoRoboArm::moveServo(uint8_t ServoID, uint16_t val) {
uint16_t PWMval = map(val, 0, MAX_PWM, servos[ServoID].GetMin(), servos[ServoID].GetMax());
pwm.setPWM(ServoID, 0, PWMval);
}
/* ================================================================================================================================= */
/**
* @brief Print on screen, using Serial.print, the current value of the postions of the servos.
* @return String with the postions, separated with a comma.
*/
String LinoRoboArm::PrintPositions() {
String aux = "";
for (uint8_t i = 0; i < JOINTS - 1; i++) {
aux += servos[i].GetServo();
aux += ",";
}
aux += servos[5].GetServo();
return aux;
}