-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (192 loc) · 9.61 KB
/
main.cpp
File metadata and controls
224 lines (192 loc) · 9.61 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <QCoreApplication>
#include <QSerialPort>
#include <iostream>
#include "liderhand.h"
typedef enum
{
IDLE,
INTERNAL,
EXTERNAL
}Mode_Type;
Mode_Type Mode;
QSerialPort serial;
LiderHand LiderHandObj;
void usage()
{
std::cout << "\tLiderHandCommTest <serialport name> <mode>" << std::endl;
std::cout << "\t\t<serialport name> - name of the serial port eg. COM3 or /dev/tty0" << std::endl;
std::cout << "\t\t<mode> - mode of operation, can be value of: IDLE | INTERNAL | EXTERNAL" << std::endl;
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
serial.setBaudRate(460800, QSerialPort::AllDirections);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if(argc != 3)
{
std::cout << "Wrong argument count" << std::endl << std::endl;
usage();
return 0;
}else
{
if(strcmp(argv[2], "IDLE") == 0)
{
Mode = IDLE;
}else if(strcmp(argv[2], "INTERNAL") == 0)
{
Mode = INTERNAL;
}else if(strcmp(argv[2], "EXTERNAL") == 0)
{
Mode = EXTERNAL;
}else
{
std::cout << "Wrong <mode> argument value" << std::endl << std::endl;
usage();
return 0;
}
serial.setPortName(argv[1]);
}
if(serial.open(QIODevice::ReadWrite))//open serial port
{
std::cout << "Serial opened" << std::endl;
//------EXAMPLE-------------------------------------------------------------------//
//------FUNCTIONAL COMMANDS EXAMPLES----------------------------------------------//
//------ENABLES LiderHand TO SEND STATUS UPDATED (100 Hz)-------------------------//
std::string EnableStatusPayload = LiderHandObj.PrepareDataEnableStatusUpdate();
serial.write(EnableStatusPayload.c_str(), EnableStatusPayload.length());
serial.waitForBytesWritten(-1);
//------DISABLES LiderHand TO SEND STATUS UPDATED---------------------------------//
//std::string DisableStatusPayload = LiderHandObj.PrepareDataDisableStatusUpdate();
//serial.write(DisableStatusPayload.c_str(), DisableStatusPayload.length());
//serial.waitForBytesWritten(-1);
//------LiderHand PERFORMS CALIBRATION OF ALL DRIVES------------------------------//
//std::string CalibrationPayload = LiderHandObj.PrepareDataPerformCalibration();
//serial.write(CalibrationPayload.c_str(), CalibrationPayload.length());
//serial.waitForBytesWritten(-1);
//------RESETS ALL LiderHand INTERNAL ERRORS, INCLUDING CurrentError STATUS AND---//
//------Faul OPERATION OF ALL DRIVES - UNLOCKS FAULTY DRIVES----------------------//
//std::string ResetErrPayload = LiderHandObj.PrepareDataResetErrors();
//serial.write(ResetErrPayload.c_str(), ResetErrPayload.length());
//serial.waitForBytesWritten(-1);
while(1)
{
//------EXAMPLE-------------------------------------------------------------------//
//------WAIT FOR NEW INCOMING DATA FROM LiderHand AND HANDLE THE DATA-------------//
//------ALL DATA IS READ NO MATTER OF THE OPERATION MODE, YOU GET A FULL VECTOR---//
//------FOR LiderHand TO START SENDING DATA, SEND THE EnableStatus COMMAND--------//
serial.waitForReadyRead(-1);//wait for new data - blocking
QByteArray data = serial.readLine();//read line (data is '\n' terminated)
if(LiderHandObj.ParseDataFromLiderHand(data.toStdString()) == LiderHand::SUCCESS) //parse the input data and fill all the class content with current LiderHand data
{
uint8_t count = LiderHandObj.GetMotorDriverCount(); //acces driver count
std::cout << "Read SUCCESS Drv count = " << (int)count << std::endl;
LiderHand::SystemOperationMode_Type mode = LiderHandObj.GetSystemOperationMode(); //acces mode etc.
LiderHand::CalibrationProcedure_Type calib = LiderHandObj.GetCalibrationProcedure();
LiderHand::CurrentError_Type error = LiderHandObj.GetCurrentError();
if(error != LiderHand::ERROR_OK)//system error handling (also indicated by LED blinking)
{
if(error & LiderHand::ERROR_RS485_TIMEOUT)
{
std::cout << "LiderHand RS485 TIMEOUT ERROR" << std::endl;
}
if(error & LiderHand::ERROR_RS485_CRC)
{
std::cout << "LiderHand RS485 CRC ERROR" << std::endl;
}
if(error & LiderHand::ERROR_MOTOR_FAULT)
{
std::cout << "LiderHand MOTOR ERROR" << std::endl;
}
if(error & LiderHand::ERROR_FT232_CRC)
{
std::cout << "LiderHand PC CRC ERROR" << std::endl;
}
}
for(int i=0; i<count; i++)
{
LiderHand::Direction_Type dir = LiderHandObj.GetDirection(i); //acces driver specyfic data
LiderHand::FreeDrive_Type fd = LiderHandObj.GetFreeDrive(i);
LiderHand::MotorDriverOperation_Type op = LiderHandObj.GetMotorDriverOperation(i);
uint16_t pwm = LiderHandObj.GetPWM(i);
uint16_t cur = LiderHandObj.GetCurrent(i);
uint8_t PositionCurrent_Count = LiderHandObj.GetPositonCurrent_Count(i);
uint16_t PositionCurrent[PositionCurrent_Count];
for(int p=0; p<PositionCurrent_Count; p++)
{
PositionCurrent[p] = LiderHandObj.GetPositonCurrent(i, p);
}
uint16_t posSet = LiderHandObj.GetPositonSet(i);
if(op == LiderHand::Operation_Fault) //handle particula driver error
{
std::cout << "Motor " << i << " faulty" << std::endl;
}
}
}else
{
std::cout << "Read ERROR" << std::endl;
}
//------EXAMPLE-------------------------------------------------------------------//
//------IDLE - LiderHand DOES NOT PERFORM ANY ACTION, ALL DRIVES BREAK------------//
//------YOU CAN CHOOSE IF EACH DRIVE IS IN FREEDRIVE MODE-------------------------//
if(Mode == IDLE)
{
uint8_t DrvCount = LiderHandObj.GetMotorDriverCount();
for(int i=0; i<DrvCount; i++)
{
LiderHandObj.SetFreeDrive(i, LiderHand::FreeDrive_DIS); //is FreeDrive mode enabled
}
std::string IdlePayload = LiderHandObj.PrepareDataIdleMode();
serial.write(IdlePayload.c_str(), IdlePayload.length());
serial.waitForBytesWritten(-1);
}
//------EXAMPLE-------------------------------------------------------------------//
//------INTERNAL - INTERNAL LiderHand REGULATOR IS USED---------------------------//
//------ONLY A POSITION TO BE OBTAINED FOR EACH DRIVE IS SEND---------------------//
//------LiderHand USES INTERNAL SIMPLE POSITION REGULATOR-------------------------//
if(Mode == INTERNAL)
{
uint8_t DrvCount = LiderHandObj.GetMotorDriverCount();
for(int i=0; i<DrvCount; i++)
{
LiderHandObj.SetPosition(i, 30000); //set position of each drive here, range 1-65535
}
std::string InternalPayload = LiderHandObj.PrepareDataInternalRegMode();
serial.write(InternalPayload.c_str(), InternalPayload.length());
serial.waitForBytesWritten(-1);
}
//------EXAMPLE-------------------------------------------------------------------//
//------EXTERNAL - USER SETS PWM OF EACH DRIVE, REGULATOR HAS TO BE IMPLEMENTED---//
//------YOU HAVE TO USE SENSOR DATA OF EACH DRIVE AND CALCULATE THE PWM OF EACH---//
//------DRIVE, USER HAS FULL CONTROL OVER ALL MOTOR-------------------------------//
if(Mode == EXTERNAL)
{
uint8_t DrvCount = LiderHandObj.GetMotorDriverCount();
for(int i=0; i<DrvCount; i++)
{
uint16_t Current = LiderHandObj.GetCurrent(i);
uint8_t PositionCurrent_Count = LiderHandObj.GetPositonCurrent_Count(i);
uint16_t PositionCurrent[PositionCurrent_Count];
for(int p=0; p<PositionCurrent_Count; p++)
{
PositionCurrent[p] = LiderHandObj.GetPositonCurrent(i, p);
}
//----IMPLEMENT YOUR REGULATOR HERE---//
LiderHandObj.SetPWM(i, 0); //PWM value, range 1-65535
LiderHandObj.SetFreeDrive(i, LiderHand::FreeDrive_DIS); //is FreeDrive mode enabled
LiderHandObj.SetDirection(i, LiderHand::Dir_Positive);
}
std::string ExternalPayload = LiderHandObj.PrepareDataExternalRegMode();
serial.write(ExternalPayload.c_str(), ExternalPayload.length());
serial.waitForBytesWritten(-1);
}
}
}else
{
std::cout << "Serial open fail" << std::endl;
}
return a.exec();
}