-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobo_Gun.cpp
80 lines (73 loc) · 1.74 KB
/
Robo_Gun.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
#include "Robo_Gun.h"
#include <thread>
#include <iostream>
Robo_Gun::Robo_Gun() :
_servo_X(Servo(SERVO_X)),
_servo_Y(Servo(SERVO_Y)),
_servo_TRIGGER(Servo(SERVO_TRIGGER)),
_firing(0)
{
absolute_move(500, 500);
}
bool Robo_Gun::fire()
{
if(_firing)
{
return false;
}
else
{
std::thread th_fire(&Robo_Gun::fire_process, this);
th_fire.detach();
_firing = true;
return true;
}
}
void Robo_Gun::relative_move(int x, int y)
{
_x = _x + x;
_y = _y + y;
if(_x > 2500) _x = 2500;
if(_x < 500) _x = 500;
if(_y > 2500) _y = 2500;
if(_y < 500) _y = 500;
_servo_X.set_value(_x);
_servo_Y.set_value(_y);
}
void Robo_Gun::absolute_move(int x, int y)
{
_x = x;
_y = y;
if(_x > 2500) _x = 2500;
if(_x < 500) _x = 500;
if(_y > 2500) _y = 2500;
if(_y < 500) _y = 500;
_servo_X.set_value(_x);
_servo_Y.set_value(_y);
}
void Robo_Gun::auto_move(double x_change, double absolute_y)
{
//std::cout << "Current angle: " << _x;
_x = _x + x_change*2000/180;
//std::cout << " New angle: " << _x << std::endl;
if(_x > 2500) _x = 2500;
if(_x < 500) _x = 500;
_y = absolute_y*5.556 + 1000;
//std::cout << "Current angle: " << _y << std::endl;
if(_y > 2500) _y = 2500;
if(_y < 500) _y = 500;
_servo_X.set_value(_x);
_servo_Y.set_value(_y);
}
void Robo_Gun::fire_process(Robo_Gun *ptr)
{
ptr->_servo_TRIGGER.set_value(1000);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
ptr->_servo_TRIGGER.set_value(2000);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
ptr->_firing = false;
}
int Robo_Gun::degree2servo(double degree)
{
return int(degree*(2000/90) + 1500);
}