This repository was archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservo.cpp
More file actions
171 lines (151 loc) · 3.58 KB
/
servo.cpp
File metadata and controls
171 lines (151 loc) · 3.58 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "servo.h"
ServoController::ServoController(MyRio_I2c* i2c, uint8_t address)
{
i2c_ = i2c;
address_ = address;
reset();
enable();
}
ServoController:: ~ServoController()
{
reset();
delay(10);
}
void ServoController::setAddress(uint8_t address)
{
uint8_t writeData[2] = {SC_SET_ID, address};
I2c_Write(i2c_, address_, writeData, 2);
delay(10);
}
uint16_t ServoController::batteryVoltage()
{
uint8_t writeData = SC_VOLTAGE;
I2c_Write(i2c_, address_, &writeData, 1);
uint8_t readData[2];
delay(10);
I2c_Read(i2c_, address_, readData, 2);
delay(10);
return (readData[0] << 8) + readData[1];
}
void ServoController::setWDTReset()
{
uint8_t writeData = SC_WDT_STOP;
I2c_Write(i2c_, address_, &writeData, 1);
delay(10);
}
void ServoController::enable()
{
uint8_t writeData = SC_ENABLE;
I2c_Write(i2c_, address_, &writeData, 1);
delay(30);
}
void ServoController::reset()
{
uint8_t writeData = SC_RESET;
I2c_Write(i2c_, address_, &writeData, 1);
delay(30);
}
void ServoController::setSpeed(uint8_t servo, uint8_t speed)
{
uint8_t writeData[2];
if (servo == 1)
writeData[0] = SC_S1_SPEED;
else if (servo == 2)
writeData[0] = SC_S2_SPEED;
else if (servo == 3)
writeData[0] = SC_S3_SPEED;
else if (servo == 4)
writeData[0] = SC_S4_SPEED;
else
return;
writeData[1] = speed;
I2c_Write(i2c_, address_, writeData, 2);
delay(10);
}
void ServoController::setSpeed(uint8_t speed1, uint8_t speed2, uint8_t speed3, uint8_t speed4, uint8_t speed5 = 0, uint8_t speed6 = 0)
{
uint8_t writeData[7];
writeData[0] = SC_S16_SPEED;
writeData[1] = speed1;
writeData[2] = speed2;
writeData[3] = speed3;
writeData[4] = speed4;
writeData[5] = speed5;
writeData[6] = speed6;
I2c_Write(i2c_, address_, writeData, 7);
delay(10);
}
void ServoController::setPosition(uint8_t servo, uint8_t deg)
{
uint8_t writeData[2];
if (servo == 1)
writeData[0] = SC_S1_POSITION;
else if (servo == 2)
writeData[0] = SC_S2_POSITION;
else if (servo == 3)
writeData[0] = SC_S3_POSITION;
else if (servo == 4)
writeData[0] = SC_S4_POSITION;
else
return;
writeData[1] = deg;
I2c_Write(i2c_, address_, writeData, 2);
delay(10);
}
void ServoController::setPositions(uint8_t pos1, uint8_t pos2, uint8_t pos3, uint8_t pos4, uint8_t pos5 = 0, uint8_t pos6 = 0)
{
uint8_t writeData[7];
writeData[0] = SC_S16_POSITION;
writeData[1] = pos1;
writeData[2] = pos2;
writeData[3] = pos3;
writeData[4] = pos4;
writeData[5] = pos5;
writeData[6] = pos6;
I2c_Write(i2c_, address_, writeData, 7);
delay(10);
}
uint8_t ServoController::readPosition(uint8_t servo)
{
uint8_t writeData[1];
if (servo == 1)
writeData[0] = SC_S1_READ;
if (servo == 2)
writeData[0] = SC_S2_READ;
if (servo == 3)
writeData[0] = SC_S3_READ;
if (servo == 4)
writeData[0] = SC_S4_READ;
uint8_t res;
I2c_Write(i2c_, address_, writeData, 1);
delay(10);
I2c_Read(i2c_, address_, &res, 1);
delay(10);
return res;
}
void ServoController::openLeft()
{
setPosition(1, 70);
}
void ServoController::openRight()
{
setPosition(4, 10);
}
void ServoController::closeLeft()
{
setPosition(1, 7);
}
void ServoController::closeRight()
{
setPosition(4, 75);
}
void ServoController::up()
{
setPosition(2, 140);
setPosition(3, 0);
}
void ServoController::down()
{
setPosition(2, 25);
setPosition(3, 95);
}