forked from tellefo92/jetank_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
209 lines (186 loc) · 4.46 KB
/
Copy pathdemo.cpp
File metadata and controls
209 lines (186 loc) · 4.46 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Jetank/SCS_combi.hpp"
#include "Jetank/Motors.h"
const char* DEVICENAME = "/dev/ttyTHS1";
const int BAUDRATE = 1000000;
const int BEARINGS_MOTOR{1}; // -90 <-0-> 90
const int ARM_PITCH_MOTOR{2}; // -100 <-15-> 100
const int ARM_FLIP_MOTOR{3}; // -105 <-0-> 105
const int CLAW_MOTOR{4}; // -60 <-0-> 20
const int CAMERA_MOTOR{5}; // -5 <- -45-> -95
constexpr int BEAR_MAX{90};
constexpr int BEAR_MIN{-90};
constexpr int PITCH_MAX{100};
constexpr int PITCH_MIN{-100};
constexpr int FLIP_MAX{105};
constexpr int FLIP_MIN{-105};
constexpr int CLAW_MAX{20};
constexpr int CLAW_MIN{-60};
constexpr int CAMERA_MAX{-5};
constexpr int CAMERA_MIN{-95};
std::vector<int> ARM_MOTORS = {BEARINGS_MOTOR, ARM_PITCH_MOTOR, ARM_FLIP_MOTOR, CLAW_MOTOR, CAMERA_MOTOR};
std::vector<int> move_ranges(int min, int max, double steps=0.1)
{
std::vector<int> range;
int diff = std::abs((max - min));
double step = ((double)diff) * steps;
int stp = ((int)(1.0/steps));
for (int i = 0; i < stp; i++)
{
double st = min + (step * i);
range.push_back(st);
}
return range;
}
inline void move_sleep(void)
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(500ms);
}
int main()
{
// Gets port of device
using namespace std::chrono_literals;
int port = openPort(DEVICENAME,BAUDRATE);
if(port == -1)
{
std::cout << "Failed to open device " << DEVICENAME << std::endl;
return -1;
}
//Servo s = Servo(DEVICENAME, BAUDRATE);
//int p = s.getPort();
for(int i=1; i<6; i++)
{
setSpeed(port,i,600);
std::this_thread::sleep_for(200ms);
}
//std::this_thread::sleep_for(200ms);
/*for(int i=1; i<6; i++)
{
moveServo(port,i,-30);
std::this_thread::sleep_for(200ms);
}
*/
std::vector<int> bear_range = move_ranges(BEAR_MIN, BEAR_MAX);
std::vector<int> pitch_range = move_ranges(PITCH_MIN, PITCH_MAX);
std::vector<int> flip_range = move_ranges(FLIP_MIN, FLIP_MAX);
std::vector<int> claw_range = move_ranges(CLAW_MIN, CLAW_MAX);
std::vector<int> cam_range = move_ranges(CAMERA_MIN, CAMERA_MAX);
Motors m;
double speed = 0.5;
m.setSpeed(speed);
char ch;
int angl = 4; // 0 to 9, ie 10 steps
int motor_choise;
system("stty raw");
while (1)
{
ch = getchar();
system("clear");
printf("\rArm controls:\n");
printf("\rControl angle with with q (decrease) and e (increase).\n");
printf("\rApply angle to a servo with '1' to '5'.\n");
printf("\r(1): arm rotation, (2): arm pitch, (3): arm flip, (4): claw, (5): camara \n");
printf("\rServos online. \n\rCurrent angle: %i.\n", angl);
printf("\rStop motors by pressing 'z'. Exit program by pressing 'c'.\n\n");
printf("\rMotor controls:\n");
printf("\rSteer with w (forward), a (left), d (right), s (backwards).\n");
printf("\rIncrease or decrease speed by pressing up and down arrow keys.\n");
printf("\rStop motors by pressing 'z'. Exit program by pressing 'c'.\n\n");
printf("\r Motors online. \n\rCurrent speed: %3.1f.\n", speed);
if (ch == 'w')
{
m.moveForward();
}
else if (ch == 'd')
{
m.moveRight();
}
else if (ch == 'a')
{
m.moveLeft();
}
else if (ch == 's')
{
m.moveBackward();
}
else if (ch == 'z')
{
m.moveStop();
}
else if (ch == 'c')
{
m.moveStop();
system("stty cooked");
system("clear");
exit(1);
}
else if (ch == 49)
{
//printf("");
int angle = bear_range.at(angl);
moveServo(port,BEARINGS_MOTOR, angle);
move_sleep();
}
else if (ch == 50)
{
int angle = pitch_range.at(angl);
moveServo(port,ARM_PITCH_MOTOR, angle);
move_sleep();
}
else if (ch == 51)
{
int angle = flip_range.at(angl);
moveServo(port,ARM_FLIP_MOTOR, angle);
move_sleep();
}
else if (ch == 52)
{
int angle = claw_range.at(angl);
moveServo(port,CLAW_MOTOR, angle);
move_sleep();
}
else if (ch == 53)
{
int angle = cam_range.at(angl);
moveServo(port,CAMERA_MOTOR, angle);
move_sleep();
}
else if (ch == 'q')
{
if (angl > 0)
{
angl -= 1;
}
}
else if (ch == 'e')
{
if (angl < 9)
{
angl += 1;
}
}
else if (ch == 65)// arrow keys
{
if (speed < 1.0)
{
speed += 0.1;
m.setSpeed(speed);
}
}
else if (ch == 66)
{
if (speed > 0.0)
{
speed -= 0.1;
m.setSpeed(speed);
}
}
}
/*moveServo(port,CLAW_MOTOR, -30);
std::cout << "ARM_PITCH_MOTOR" << '\n';
std::this_thread::sleep_for(1000ms);
moveServo(port,CLAW_MOTOR, -20);*/
//moveAllServos(port,{0,0,0,0,0});
closePort(port);
return 0;
}