forked from karfly/balanceball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanceball.cpp
192 lines (137 loc) · 4.74 KB
/
balanceball.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "./platform/platform.hpp"
#include "./camera/camera.hpp"
#include "./pid/pid.hpp"
#include <iostream>
#include <cmath>
#include <unistd.h>
#define REFRESH_PERIOD 5 // In milliseconds
#define ESC_SC 27 // ESC scan code
#define NORMAL 0
#define SQUARE 1
#define X_BALANCE 440
#define Y_BALANCE 300
#define SQUARE_SIDE 140
#define SQUARE_PERIOD 250
using std::cout;
using std::cerr;
using std::endl;
int main(int argc, char* argv[])
{
int mode = NORMAL;
if ( argc == 2 )
{
if ( !strcmp( argv[1], "square" ) )
{
mode = SQUARE;
}
}
Platform platform( "/dev/tty.usbmodem1421", 9600 );
if ( platform.fail() )
{
cerr << platform.getErrorStr() << endl;
exit( EXIT_FAILURE );
}
Camera camera;
if ( camera.fail() )
{
cerr << camera.getErrorStr() << endl;
exit( EXIT_FAILURE );
}
PID pidServoX( "./pid/pidServoX_config" );
if ( pidServoX.fail() )
{
cerr << pidServoX.getErrorStr() << endl;
exit( EXIT_FAILURE );
}
PID pidServoY( "./pid/pidServoX_config" );
if ( pidServoY.fail() )
{
cerr << pidServoY.getErrorStr() << endl;
exit( EXIT_FAILURE );
}
switch( mode )
{
case NORMAL:
{
while ( true )
{
camera.refresh();
///
cout << "-----------" << endl;
cout << "X: " << camera.getX() << endl;
cout << "Y: " << camera.getY() << endl;
///
pidServoX.compute( X_BALANCE - camera.getX() );
pidServoY.compute( Y_BALANCE - camera.getY() );
cout << "errorX: " << X_BALANCE - camera.getX() << endl;
cout << "errorY: " << Y_BALANCE - camera.getY() << endl;
cout << "signalX: " << pidServoX.getOutput() << endl;
cout << "signalY: " << pidServoY.getOutput() << endl;
platform.setAngles( -pidServoX.getOutput(), -pidServoY.getOutput() );
cout << "-----------" << endl;
if ( cv::waitKey(REFRESH_PERIOD) == ESC_SC ) // Waiting for 'ESC' key press for 30ms. If 'ESC' key is pressed, break loop
{
platform.setAngles( 0, 0 );
cout << "'ESC' key was pressed." << endl;
break;
}
}
break;
}
case SQUARE:
{
int xSquare = X_BALANCE - SQUARE_SIDE;
int ySquare = Y_BALANCE - SQUARE_SIDE;
int count = 0;
while ( true )
{
camera.refresh();
///
cout << "-----------" << endl;
cout << "X: " << camera.getX() << endl;
cout << "Y: " << camera.getY() << endl;
///
count++;
if ( count == SQUARE_PERIOD / REFRESH_PERIOD * 1)
{
xSquare = X_BALANCE + SQUARE_SIDE;
ySquare = Y_BALANCE - SQUARE_SIDE;
}
if ( count == SQUARE_PERIOD / REFRESH_PERIOD * 2 )
{
xSquare = X_BALANCE + SQUARE_SIDE;
ySquare = Y_BALANCE + SQUARE_SIDE;
}
if ( count == SQUARE_PERIOD / REFRESH_PERIOD * 3)
{
xSquare = X_BALANCE - SQUARE_SIDE;
ySquare = Y_BALANCE + SQUARE_SIDE;
}
if ( count == SQUARE_PERIOD / REFRESH_PERIOD * 4)
{
xSquare = X_BALANCE - SQUARE_SIDE;
ySquare = Y_BALANCE - SQUARE_SIDE;
count = 0;
}
pidServoX.compute( xSquare - camera.getX() );
pidServoY.compute( ySquare - camera.getY() );
cout << "squareX: " << xSquare << endl;
cout << "squareY: " << ySquare << endl;
cout << "errorX: " << xSquare - camera.getX() << endl;
cout << "errorY: " << ySquare - camera.getY() << endl;
cout << "signalX: " << pidServoX.getOutput() << endl;
cout << "signalY: " << pidServoY.getOutput() << endl;
platform.setAngles( -pidServoX.getOutput(), -pidServoY.getOutput() );
cout << "-----------" << endl;
if ( cv::waitKey(REFRESH_PERIOD) == ESC_SC ) // Waiting for 'ESC' key press for 30ms. If 'ESC' key is pressed, break loop
{
platform.setAngles( 0, 0 );
cout << "'ESC' key was pressed." << endl;
break;
}
}
break;
}
}
exit( EXIT_SUCCESS );
}