-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMSCL.cpp
191 lines (164 loc) · 3.23 KB
/
SMSCL.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
/*
* SMSCL.cpp
* 飞特SMSCL系列串行舵机应用层程序
* 日期: 2017.11.21
* 作者: 谭雄乐
*/
#include "SCServo.h"
SMSCL::SMSCL()
{
End = 0;
}
SMSCL::SMSCL(u8 End):SCSerail(End)
{
}
SMSCL::SMSCL(u8 End, u8 Level):SCSerail(End, Level)
{
}
int SMSCL::EnableTorque(u8 ID, u8 Enable)
{
return writeByte(ID, SMSCL_TORQUE_ENABLE, Enable);
}
int SMSCL::writePos(u8 ID, s16 Position, u16 Time, u16 Speed, u8 Fun)
{
if(Position<0){
Position = -Position;
Position |= (1<<15);
}
flushSCS();
u8 buf[6];
Host2SCS(buf+0, buf+1, Position);
Host2SCS(buf+2, buf+3, Time);
Host2SCS(buf+4, buf+5, Speed);
writeBuf(ID, SMSCL_GOAL_POSITION_L, buf, 6, Fun);
return Ack(ID);
}
//写位置指令
//舵机ID,Position位置,执行时间Time,执行速度Speed
int SMSCL::WritePos(u8 ID, s16 Position, u16 Time, u16 Speed)
{
return writePos(ID, Position, Time, Speed, INST_WRITE);
}
//异步写位置指令
//舵机ID,Position位置,执行时间Time,执行速度Speed
int SMSCL::RegWritePos(u8 ID, s16 Position, u16 Time, u16 Speed)
{
return writePos(ID, Position, Time, Speed, INST_REG_WRITE);
}
void SMSCL::RegWriteAction()
{
writeBuf(0xfe, 0, NULL, 0, INST_ACTION);
}
//写位置指令
//舵机ID[]数组,IDN数组长度,Position位置,执行时间Time,执行速度Speed
void SMSCL::SyncWritePos(u8 ID[], u8 IDN, s16 Position, u16 Time, u16 Speed)
{
if(Position<0){
Position = -Position;
Position |= (1<<15);
}
u8 buf[6];
Host2SCS(buf+0, buf+1, Position);
Host2SCS(buf+2, buf+3, Time);
Host2SCS(buf+4, buf+5, Speed);
snycWrite(ID, IDN, SMSCL_GOAL_POSITION_L, buf, 6);
}
//读位置,超时返回Err=1
s16 SMSCL::ReadPos(u8 ID, u8 *Err)
{
s16 curPos = readWord(ID, SMSCL_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;
}
//恒速控制指令
int SMSCL::WriteSpe(u8 ID, s16 Speed)
{
if(Speed<0){
Speed = -Speed;
Speed |= (1<<15);
}
return writeWord(ID,SMSCL_GOAL_SPEED_L , Speed);
}
//PWM输出模式
int SMSCL::WritePWM(u8 ID, s16 pwmOut)
{
if(pwmOut<0){
pwmOut = -pwmOut;
pwmOut |= (1<<15);
}
return writeWord(ID, SMSCL_GOAL_TIME_L, pwmOut);
}
//读负载,超时返回-1
int SMSCL::ReadLoad(u8 ID)
{
return readWord(ID, SMSCL_PRESENT_LOAD_L);
}
//读电压,超时返回-1
int SMSCL::ReadVoltage(u8 ID)
{
return readByte(ID, SMSCL_PRESENT_VOLTAGE);
}
//读温度,超时返回-1
int SMSCL::ReadTemper(u8 ID)
{
return readByte(ID, SMSCL_PRESENT_TEMPERATURE);
}
//Ping指令,返回舵机ID,超时返回-1
int SMSCL::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 SMSCL::wheelMode(u8 ID)
{
return writeByte(ID, SMSCL_MODE, 1);
}
int SMSCL::pwmMode(u8 ID)
{
return writeByte(ID, SMSCL_MODE, 2);
}
int SMSCL::joinMode(u8 ID)
{
return writeByte(ID, SMSCL_MODE, 0);
}
//复位舵机参数为默认值
int SMSCL::Reset(u8 ID)
{
flushSCS();
writeBuf(ID, 0, NULL, 0, INST_RESET);
return Ack(ID);
}
int SMSCL::WriteOfs(u8 ID, s16 Ofs)
{
if(Ofs<0){
Ofs = -Ofs;
Ofs |= (1<<11);
}
return writeWord(ID, SMSCL_OFS_L, Ofs);
}
int SMSCL::unLockEprom(u8 ID)
{
return writeByte(ID, SMSCL_LOCK, 0);//打开EPROM保存功能
}
int SMSCL::LockEprom(u8 ID)
{
return writeByte(ID, SMSCL_LOCK, 1);//关闭EPROM保存功能
}