-
Notifications
You must be signed in to change notification settings - Fork 1
/
isimControl.cpp
169 lines (141 loc) · 3.51 KB
/
isimControl.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
#include "isimControl.h"
IsimControl::IsimControl(int id, QSerialPort* xbee){
this->id = id;
this->xbee = xbee;
this->ldxlValue = 512;
this->rdxlValue = 512;
this->lmagnetValue = 0;
this->rmagnetValue = 0;
this->lmotorValue = 0;
this->rmotorValue = 0;
}
IsimControl::~IsimControl(){
}
void IsimControl::sendInstruction(quint8 length, quint8 instruction, float* params){
QByteArray* instructionByteArray = new QByteArray();
int instIndex = 0;
/*Insert Start Index*/
for (int i = 0; i < 3; i++){
instructionByteArray->insert(instIndex++, 0x55);
}
/*Insert ID, Length, Instruction*/
instructionByteArray->insert(instIndex++,(char)(this->id) );
instructionByteArray->insert(instIndex++, length);
instructionByteArray->insert(instIndex++, instruction);
/*Insert Data*/
for (int i = 0; i < length; i++)
{
bool isPostive;
if (params[i] > 0){
isPostive = true;
}
else{
isPostive = false;
params[i] = 0 - params[i];
}
qint16 param_H ;
qint16 param_L ;
param_H = (qint16)(((int)(params[i])) % 10000);
param_L = (qint16)(((int)(params[i] * 10000.0)) % 10000);
if (isPostive){
}
else{
param_H = -(param_H);
param_L = -(param_L);
}
instructionByteArray->insert(instIndex++, ((char*)¶m_H) , 2);
instIndex++;
instructionByteArray->insert(instIndex++, ((char*)¶m_L) , 2);
instIndex++;
}
/*Insert CheckSum*/
instructionByteArray->insert(instIndex++,0xFE);
/*Send Data after checking port*/
if (xbee->isOpen()){
xbee->write(*instructionByteArray);
xbee->waitForBytesWritten(-1);
}
else{
}
delete(instructionByteArray);
}
void IsimControl::setWheelSpeed(float leftSpeed, float rightSpeed){
float wheelSpeed[2];
wheelSpeed[0] = leftSpeed;
wheelSpeed[1] = rightSpeed;
this->lmotorValue = leftSpeed;
this->rmotorValue = rightSpeed;
sendInstruction(0x02, 0x01, wheelSpeed);
return;
}
void IsimControl::setDxlPosition(float leftAngle, float rightAngle){
float dxlPosition[2];
dxlPosition[0] = leftAngle;
dxlPosition[1] = rightAngle;
this->ldxlValue = leftAngle;
this->rdxlValue = rightAngle;
sendInstruction(0x02, 0x04, dxlPosition);
return;
}
void IsimControl::setMagnetPower(float leftMagnet, float rightManget){
float magnetPower[2];
magnetPower[0] = leftMagnet;
magnetPower[1] = rightManget;
this->lmagnetValue = leftMagnet;
this->rmagnetValue = rightManget;
sendInstruction(0x02, 0x03, magnetPower);
return;
}
void IsimControl::setGyroscopeData(float* ypr){
this->yaw = ypr[0];
this->pitch = ypr[1];
this->roll = ypr[2];
}
void IsimControl::setYaw(float yaw){
this->yaw = yaw;
}
void IsimControl::updateGyroscopeData(){
float GYaddress = 1.0f;
sendInstruction(0x01, 0x06, &GYaddress);
}
void IsimControl::updateYaw(){
float GYaddress = 1.0f;
sendInstruction(0x01, 0x06, &GYaddress);
}
void IsimControl::updateSwitchPressed(){
float SWaddress = 2.0f;
sendInstruction(0x01, 0x06, &SWaddress);
}
int IsimControl::getId(){
return this->id;
}
float IsimControl::getYaw(){
return this->yaw;
}
float IsimControl::getRoll(){
return this->roll;
}
float IsimControl::getPitch(){
return this->pitch;
}
bool IsimControl::getSwitchPressed(){
return this->switchPressed;
}
float IsimControl::getRmotorValue(){
return this->rmotorValue;
}
float IsimControl::getLmotorValue(){
return this->lmotorValue;
}
float IsimControl::getRdxlValue(){
return this->rdxlValue;
}
float IsimControl::getLdxlValue(){
return this->ldxlValue;
}
float IsimControl::getRmagnetValue(){
return this->rmagnetValue;
}
float IsimControl::getLmagnetValue(){
return this->lmagnetValue;
}