Wicked Device Motor Shield
Software Library for Wicked Device Motor Shield
WickedMotorShield.h
Go to the documentation of this file.
1 /** @file
2  */
3 /* Copyright (C) 2014 by Victor Aprea <victor.aprea@wickeddevice.com>
4 
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 
24 #ifndef _WICKED_MOTOR_SHIELD_H
25 #define _WICKED_MOTOR_SHIELD_H
26 
27 #if ARDUINO >= 100
28  #include <Arduino.h> // Arduino 1.0
29 #else
30  #include <WProgram.h> // Arduino 0022
31 #endif
32 
33 #include <stdint.h>
34 /** Integer value defining counterclockwise rotation. (Value = 0) */
35 #define DIR_CCW (0)
36 /** Integer value defining clockwise rotation. (Value = 1) */
37 #define DIR_CW (1)
38 /** Integer value defining "brake off". */
39 #define BRAKE_OFF (0)
40 /**
41  * Integer value defining "brake hard".
42  */
43 #define BRAKE_HARD (1)
44 /**
45  * Integer value defining "brake soft".
46  */
47 #define BRAKE_SOFT (2)
48 /**
49  * Integer value identifying motor M1.
50  */
51 #define M1 (0)
52 /**
53  * Integer value identifying motor M2.
54  */
55 #define M2 (1)
56 /**
57  * Integer value identifying motor M3.
58  */
59 #define M3 (2)
60 /**
61  * Integer value identifying motor M4.
62  */
63 #define M4 (3)
64 /**
65  * Intger value identifying motor M5.
66  */
67 #define M5 (4)
68 /**
69  * Integer value identifying motor M6.
70  */
71 #define M6 (5)
72 
73 // these bits are in shift register 1
74 /**
75  * Shows location of bit for direction status of motor #M4 in
76  * WickedMotorShield#shift_register_1.
77  *
78  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
79  */
80 #define M4_DIR_MASK (0x80)
81 /**
82  * Shows location of bit for brake status of motor #M4 in
83  * WickedMotorShield#shift_register_1.
84  *
85  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
86  */
87 #define M4_BRAKE_MASK (0x40)
88 /**
89  * Shows location of bit for brake status for motor #M1 in
90  * WickedMotorShield#first_shift_register.
91  *
92  * See #M1_BRAKE_MASK
93  */
94 #define M1_DIR_MASK (0x20)
95 /**
96  * Shows location of bit for brake status of motor #M1 in
97  * WickedMotorShield#first_shift_register.
98  *
99  * If M1_BRAKE_MASK bit is zero, brake
100  * is set to BRAKE_OFF and M1_DIR_MASK bit
101  * indicates direction of rotation. If M1_BRAKE_MASK
102  * bit is one, M1_DIR_MASK indicates whether hard
103  * brake or soft brake.
104  *
105  * There are equivalent macro definitions BRAKE_MASK and
106  * DIR_MASK for the other motors (M2, M3, M4, M5, M6).
107  */
108 #define M1_BRAKE_MASK (0x10)
109 /**
110  * Shows location of bit for direction status of motor #M2 in
111  * WickedMotorShield#first_shift_register.
112  *
113  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
114  */
115 #define M2_DIR_MASK (0x08)
116 /**
117  * Shows location of bit for brake status of motor #M2 in
118  * WickedMotorShield#first_shift_register.
119  *
120  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
121  */
122 #define M2_BRAKE_MASK (0x04)
123 /**
124  * Shows location of bit for direction status of motor #M3 in
125  * WickedMotorShield#first_shift_register.
126  *
127  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
128  */
129 #define M3_DIR_MASK (0x02)
130 /**
131  * Shows location of bit for brake status of motor #M3 in
132  * WickedMotorShield#first_shift_register.
133  *
134  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
135  */
136 #define M3_BRAKE_MASK (0x01)
137 
138 // these bits are in shift register 2
139 /**
140  * Shows location of bit for direction status of motor #M6 in
141  * WickedMotorShield#second_shift_register.
142  *
143  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
144  */
145 #define M6_DIR_MASK (0x80)
146 /**
147  * Shows location of bit for brake status of motor #M6 in
148  * WickedMotorShield#second_shift_register.
149  *
150  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
151  */
152 #define M6_BRAKE_MASK (0x40)
153 /**
154  * Shows location of bit for direction status of motor #M5 in
155  * WickedMotorShield#second_shift_register.
156  *
157  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
158  */
159 #define M5_DIR_MASK (0x20)
160 /**
161  * Shows location of bit for brake status of motor #M5 in
162  * WickedMotorShield#second_shift_register.
163  *
164  * See #M1_DIR_MASK and #M1_BRAKE_MASK for more information.
165  */
166 #define M5_BRAKE_MASK (0x10)
167 
168 /**
169  * Digital pin used for specfying speed of motor M2.
170  */
171 #define M2_PWM_PIN (9)
172 /**
173  * Digital pin used for specifying speed of motor M3.
174  */
175 #define M3_PWM_PIN (5)
176 /**
177  * Digital pin used for specifying speed of motor M4.
178  */
179 #define M4_PWM_PIN (10)
180 /**
181  * Digital pin used for specifying speed of motor M5.
182  */
183 #define M5_PWM_PIN (6)
184 
185 #define RCIN1 (1)
186 #define RCIN2 (2)
187 
188 #define SERIAL_CLOCK_PIN (2)
189 #define SERIAL_LATCH_PIN (7)
190 
191 #define OPERATION_CLEAR (0)
192 #define OPERATION_SET (1)
193 #define OPERATION_NONE (2)
194 
195 #define USE_ALTERNATE_PINS (1)
196 
198  private:
199  static uint8_t first_shift_register;
200  static uint8_t second_shift_register;
201  static uint8_t SERIAL_DATA_PIN;
202  static uint8_t RCIN1_PIN;
203  static uint8_t RCIN2_PIN;
204  static uint8_t get_rc_input_pin(uint8_t rc_input_number);
205  protected:
206  /* Digital pin to be used for setting PWM (pulse width modulation) duty cycle for motor M1.
207  *
208  * Value is different for standard and alternate pins.
209  */
210  static uint8_t M1_PWM_PIN;
211  /* Digital pin to be used for setting PWM (pulse width modulation) duty cycle for motor M6.
212  *
213  * Value is different for standard and alternate pins.
214  */
215  static uint8_t M6_PWM_PIN;
216  static uint8_t old_dir[6];
217  uint8_t get_shift_register_value(uint8_t motor_number);
218  void apply_mask(uint8_t * shift_register_value, uint8_t mask, uint8_t operation);
219  uint8_t filter_mask(uint8_t shift_register_value, uint8_t mask);
220  void set_shift_register_value(uint8_t motor_number, uint8_t value);
221  void load_shift_register(void);
222  uint8_t get_motor_directionM(uint8_t motor_number);
223  uint8_t get_motor_brakeM(uint8_t motor_number);
224 
225  void setSpeedM(uint8_t motor_number, uint8_t pwm_val); // 0..255
226  void setDirectionData(uint8_t motor_number, uint8_t direction); // DIR_CCW, DIR_CW
227  void setBrakeData(uint8_t motor_number, uint8_t brake_type); // BRAKE_HARD, BRAKE_SOFT, BRAKE_OFF
228  public:
229  WickedMotorShield(uint8_t use_alternate_pins = 0); // defaults for arduino uno
230  static uint32_t getRCIN(uint8_t rc_input_number, uint32_t timeout = 0); // returns the result for pulseIn for the requested channel
231  static uint8_t version(void);
232 };
233 
235  private:
236  void stepMotor(int this_step);
237 
238  uint8_t direction; // Direction of rotation
239  uint16_t speed; // Speed in RPMs
240  uint32_t step_delay; // delay between steps, in ms, based on speed
241  uint16_t number_of_steps; // total number of steps this motor can take
242  uint16_t step_number; // which step the motor is on
243  uint32_t last_step_time; // time stamp in ms of when the last step was taken
244  uint8_t m1; // the M-number of the first coil
245  uint8_t m2; // the M-number of the second coil
246 
247  public:
248  Wicked_Stepper(uint16_t number_of_steps, uint8_t m1, uint8_t m2, uint8_t use_alternate_pins = 0);
249  void setSpeed(uint32_t speed);
250  void step(int16_t number_of_steps);
251 };
252 
253 
255  private:
256  uint8_t get_motor_direction(void);
257  uint8_t motor_number;
258  public:
259  Wicked_DCMotor(uint8_t motor_number, uint8_t use_alternate_pins = 0);
260  /**
261  * Set the speed of rotation as a value in the range 0..255.
262  * @param pwm_val Value for speed with an integer in the range 0 to 255.
263  */
264  void setSpeed(uint8_t pwm_val); // 0..255
265  /**
266  * Set the direction of rotation of the motor.
267  */
268  void setDirection(uint8_t direction); // DIR_CCW, DIR_CW
269  /**
270  * Set the type of braking for the motor.
271  * @param brake_type Options are BRAKE_HARD, BRAKE_SOFT, and BRAKE_OFF.
272  *
273  * It is assumed that BRAKE_HARD refers to dynamic braking with the
274  * leads on the motor connected together, BRAKE_SOFT refers to the
275  * situation where no power flows through the motor or both leads
276  * are connected to ground, and BRAKE_OFF refers to power being
277  * applied to DC motor.
278  */
279  void setBrake(uint8_t brake_type); // BRAKE_HARD, BRAKE_SOFT, BRAKE_OFF
280  uint16_t currentSense(void);
281 };
282 
283 #endif /* _WICKED_MOTOR_SHIELD_H */
284 
Wicked_Stepper::Wicked_Stepper
Wicked_Stepper(uint16_t number_of_steps, uint8_t m1, uint8_t m2, uint8_t use_alternate_pins=0)
Definition: WickedMotorShield.cpp:524
WickedMotorShield::load_shift_register
void load_shift_register(void)
Definition: WickedMotorShield.cpp:181
WickedMotorShield::second_shift_register
static uint8_t second_shift_register
Definition: WickedMotorShield.h:200
Wicked_Stepper::setSpeed
void setSpeed(uint32_t speed)
Definition: WickedMotorShield.cpp:545
WickedMotorShield::get_motor_brakeM
uint8_t get_motor_brakeM(uint8_t motor_number)
Definition: WickedMotorShield.cpp:493
WickedMotorShield::SERIAL_DATA_PIN
static uint8_t SERIAL_DATA_PIN
Definition: WickedMotorShield.h:201
WickedMotorShield::RCIN1_PIN
static uint8_t RCIN1_PIN
Definition: WickedMotorShield.h:202
WickedMotorShield::RCIN2_PIN
static uint8_t RCIN2_PIN
Definition: WickedMotorShield.h:203
Wicked_DCMotor::motor_number
uint8_t motor_number
Definition: WickedMotorShield.h:257
WickedMotorShield::apply_mask
void apply_mask(uint8_t *shift_register_value, uint8_t mask, uint8_t operation)
Definition: WickedMotorShield.cpp:236
Wicked_DCMotor::currentSense
uint16_t currentSense(void)
Definition: WickedMotorShield.cpp:632
WickedMotorShield
Definition: WickedMotorShield.h:197
Wicked_Stepper
Definition: WickedMotorShield.h:234
WickedMotorShield::setDirectionData
void setDirectionData(uint8_t motor_number, uint8_t direction)
Definition: WickedMotorShield.cpp:328
WickedMotorShield::version
static uint8_t version(void)
Definition: WickedMotorShield.cpp:518
Wicked_Stepper::speed
uint16_t speed
Definition: WickedMotorShield.h:239
WickedMotorShield::get_shift_register_value
uint8_t get_shift_register_value(uint8_t motor_number)
Definition: WickedMotorShield.cpp:195
WickedMotorShield::M1_PWM_PIN
static uint8_t M1_PWM_PIN
Definition: WickedMotorShield.h:210
WickedMotorShield::setSpeedM
void setSpeedM(uint8_t motor_number, uint8_t pwm_val)
Definition: WickedMotorShield.cpp:292
WickedMotorShield::getRCIN
static uint32_t getRCIN(uint8_t rc_input_number, uint32_t timeout=0)
Definition: WickedMotorShield.cpp:264
WickedMotorShield::set_shift_register_value
void set_shift_register_value(uint8_t motor_number, uint8_t value)
Definition: WickedMotorShield.cpp:209
Wicked_Stepper::stepMotor
void stepMotor(int this_step)
Definition: WickedMotorShield.cpp:586
WickedMotorShield::filter_mask
uint8_t filter_mask(uint8_t shift_register_value, uint8_t mask)
Definition: WickedMotorShield.cpp:255
Wicked_Stepper::step_number
uint16_t step_number
Definition: WickedMotorShield.h:242
Wicked_DCMotor::get_motor_direction
uint8_t get_motor_direction(void)
Definition: WickedMotorShield.cpp:628
WickedMotorShield::old_dir
static uint8_t old_dir[6]
Definition: WickedMotorShield.h:216
Wicked_DCMotor::setDirection
void setDirection(uint8_t direction)
Definition: WickedMotorShield.cpp:617
Wicked_Stepper::step
void step(int16_t number_of_steps)
Definition: WickedMotorShield.cpp:549
Wicked_Stepper::last_step_time
uint32_t last_step_time
Definition: WickedMotorShield.h:243
Wicked_DCMotor::Wicked_DCMotor
Wicked_DCMotor(uint8_t motor_number, uint8_t use_alternate_pins=0)
Definition: WickedMotorShield.cpp:610
WickedMotorShield::M6_PWM_PIN
static uint8_t M6_PWM_PIN
Definition: WickedMotorShield.h:215
WickedMotorShield::WickedMotorShield
WickedMotorShield(uint8_t use_alternate_pins=0)
Definition: WickedMotorShield.cpp:148
Wicked_Stepper::m1
uint8_t m1
Definition: WickedMotorShield.h:244
WickedMotorShield::get_rc_input_pin
static uint8_t get_rc_input_pin(uint8_t rc_input_number)
Definition: WickedMotorShield.cpp:279
Wicked_Stepper::direction
uint8_t direction
Definition: WickedMotorShield.h:238
Wicked_Stepper::number_of_steps
uint16_t number_of_steps
Definition: WickedMotorShield.h:241
Wicked_DCMotor
Definition: WickedMotorShield.h:254
Wicked_Stepper::m2
uint8_t m2
Definition: WickedMotorShield.h:245
WickedMotorShield::first_shift_register
static uint8_t first_shift_register
Definition: WickedMotorShield.h:199
Wicked_DCMotor::setBrake
void setBrake(uint8_t brake_type)
Definition: WickedMotorShield.cpp:623
WickedMotorShield::setBrakeData
void setBrakeData(uint8_t motor_number, uint8_t brake_type)
Definition: WickedMotorShield.cpp:383
WickedMotorShield::get_motor_directionM
uint8_t get_motor_directionM(uint8_t motor_number)
Definition: WickedMotorShield.cpp:464
Wicked_Stepper::step_delay
uint32_t step_delay
Definition: WickedMotorShield.h:240
Wicked_DCMotor::setSpeed
void setSpeed(uint8_t pwm_val)
Definition: WickedMotorShield.cpp:651