-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPedro.cpp
152 lines (136 loc) · 4.18 KB
/
Pedro.cpp
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
/*
* Pedro - A simple library to control the Meccanoid G15
* @doc http://cdn.meccano.com/open-source/Meccano_SmartModuleProtocols_2015.pdf
*/
#include "Pedro.h"
Pedro::Pedro(
int pinHead,
int pinRightShoulder,
int pinRightHand,
int pinLeftShoulder,
int pinLeftHand,
int pinLeftWheel1,
int pinLeftWheel2,
int pinLeftWheelEnable,
int pinRightWheel1,
int pinRightWheel2,
int pinRightWheelEnable
) {
this->_pins[0] = pinHead;
this->_pins[1] = pinRightShoulder;
this->_pins[2] = pinRightHand;
this->_pins[3] = pinLeftShoulder;
this->_pins[4] = pinLeftHand;
this->_pins[5] = pinLeftWheel1;
this->_pins[6] = pinLeftWheel2;
this->_pins[7] = pinLeftWheelEnable;
this->_pins[8] = pinRightWheel1;
this->_pins[9] = pinRightWheel2;
this->_pins[10] = pinRightWheelEnable;
for (int i = 0; i < 5; i++)
this->_controllers[i] = new MeccaBrain(this->_pins[i]);
}
Pedro::~Pedro()
{
for (int i = 0; i < 5; i++)
delete(this->_controllers[i]);
}
void Pedro::setPin(int pin, ComponentType type)
{
unsigned char index = 0;
while (index >>= 1 != 0)
index += 1;
if (index < 0 || index > 5)
return;
this->_pins[index] = pin;
}
void Pedro::setEyesColor(byte selector, byte r, byte g, byte b, byte fadeTime)
{
if (!(selector & Pedro::HEAD))
return;
if (this->_pins[0] == -1)
return;
this->_controllers[0]->communicate();
this->_controllers[0]->setLEDColor(r, g, b, fadeTime);
}
void Pedro::setServoColor(byte selector, int servoNum, byte color)
{
unsigned char index = 0;
for (byte mask = 00000001; mask > 0; mask <<= 1, index++) {
if (!(selector & mask))
continue;
if ((selector & Pedro::HEAD) || (selector & Pedro::LEFT_WHEEL) || (selector & Pedro::RIGHT_WHEEL))
continue;
if (this->_pins[index] == -1)
continue;
this->_controllers[index]->communicate();
this->_controllers[index]->setServoColor(servoNum, color);
}
}
void Pedro::setServoPosition(byte selector, int servoNum, byte pos)
{
unsigned char index = 0;
for (byte mask = 00000001; mask > 0; mask <<= 1, index++) {
if (!(selector & mask))
continue;
if ((selector & Pedro::HEAD) || (selector & Pedro::LEFT_WHEEL) || (selector & Pedro::RIGHT_WHEEL))
continue;
if (this->_pins[index] == -1)
continue;
this->_controllers[index]->communicate();
this->_controllers[index]->setServoPosition(servoNum, pos);
}
}
void Pedro::getServoPosition(byte selector)
{
unsigned char index = 0;
for (byte mask = 00000001; mask > 0; mask <<= 1, index++) {
if (!(selector & mask))
continue;
if ((selector & Pedro::HEAD) || (selector & Pedro::LEFT_WHEEL) || (selector & Pedro::RIGHT_WHEEL))
continue;
if (this->_pins[index] == -1)
continue;
this->_controllers[index]->communicate();
this->_controllers[index]->getServoPosition(0);
}
}
void Pedro::communicate(byte selector)
{
unsigned char index = 0;
for (byte mask = 00000001; mask > 0; mask <<= 1, index++) {
if (!(selector & mask))
continue;
if ((selector & Pedro::LEFT_WHEEL) || (selector & Pedro::RIGHT_WHEEL))
continue;
if (this->_pins[index] == -1)
continue;
this->_controllers[index]->communicate();
}
}
void Pedro::move(byte selector, int speed)
{
unsigned char index = 0;
char pin1 = 0;
char pin2 = 0;
char pinEnable = 0;
if (abs(speed) > 255)
return;
for (byte mask = 00000001; mask > 0; mask <<= 1, index++) {
if (!(selector & mask))
continue;
if (mask & Pedro::LEFT_WHEEL) {
pin1 = this->_pins[5];
pin2 = this->_pins[6];
pinEnable = this->_pins[7];
} else if (mask & Pedro::RIGHT_WHEEL) {
pin1 = this->_pins[8];
pin2 = this->_pins[9];
pinEnable = this->_pins[10];
} else
continue;
digitalWrite(pinEnable, abs(speed));
analogWrite(pin1, !(speed > 0));
digitalWrite(pin2, (speed > 0));
}
}