-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotControl.cpp
More file actions
169 lines (141 loc) · 3.5 KB
/
RobotControl.cpp
File metadata and controls
169 lines (141 loc) · 3.5 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 "RobotControl.h"
#include <stdlib.h>
RobotControl::RobotControl(int robId) : RobotObject(robId) , _defaultSpeed(0.5), _speedMultiplier(0.01) {
// TODO Auto-generated constructor stub
_RobSocket = -1;
_motorLeftSpeed = _defaultSpeed;
_motorRightSpeed = _defaultSpeed;
_currentTargetPoint = 0;
}
RobotControl::~RobotControl() {
// TODO Auto-generated destructor stub
}
void RobotControl::setMotorLeft(float left) {
char leftCmd = 1;
sendMotorCmd(leftCmd,left);
}
void RobotControl::setMotorRight(float right) {
char rightCmd = 2;
sendMotorCmd(rightCmd,right);
}
void RobotControl::setMotors(float left, float right) {
setMotorLeft(left);
setMotorRight(right);
}
void RobotControl::setMotors(){
setMotorLeft(_motorLeftSpeed);
setMotorRight(_motorRightSpeed);
}
Vect2D RobotControl::getCurrentTargetPoint() {
return _tagetPoints.at(_currentTargetPoint);
}
void RobotControl::setNextTargetPoint(){
if(_tagetPoints.size() >= _currentTargetPoint + 1){
_currentTargetPoint = 0;
} else {
_currentTargetPoint++;
}
}
bool RobotControl::OpenSocket(char *aMac) {
struct sockaddr_rc addr = { 0 };
int status;
// allocate a socket
_RobSocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (_RobSocket < 0) {
perror("no Socket");
return false;
} else {
printf("RobS:%d\n", _RobSocket);
}
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba(aMac, &addr.rc_bdaddr);
// connect to server
status = connect(_RobSocket, (struct sockaddr *) &addr, sizeof(addr));
if (status < 0) {
perror("uh oh");
return false;
} else {
printf("status:%d\n", status);
return true;
}
}
void RobotControl::CloseSocket() {
if (_RobSocket >= 0) {
close(_RobSocket);
}
}
void RobotControl::sendCMD(void *cmd, size_t size) {
if (_RobSocket >= 0) {
write(_RobSocket, &cmd, size);
}
}
void RobotControl::controlRobot(){
switch (controlCmd) {
case FORWARD:
if(_tagetPoints.empty()){
setMotors();
} else {
folowPoints();
}
break;
case TURNCLOCKWISE:
turnRobot(true);
break;
case TURNCOUNTERCLOCKWISE:
turnRobot(false);
break;
default:
stopRobot();
break;
}
}
void RobotControl::sendMotorCmd(char motor,float speed){
if (speed > 1.0) {
speed = 1;
} else if (speed < -1.0) {
speed = -1;
}
//printf("ROB%d: motor:%d,speed:%f\n",GetRobId(),motor,speed);
std::lock_guard<std::mutex> lock(sendCmdMurtex);
this->sendCMD(&motor, sizeof(char));
this->sendCMD(&speed, sizeof(float));
}
void RobotControl::stopRobot(){
setMotors(0,0);
}
void RobotControl::turnRobot(bool clockwise){
if (clockwise) {
setMotors(_defaultSpeed,-_defaultSpeed);
} else {
setMotors(-_defaultSpeed,_defaultSpeed);
}
}
void RobotControl::folowPoints(){
Vect2D tagetPoint = getCurrentTargetPoint();
Vect2D currentPos = this->GetDirVect();
double dist = tagetPoint.DistBetweenPoints(currentPos);
if(dist < _tagetPointTreshold){
setNextTargetPoint();
return;
}
double angle = currentPos.AngleBetweenVect_Grad(tagetPoint);
if(std::abs(angle) > 90){
this->StartRotationBy(angle,(bool)(angle > 0));
}
else if(angle < 0){
_motorRightSpeed = -angle * _speedMultiplier + _defaultSpeed;
} else {
_motorLeftSpeed = angle * _speedMultiplier + _defaultSpeed;
}
}
void RobotControl::keepAlive(){
std::lock_guard<std::mutex> lock(sendCmdMurtex);
//printf("ROB%d: KEEP_ALIVE\n",GetRobId());
char c = 0;
sendCMD(&c,sizeof(char));
}
void RobotControl::setTargetPoints(std::vector<Vect2D> points){
_tagetPoints = points;
}