-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCS.cpp
204 lines (186 loc) · 3.35 KB
/
SCS.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
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
/*
* SCS.cpp
* 飞特串行舵机通信层协议程序
* 日期: 2017.8.22
* 作者: 谭雄乐
*/
#include <stddef.h>
#include "SCS.h"
SCS::SCS()
{
Level = 1;//除广播指令所有指令返回应答
//End = 1;//舵机处理器与控制板处理器端结构不一致
}
SCS::SCS(u8 End)
{
Level = 1;
this->End = End;
}
SCS::SCS(u8 End, u8 Level)
{
this->Level = Level;
this->End = End;
}
//1个16位数拆分为2个8位数
//DataL为低位,DataH为高位
void SCS::Host2SCS(u8 *DataL, u8* DataH, u16 Data)
{
if(End){
*DataL = (Data>>8);
*DataH = (Data&0xff);
}else{
*DataH = (Data>>8);
*DataL = (Data&0xff);
}
}
//2个8位数组合为1个16位数
//DataL为低位,DataH为高位
u16 SCS::SCS2Host(u8 DataL, u8 DataH)
{
u16 Data;
if(End){
Data = DataL;
Data<<=8;
Data |= DataH;
}else{
Data = DataH;
Data<<=8;
Data |= DataL;
}
return Data;
}
void SCS::writeBuf(u8 ID, u8 MemAddr, u8 *nDat, u8 nLen, u8 Fun)
{
u8 msgLen = 2;
u8 bBuf[6];
u8 CheckSum = 0;
bBuf[0] = 0xff;
bBuf[1] = 0xff;
bBuf[2] = ID;
bBuf[4] = Fun;
if(nDat){
msgLen += nLen + 1;
bBuf[3] = msgLen;
bBuf[5] = MemAddr;
writeSCS(bBuf, 6);
}else{
bBuf[3] = msgLen;
writeSCS(bBuf, 5);
}
CheckSum = ID + msgLen + Fun + MemAddr;
u8 i = 0;
if(nDat){
for(i=0; i<nLen; i++){
CheckSum += nDat[i];
}
writeSCS(nDat, nLen);
}
writeSCS(~CheckSum);
}
//普通写指令
//舵机ID,MemAddr内存表地址,写入数据,写入长度
int SCS::genWrite(u8 ID, u8 MemAddr, u8 *nDat, u8 nLen)
{
flushSCS();
writeBuf(ID, MemAddr, nDat, nLen, INST_WRITE);
return Ack(ID);
}
//异步写指令
//舵机ID,MemAddr内存表地址,写入数据,写入长度
int SCS::regWrite(u8 ID, u8 MemAddr, u8 *nDat, u8 nLen)
{
flushSCS();
writeBuf(ID, MemAddr, nDat, nLen, INST_REG_WRITE);
return Ack(ID);
}
//同步写指令
//舵机ID[]数组,IDN数组长度,MemAddr内存表地址,写入数据,写入长度
void SCS::snycWrite(u8 ID[], u8 IDN, u8 MemAddr, u8 *nDat, u8 nLen)
{
u8 mesLen = ((nLen+1)*IDN+4);
u8 Sum = 0;
u8 bBuf[7];
bBuf[0] = 0xff;
bBuf[1] = 0xff;
bBuf[2] = 0xfe;
bBuf[3] = mesLen;
bBuf[4] = INST_SYNC_WRITE;
bBuf[5] = MemAddr;
bBuf[6] = nLen;
writeSCS(bBuf, 7);
Sum = 0xfe + mesLen + INST_SYNC_WRITE + MemAddr + nLen;
u8 i, j;
for(i=0; i<IDN; i++){
writeSCS(ID[i]);
writeSCS(nDat, nLen);
Sum += ID[i];
for(j=0; j<nLen; j++){
Sum += nDat[j];
}
}
writeSCS(~Sum);
}
int SCS::writeByte(u8 ID, u8 MemAddr, u8 bDat)
{
flushSCS();
writeBuf(ID, MemAddr, &bDat, 1, INST_WRITE);
return Ack(ID);
}
int SCS::writeWord(u8 ID, u8 MemAddr, u16 wDat)
{
flushSCS();
u8 buf[2];
Host2SCS(buf+0, buf+1, wDat);
writeBuf(ID, MemAddr, buf, 2, INST_WRITE);
return Ack(ID);
}
//读指令
//舵机ID,MemAddr内存表地址,返回数据nData,数据长度nLen
int SCS::Read(u8 ID, u8 MemAddr, u8 *nData, u8 nLen)
{
flushSCS();
writeBuf(ID, MemAddr, &nLen, 1, INST_READ);
u8 bBuf[5];
if(readSCS(bBuf, 5)!=5){
return 0;
}
int Size = readSCS(nData, nLen);
if(readSCS(bBuf, 1)){
return Size;
}
return 0;
}
//读1字节,超时返回-1
int SCS::readByte(u8 ID, u8 MemAddr)
{
u8 bDat;
int Size = Read(ID, MemAddr, &bDat, 1);
if(Size!=1){
return -1;
}else{
return bDat;
}
}
//读2字节,超时返回-1
int SCS::readWord(u8 ID, u8 MemAddr)
{
u8 nDat[2];
int Size;
u16 wDat;
Size = Read(ID, MemAddr, nDat, 2);
if(Size!=2)
return -1;
wDat = SCS2Host(nDat[0], nDat[1]);
return wDat;
}
int SCS::Ack(u8 ID)
{
if(ID != 0xfe && Level){
u8 buf[6];
u8 Size = readSCS(buf, 6);
if(Size!=6){
return 0;
}
}
return 1;
}