-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand.cpp
171 lines (148 loc) · 2.93 KB
/
command.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
#include "command.h"
static int mf = 1;
static int mb = 1;
static int tr = 1;
static int tl = 1;
static int moveType = 1;
boost::mutex mu;
CCommand::CCommand()
{
serialPort.Open(5,115200);
}
CCommand::~CCommand()
{
}
void CCommand::moveContorl(int sumX, int sumY, int lastSeen)
{
if(sumX == 0 || sumY == 0)
{
stop();
}
mu.lock();
cout<<sumY<<endl;
if(moveType == 2){
if(sumY>420 && lastSeen==0) //ºóÍË
{
moveFoward();
}
else if(sumY<400 && lastSeen==0)//Ç°½ø
{
moveBackwoard();
}
else
{
stop();
}
}
mu.unlock();
}
void CCommand::rationControl(int sumX, int sumY, int lastSeen)
{
mu.lock();
if(moveType == 1){
double angle = atan((double)abs(sumX - 320)/(640-sumY))*180/3.14;
if(angle>5 && (sumX-320)>0 && lastSeen==0)
{
turnLeft();
}
else if(angle>5 && (sumX-320)<0 && lastSeen==0)
{
turnRight();
}
else
{
stop();
}
}
mu.unlock();
}
void CCommand::moveFoward()
{
if(mf == 1)
{
cout<<"forward"<<endl;
//char cmd[9] = {0xaa, 0x55, 0x01, 0x00, 0x00, 0x02, 0x04, 0x6c, 0x20};
//serialPort.SendData(cmd, 9);
char cmd[11] = {0xAA, 0x55, 0x01 ,0x00, 0x02, 0x02, 0x05, 0x0B, 0xFF, 0x9D, 0x76};
serialPort.SendData(cmd, 11);
Sleep(50);
mb = 1;
}
mf++;
}
void CCommand::moveBackwoard()
{
if(mb == 1)
{ cout<<"backward"<<endl;
mf = 1;
//char cmd[9] = {0xaa, 0x55, 0x01, 0x00, 0x00, 0x02, 0x06, 0xad, 0xa1};
//serialPort.SendData(cmd, 9);
char cmd[11] = {0xAA, 0x55, 0x01, 0x00, 0x02, 0x02, 0x07, 0x10, 0xFF, 0xAD,0xDD};
serialPort.SendData(cmd, 11);
Sleep(50);
}
mb++;
}
void CCommand::turnLeft()
{
if(tl == 1)
{
tr = 1;
//char cmd[9] = {0xaa, 0x55, 0x01, 0x00, 0x00, 0x02, 0x00, 0xaf, 0x21};
char cmd[11] = {0xAA ,0x55, 0x01, 0x00, 0x02, 0x02, 0x01, 0x01, 0x30, 0xa8, 0x71};
serialPort.SendData(cmd, 11);
}
tl++;
}
//AA 55 01 00 02 02 02 00 50 10 80
void CCommand::turnRight()
{
if(tr == 1)
{
cout<<"right"<<endl;
tl = 1;
char cmd[11] = {0xAA, 0x55, 0x01, 0x00, 0x02, 0x02, 0x02, 0x00, 0x10, 0xe0,0x81};
//char cmd[9] = {0xaa, 0x55, 0x01, 0x00, 0x00, 0x02, 0x02, 0x6e, 0xa0};
serialPort.SendData(cmd,11);
}
tr++;
}
void CCommand::stop()
{
tr = 1;
tl = 1;
mf = 1;
mb = 1;
char cmd[9] = {0xAA, 0x55, 0x01, 0x00, 0x00, 0x02, 0x08, 0x69, 0x20};
serialPort.SendData(cmd, 9);
if(moveType == 1) moveType = 2;
else
moveType = 1;
}
//void CCommand::crc16(unsigned char *ptr, unsigned int len)
//{
// unsigned long wcrc = 0XFFFF;
// unsigned char temp;
//
// for(int i=0; i<len; i++)
// {
// temp = (*ptr) & 0X00FF;
// ptr++;
// wcrc ^= temp;
// for(int j=0; j<8; j++)
// {
// if(wcrc & 0X0001)
// {
// wcrc >>= 1;
// wcrc ^= 0XA001;
// }
// else
// {
// wcrc >>= 1;
// }
// }
// }
// //temp=wcrc;
// CRC[0] = wcrc;
// CRC[1] = wcrc >> 8;
//}