-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMSBL.cpp
221 lines (193 loc) · 3.63 KB
/
SMSBL.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* SMSBL.cpp
* 飞特SMSBL系列串行舵机应用层程序
* 日期: 2017.11.21
* 作者: 谭雄乐
*/
#include "SMSBL.h"
SMSBL::SMSBL()
{
End = 0;
}
SMSBL::SMSBL(u8 End):SCSerail(End)
{
}
SMSBL::SMSBL(u8 End, u8 Level):SCSerail(End, Level)
{
}
int SMSBL::EnableTorque(u8 ID, u8 Enable)
{
return writeByte(ID, SMSBL_TORQUE_ENABLE, Enable);
}
int SMSBL::writePos(u8 ID, s16 Position, u16 Speed, u8 ACC, u8 Fun)
{
if(Position<0){
Position = -Position;
Position |= (1<<15);
}
flushSCS();
u8 buf[7];
buf[0] = ACC;
Host2SCS(buf+1, buf+2, Position);
Host2SCS(buf+3, buf+4, 0);
Host2SCS(buf+5, buf+6, Speed);
writeBuf(ID, SMSBL_ACC, buf, 7, Fun);
return Ack(ID);
}
//写位置指令
//舵机ID,Position位置,加速度ACC,速度Speed
int SMSBL::WritePos(u8 ID, s16 Position, u16 Speed, u8 ACC)
{
return writePos(ID, Position, Speed, ACC, INST_WRITE);
}
//异步写位置指令
//舵机ID,Position位置,加速度ACC,速度Speed
int SMSBL::RegWritePos(u8 ID, s16 Position, u16 Speed, u8 ACC)
{
return writePos(ID, Position, Speed, ACC, INST_REG_WRITE);
}
void SMSBL::RegWriteAction()
{
writeBuf(0xfe, 0, NULL, 0, INST_ACTION);
}
//写位置指令
//舵机ID[]数组,IDN数组长度,Position位置,ACC加速度,速度Speed
void SMSBL::SyncWritePos(u8 ID[], u8 IDN, s16 Position, u16 Speed, u8 ACC)
{
if(Position<0){
Position = -Position;
Position |= (1<<15);
}
u8 buf[7];
buf[0] = ACC;
Host2SCS(buf+1, buf+2, Position);
Host2SCS(buf+3, buf+4, 0);
Host2SCS(buf+5, buf+6, Speed);
snycWrite(ID, IDN, SMSBL_ACC, buf, 7);
}
//读位置,超时Err=1
s16 SMSBL::ReadPos(u8 ID, u8 *Err)
{
s16 curPos = readWord(ID, SMSBL_PRESENT_POSITION_L);
if(curPos==-1){
if(Err){
*Err = 1;
}
return -1;
}
if(curPos&(1<<15)){
curPos = -(curPos&~(1<<15));
}
if(Err){
*Err = 0;
}
return curPos;
}
//读速度,超时Err=1
s16 SMSBL::ReadSpe(u8 ID, u8 *Err)
{
s16 curSpe = readWord(ID, SMSBL_PRESENT_SPEED_L);
if(curSpe==-1){
if(Err){
*Err = 1;
}
return -1;
}
if(curSpe&(1<<15)){
curSpe = -(curSpe&~(1<<15));
}
if(Err){
*Err = 0;
}
return curSpe;
}
//速度控制模式
int SMSBL::WriteSpe(u8 ID, s16 Speed, u8 ACC)
{
if(Speed<0){
Speed = -Speed;
Speed |= (1<<15);
}
int res = writeByte(ID, SMSBL_ACC, ACC);
if(res==-1){
return -1;
}
return writeWord(ID, SMSBL_GOAL_SPEED_L, Speed);
}
//PWM输出模式
int SMSBL::WritePWM(u8 ID, s16 pwmOut)
{
if(pwmOut<0){
pwmOut = -pwmOut;
pwmOut |= (1<<15);
}
return writeWord(ID, SMSBL_GOAL_SPEED_L, pwmOut);
}
//读负载,超时返回-1
int SMSBL::ReadLoad(u8 ID)
{
return readWord(ID, SMSBL_PRESENT_LOAD_L);
}
//读当前电流
int SMSBL::ReadCurrent(u8 ID)
{
return readWord(ID, SMSBL_PRESENT_CURRENT_L);
}
//读电压,超时返回-1
int SMSBL::ReadVoltage(u8 ID)
{
return readByte(ID, SMSBL_PRESENT_VOLTAGE);
}
//读温度,超时返回-1
int SMSBL::ReadTemper(u8 ID)
{
return readByte(ID, SMSBL_PRESENT_TEMPERATURE);
}
//Ping指令,返回舵机ID,超时返回-1
int SMSBL::Ping(u8 ID)
{
flushSCS();
u8 bBuf[6];
writeBuf(ID, 0, NULL, 0, INST_PING);
int Size = readSCS(bBuf, 6);
if(Size==6){
return bBuf[2];
}else{
return -1;
}
}
int SMSBL::wheelMode(u8 ID)
{
return writeByte(ID, SMSBL_MODE, 1);
}
int SMSBL::pwmMode(u8 ID)
{
return writeByte(ID, SMSBL_MODE, 2);
}
int SMSBL::joinMode(u8 ID)
{
return writeByte(ID, SMSBL_MODE, 0);
}
//复位舵机参数为默认值
int SMSBL::Reset(u8 ID)
{
flushSCS();
writeBuf(ID, 0, NULL, 0, INST_RESET);
return Ack(ID);
}
int SMSBL::WriteOfs(u8 ID, s16 Ofs)
{
if(Ofs<0){
Ofs = -Ofs;
Ofs |= (1<<15);
}
return writeWord(ID, SMSBL_OFS_L, Ofs);
}
int SMSBL::unLockEprom(u8 ID)
{
return writeByte(ID, SMSBL_LOCK, 0);//打开EPROM保存功能
}
int SMSBL::LockEprom(u8 ID)
{
return writeByte(ID, SMSBL_LOCK, 1);//关闭EPROM保存功能
}