-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANMotorUnit.h
315 lines (284 loc) · 11.7 KB
/
CANMotorUnit.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* File: CANMotorUnit.h
* Authors: Jaden Bottemiller, Benton Kwong, Dylan Tomberlin.
* Organization: Husky Robotics Team
*
* This file includes fuction prototypes for CAN Packet manipulation
* using the Hindsight CAN Communication standard. Specific files
* for the motor unit boards.
* Documentation: https://huskyroboticsteam.slite.com/app/channels/iU0BryG7M9/collections/aXvWTcIR6c/notes/4otlSFsSp2
*/
#include "CANPacket.h"
// TODO: Add parameters to packet assembly
//Mode set (PWM or PID)
void AssembleModeSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t mode);
uint8_t GetModeFromPacket(CANPacket *packet);
//PWM value and direction set
void AssemblePWMDirSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
int16_t PWMSet);
int16_t GetPWMFromPacket(CANPacket *packet);
int32_t GetDirectionFromPacket(CANPacket *packet);
//PID postional target set
void AssemblePIDTargetSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
int32_t target);
int32_t GetPIDTargetFromPacket(CANPacket *packet);
//P coeffiecent set
void AssemblePSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
int32_t pCoef);
int32_t GetPFromPacket(CANPacket *packet);
//I coeffiecent set
void AssembleISetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
int32_t iCoef);
int32_t GetIFromPacket(CANPacket *packet);
//D coeffiecent set
void AssembleDSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
int32_t dCoef);
int32_t GetDFromPacket(CANPacket *packet);
//Initialize with mode (motors shall not move until have been inited)
void AssembleInitializePacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t initMode);
uint8_t GetInitModeFromPacket(CANPacket *packet);
//Limit switch alert
//each bit represents one limit switch, 1 for closed, 0 for open,
//switch number corresponds to the bit number
void AssembleLimitSwitchAlertPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t switches);
uint8_t GetLimStatusFromPacket(CANPacket *packet);
//Encoder pulses per joint revolution set
void AssembleEncoderPPJRSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint32_t pulses);
uint32_t GetEncoderPPJRFromPacket(CANPacket *packet);
//Maximum joint rotations set
void AssembleMaxJointRevolutionPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint32_t revolutions);
uint32_t GetMaxJointRevolutionsFromPacket(CANPacket *packet);
// Potentiometer configuration packets
/**
* @brief Assemble a packet to set the high point of the potentiometer.
*
* This, along with AssemblePotLoSetPacket() are required to initialize the potentiometer.
* Behavior is undefined if only one packet is sent and not the other.
*
* @param packetToAssemble The packet to write the data into.
* @param targetDeviceGroup The group of the target device.
* @param targetDeviceSerial Ther serial code of the target device.
* @param adcHi The raw ADC value of the pot at the max.
* @param mdegHi The joint pos in millideg at the max.
*
* @see https://github.com/huskyroboticsteam/HindsightCAN/wiki/Motor-Unit-Packets
*/
void AssemblePotHiSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint16_t adcHi,
int32_t mdegHi);
/**
* @brief Assemble a packet to set the low point of the potentiometer.
*
* This, along with AssemblePotHiSetPacket() are required to initialize the potentiometer.
* Behavior is undefined if only one packet is sent and not the other.
*
* @param packetToAssemble The packet to write the data into.
* @param targetDeviceGroup The group of the target device.
* @param targetDeviceSerial Ther serial code of the target device.
* @param adcHi The raw ADC value of the pot at the low.
* @param mdegHi The joint pos in millideg at the low.
*
* @see https://github.com/huskyroboticsteam/HindsightCAN/wiki/Motor-Unit-Packets
*/
void AssemblePotLoSetPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint16_t adcLo,
int32_t mdegLo);
/**
* @brief Get the raw ADC value from a pot initialization packet.
*
* @param packet The packet, produced by either AssemblePotHiSetPacket()
* or AssemblePotLoSetPacket(), to read from.
* @return uint16_t The raw ADC value.
*/
uint16_t GetPotInitADCFromPacket(const CANPacket *packet);
/**
* @brief Get the joint position from a pot initialization packet.
*
* @param packet The packet, produced by either AssemblePotHiSetPacket()
* or AssemblePotLoSetPacket(), to read from.
* @return int32_t The joint position in millidegrees.
*/
int32_t GetPotInitmDegFromPacket(const CANPacket *packet);
//Initialize Encoder Settings
void AssembleEncoderInitializePacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t encoderType,
uint8_t angleDirection,
uint8_t zeroAngle);
uint8_t GetEncoderTypeFromPacket(CANPacket *packet);
uint8_t GetEncoderDirectionFromPacket(CANPacket *packet);
uint8_t GetEncoderZeroFromPacket(CANPacket *packet);
void AssembleMaxPIDPWMPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint16_t PWMSetMax);
uint16_t GetMaxPIDPWMFromPacket(CANPacket *packet);
/**
* @brief Assemble a packet to initialize the PCA servo
*
* @param packetToAssemble The packet to write the data into.
* @param targetDeviceGroup The group of the target device.
* @param targetDeviceSerial The serial code of the target device.
* @param serverNum The servo number
* @param angle The angle degree in millidegrees
*
* @see https://github.com/huskyroboticsteam/HindsightCAN/wiki/Motor-Unit-Packets
*/
void AssemblePCAServoPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t serverNum,
int32_t angle);
/**
* @brief Get the PCA servo angle value from its packet
*
* @param packet The packet, produced by AssemblePCAServoPacket, to read from.
* @return int32_t The angle in millidegrees.
*/
int32_t GetAngleValueFromPacket(const CANPacket *packet);
/**
* @brief Get the PCA servo number from its packet
*
* @param packet The packet, produced by AssemblePCAServoPacket, to read from.
* @return uint8_t the servo num.
*/
uint8_t GetServoNumFromPacket(const CANPacket *packet);
/**
* @brief Assemble a packet to set the encoder bounds on limit switch interrupt
*
* @param packetToAssemble The packet to write the data into.
* @param targetDeviceGroup The group of the target device.
* @param targetDeviceSerial The serial code of the target device.
* @param limSwNum The limit switch that the encoder bound should be associated
* with.
* @param encoderBound The count the encoder should be set to when the given
* limit switch is hit.
*
* @see https://github.com/huskyroboticsteam/HindsightCAN/wiki/Motor-Unit-Packets
*/
void AssembleLimSwEncoderBoundPacket(CANPacket* packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t limSwNum,
int32_t encoderBound);
/*
* @brief Get encoder bound value from the limit switch encoder bound packet
*
* @param packet The packet, produced by AssembleLimSwEncoderBoundPacket, to
* read from.
*
* @return int32_t The encoder bound limit.
*/
int32_t GetEncoderValueFromPacket(const CANPacket* packet);
/*
* @brief Get limit switch number associated with encoder bound.
*
* @param packet The packet, produced by AssembleLimSwEncoderBoundPacket, to
* read from.
*
* @return uint8_t The limit switch associated with the encoder bound.
*/
uint8_t GetLimSwNumFromPacket(const CANPacket* packet);
/**
* @brief Get the ID of the peripheral to control.
*
* @param packet The packet sent to read from.
* @return uint8_t The peripheral ID.
*/
uint8_t GetPeripheralID(const CANPacket* packet);
/**
* @brief Get the data to set the peripheral.
*
* @param packet The packet sent to read from.
* @return uint8_t The data to set the peripheral to.
*/
uint16_t GetPeripheralData(const CANPacket* packet);
// Motor Unit Packet IDs
#define ID_MOTOR_UNIT_MODE_SEL (uint8_t) 0x00
#define ID_MOTOR_UNIT_PWM_DIR_SET (uint8_t) 0x03
#define ID_MOTOR_UNIT_PID_POS_TGT_SET (uint8_t) 0x04
#define ID_MOTOR_UNIT_PID_P_SET (uint8_t) 0x05
#define ID_MOTOR_UNIT_PID_I_SET (uint8_t) 0x06
#define ID_MOTOR_UNIT_PID_D_SET (uint8_t) 0x07
#define ID_MOTOR_UNIT_INIT (uint8_t) 0x08
#define ID_MOTOR_UNIT_LIM_ALERT (uint8_t) 0x09
#define ID_MOTOR_UNIT_ENC_PPJR_SET (uint8_t) 0x0A
#define ID_MOTOR_UNIT_MAX_JNT_REV_SET (uint8_t) 0x0B
#define ID_MOTOR_UNIT_ENC_INIT (uint8_t) 0x0C
#define ID_MOTOR_UNIT_MAX_PID_PWM (uint8_t) 0x0D
#define ID_MOTOR_UNIT_PCA_PWM (uint8_t) 0x0E
#define ID_MOTOR_UNIT_POT_INIT_LO (uint8_t) 0x0F
#define ID_MOTOR_UNIT_POT_INIT_HI (uint8_t) 0x10
#define ID_MOTOR_UNIT_PCA_SERVO (uint8_t) 0x11
#define ID_MOTOR_UNIT_SET_ENCODER_BOUND (uint8_t) 0x12
#define ID_MOTOR_UNIT_SET_PERIPHERALS (uint8_t) 0x13
// Packet DLCs
#define DLC_MOTOR_UNIT_MODE_SEL (uint8_t) 0x02
#define DLC_MOTOR_UNIT_PWM_DIR_SET (uint8_t) 0x03
#define DLC_MOTOR_UNIT_PID_POS_TGT_SET (uint8_t) 0x05
#define DLC_MOTOR_UNIT_PID_P_SET (uint8_t) 0x05
#define DLC_MOTOR_UNIT_PID_I_SET (uint8_t) 0x05
#define DLC_MOTOR_UNIT_PID_D_SET (uint8_t) 0x05
#define DLC_MOTOR_UNIT_INIT (uint8_t) 0x02
#define DLC_MOTOR_UNIT_LIM_ALERT (uint8_t) 0x04
#define DLC_MOTOR_UNIT_ENC_PPJR_SET (uint8_t) 0x05
#define DLC_MOTOR_UNIT_MAX_JNT_REV_SET (uint8_t) 0x02
#define DLC_MOTOR_UNIT_ENC_INIT (uint8_t) 0x02
#define DLC_MOTOR_UNIT_MAX_PID_PWM (uint8_t) 0x03
#define DLC_MOTOR_UNIT_POT_INIT (uint8_t) 0x07
#define DLC_MOTOR_UNIT_PCA_SERVO (uint8_t) 0x06
#define DLC_MOTOR_UNIT_ENCODER_BOUND (uint8_t) 0x06
#define DLC_MOTOR_UNIT_PERIPHERALS (uint8_t) 0x2
//Packet priorities
#define PRIO_MOTOR_UNIT_MODE_SEL PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PWM_DIR_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PID_POS_TGT_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PID_P_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PID_I_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PID_D_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_INIT PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_LIM_ALERT PACKET_PRIORITY_HIGH
#define PRIO_MOTOR_UNIT_ENC_PPJR_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_MAX_JNT_REV_SET PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_ENC_INIT PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_MAX_PID_PWM PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_POT_INIT PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_PCA_SERVO PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_SET_ENCODER_BOUND PACKET_PRIORITY_NORMAL
#define PRIO_MOTOR_UNIT_SET_PERIPHERALS PACKET_PRIORITY_NORMAL
// Motor Unit Mode IDs
#define MOTOR_UNIT_MODE_PWM (uint8_t) 0x00
#define MOTOR_UNIT_MODE_PID (uint8_t) 0x01
// Motor Unit Peripheral IDs
#define NULL_PERIPH_ID (uint8_t) 0x00
#define LASER_PERIPH_ID (uint8_t) 0x01
#define LINEAR_PERIPH_ID (uint8_t) 0x02