Skip to content

Commit 0fe6ca5

Browse files
author
leixiaowei
committed
add getEncoders
1 parent ba8f3ea commit 0fe6ca5

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

Arduino/MycobotBasic/CommunicateDefine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
#define SET_ENCODERS 0x3C
113113
#define SET_ENCODERS_LEN 15
114114

115+
#define GET_ENCODERS 0x3D
116+
#define GET_ENCODERS_LEN 2
117+
#define GET_ENCODES_RETURN_LEN 14
118+
119+
115120

116121
// Running Status and Settings
117122
#define GET_SPEED 0x40

Arduino/MycobotBasic/MycobotBasic.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ void* MycobotBasic::readData()
298298
*pEncoder = encoder_low + encoder_high * 256;
299299
return pEncoder;
300300
}
301-
302301
case GET_GRIPPER_VALUE:
303302
{
304303
int* returnValue = new int;
@@ -390,7 +389,35 @@ void* MycobotBasic::readData()
390389

391390
return pCoords;
392391
}
392+
case GET_ENCODERS:
393+
{
394+
byte encode_1_high = r_data_14[1];
395+
byte encode_1_low = r_data_14[2];
396+
397+
byte encode_2_high = r_data_14[3];
398+
byte encode_2_low = r_data_14[4];
399+
400+
byte encode_3_high = r_data_14[5];
401+
byte encode_3_low = r_data_14[6];
393402

403+
byte encode_4_high = r_data_14[7];
404+
byte encode_4_low = r_data_14[8];
405+
406+
byte encode_5_high = r_data_14[9];
407+
byte encode_5_low = r_data_14[10];
408+
409+
byte encode_6_high = r_data_14[11];
410+
byte encode_6_low = r_data_14[12];
411+
412+
Angles* pEncoders = new Angles;
413+
pEncoders->at(0) = encode_1_low + encode_1_high * 256;
414+
pEncoders->at(1) = encode_2_low + encode_2_high * 256;
415+
pEncoders->at(2) = encode_3_low + encode_3_high * 256;
416+
pEncoders->at(3) = encode_4_low + encode_4_high * 256;
417+
pEncoders->at(4) = encode_5_low + encode_5_high * 256;
418+
pEncoders->at(5) = encode_6_low + encode_6_high * 256;
419+
return pEncoders;
420+
}
394421
case GET_TOOL_REF:
395422
{
396423
byte x_high = r_data_14[1];
@@ -872,6 +899,38 @@ int MycobotBasic::getEncoder(int joint)
872899
return -1;
873900
}
874901

902+
Angles MycobotBasic::getEncoders()
903+
{
904+
Serial2.write(header);
905+
Serial2.write(header);
906+
Serial2.write(GET_ENCODERS_LEN);
907+
Serial2.write(GET_ENCODERS);
908+
Serial2.write(footer);
909+
910+
unsigned long t_begin = millis();
911+
void* tempPtr = nullptr;
912+
Angles* pEncoders = nullptr;
913+
Angles encoders;
914+
915+
while (true)
916+
{
917+
if (millis() - t_begin > 40)
918+
break;
919+
tempPtr = readData();
920+
if (tempPtr == nullptr)
921+
continue;
922+
else
923+
{
924+
pEncoders = (Angles*)tempPtr;
925+
for (int i = 0; i < 6; ++i)
926+
encoders[i] = pEncoders->at(i);
927+
delete pEncoders;
928+
return encoders;
929+
}
930+
}
931+
return error_encoders;
932+
}
933+
875934
void MycobotBasic::setEncoders(Angles angleEncoders, int speed)
876935
{
877936
byte angle_1_high = highByte(static_cast<int>(angleEncoders[0]));

Arduino/MycobotBasic/MycobotBasic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ class MycobotBasic
6767
void jogAngle(int joint, int direction, int speed);
6868
void jogCoord(Axis axis, int direction, int speed);
6969
void jogStop();
70+
71+
//Encoder mode and operation
7072
void setEncoder(int joint, int encoder);
7173
int getEncoder(int joint);
7274
void setEncoders(Angles angleEncoders, int speed);
75+
Angles getEncoders();
7376

7477

7578

@@ -156,6 +159,7 @@ class MycobotBasic
156159

157160
Angles error_angles;
158161
Coords error_coords;
162+
Angles error_encoders;
159163

160164
std::map<int, std::string> messages_map;
161165
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <MycobotBasic.h>
2+
#include <ParameterList.h>
3+
4+
MycobotBasic myCobot;
5+
6+
Encoders encoders;
7+
8+
void setup() {
9+
// 打开Atom链接
10+
myCobot.setup();
11+
myCobot.powerOn();
12+
Serial.begin(9600);
13+
}
14+
void loop() {
15+
encoders = myCobot.getEncoders();
16+
for(auto encoder : encoders) {
17+
Serial.print(encoder);
18+
Serial.print(" ");;
19+
}
20+
Serial.println();
21+
}

0 commit comments

Comments
 (0)