@@ -75,26 +75,34 @@ where
75
75
{
76
76
/// Instantiate a new [`Tb6612fng`] with the defined pins.
77
77
/// This also automatically enables the two PWM pins.
78
- /// The initial state of the motors will be [stopped](DriveCommand::Stop).
78
+ /// The initial state of the motors will be set to [stopped](DriveCommand::Stop).
79
+ /// The initial state of standby will be *disabled*.
79
80
///
80
81
/// Usage example:
81
82
/// ```
82
83
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
83
84
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
84
- /// # let motor_a_in1 = PinMock::new([]);
85
+ /// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
86
+ /// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
87
+ /// # use embedded_hal_mock::eh1::pin::State::{High, Low};
88
+ ///
89
+ /// # let motor_a_in1 = PinMock::new(&[PinTransaction::set(Low)]);
85
90
/// # let mut motor_a_in1_ = motor_a_in1.clone();
86
- /// # let motor_a_in2 = PinMock::new([ ]);
91
+ /// # let motor_a_in2 = PinMock::new(&[PinTransaction::set(Low) ]);
87
92
/// # let mut motor_a_in2_ = motor_a_in2.clone();
88
- /// # let motor_a_pwm = PwmMock::new(&[]);
93
+ /// # let motor_a_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0) ]);
89
94
/// # let mut motor_a_pwm_ = motor_a_pwm.clone();
90
- /// # let motor_b_in1 = PinMock::new([]);
95
+ ///
96
+ /// # let motor_b_in1 = PinMock::new(&[PinTransaction::set(Low)]);
91
97
/// # let mut motor_b_in1_ = motor_b_in1.clone();
92
- /// # let motor_b_in2 = PinMock::new([ ]);
98
+ /// # let motor_b_in2 = PinMock::new(&[PinTransaction::set(Low) ]);
93
99
/// # let mut motor_b_in2_ = motor_b_in2.clone();
94
- /// # let motor_b_pwm = PwmMock::new(&[]);
100
+ /// # let motor_b_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0) ]);
95
101
/// # let mut motor_b_pwm_ = motor_b_pwm.clone();
96
- /// # let standby = PinMock::new([]);
102
+ ///
103
+ /// # let standby = PinMock::new(&[PinTransaction::set(High)]);
97
104
/// # let mut standby_ = standby.clone();
105
+ ///
98
106
/// use tb6612fng::Tb6612fng;
99
107
///
100
108
/// let controller = Tb6612fng::new(
@@ -124,11 +132,15 @@ where
124
132
motor_b_pwm : MBPWM ,
125
133
standby : STBY ,
126
134
) -> Tb6612fng < MAIN1 , MAIN2 , MAPWM , MBIN1 , MBIN2 , MBPWM , STBY > {
127
- Tb6612fng {
135
+ let mut controller = Tb6612fng {
128
136
motor_a : Motor :: new ( motor_a_in1, motor_a_in2, motor_a_pwm) ,
129
137
motor_b : Motor :: new ( motor_b_in1, motor_b_in2, motor_b_pwm) ,
130
138
standby,
131
- }
139
+ } ;
140
+
141
+ controller. disable_standby ( ) ;
142
+
143
+ controller
132
144
}
133
145
134
146
/// Enable standby. This ignores any other setting currently done on the motors and puts them into standby.
@@ -165,18 +177,20 @@ where
165
177
{
166
178
/// Instantiate a new [`Motor`] with the defined pins.
167
179
/// This also automatically enables the PWM pin.
168
- /// The initial state of the motor will be [stopped](DriveCommand::Stop).
180
+ /// The initial state of the motor will be set to [stopped](DriveCommand::Stop).
169
181
///
170
182
/// Usage example:
171
183
/// ```
172
184
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
173
185
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
186
+ /// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
174
187
/// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
175
- /// # let motor_in1 = PinMock::new([]);
188
+ /// # use embedded_hal_mock::eh1::pin::State::{Low};
189
+ /// # let motor_in1 = PinMock::new(&[PinTransaction::set(Low)]);
176
190
/// # let mut motor_in1_ = motor_in1.clone();
177
- /// # let motor_in2 = PinMock::new([ ]);
191
+ /// # let motor_in2 = PinMock::new(&[PinTransaction::set(Low) ]);
178
192
/// # let mut motor_in2_ = motor_in2.clone();
179
- /// # let motor_pwm = PwmMock::new([ ]);
193
+ /// # let motor_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0) ]);
180
194
/// # let mut motor_pwm_ = motor_pwm.clone();
181
195
/// use tb6612fng::Motor;
182
196
///
@@ -191,12 +205,16 @@ where
191
205
/// # motor_pwm_.done();
192
206
/// ```
193
207
pub fn new ( in1 : IN1 , in2 : IN2 , pwm : PWM ) -> Motor < IN1 , IN2 , PWM > {
194
- Motor {
208
+ let mut motor = Motor {
195
209
in1,
196
210
in2,
197
211
pwm,
198
212
current_drive_command : DriveCommand :: Stop ,
199
- }
213
+ } ;
214
+
215
+ motor. drive ( motor. current_drive_command ) . unwrap ( ) ;
216
+
217
+ motor
200
218
}
201
219
202
220
/// Drive forward with the defined speed. Note that the speed is a percentage between 0 and 100!
@@ -296,11 +314,13 @@ mod tests {
296
314
#[ test]
297
315
fn test_motor_stop ( ) {
298
316
let max_duty = 100 ;
299
- let motor_in1_expectations = [ PinTransaction :: set ( Low ) ] ;
300
- let motor_in2_expectations = [ PinTransaction :: set ( Low ) ] ;
317
+ let motor_in1_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( Low ) ] ;
318
+ let motor_in2_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( Low ) ] ;
301
319
let motor_pwm_expectations = [
302
320
PwmTransaction :: max_duty_cycle ( max_duty) ,
303
321
PwmTransaction :: set_duty_cycle ( 0 ) ,
322
+ PwmTransaction :: max_duty_cycle ( max_duty) ,
323
+ PwmTransaction :: set_duty_cycle ( 0 ) ,
304
324
] ;
305
325
let mut motor_in1 = PinMock :: new ( & motor_in1_expectations) ;
306
326
let mut motor_in2 = PinMock :: new ( & motor_in2_expectations) ;
@@ -321,11 +341,13 @@ mod tests {
321
341
#[ test]
322
342
fn test_motor_brake ( ) {
323
343
let max_duty = 100 ;
324
- let motor_in1_expectations = [ PinTransaction :: set ( High ) ] ;
325
- let motor_in2_expectations = [ PinTransaction :: set ( High ) ] ;
344
+ let motor_in1_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( High ) ] ;
345
+ let motor_in2_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( High ) ] ;
326
346
let motor_pwm_expectations = [
327
347
PwmTransaction :: max_duty_cycle ( max_duty) ,
328
348
PwmTransaction :: set_duty_cycle ( 0 ) ,
349
+ PwmTransaction :: max_duty_cycle ( max_duty) ,
350
+ PwmTransaction :: set_duty_cycle ( 0 ) ,
329
351
] ;
330
352
let mut motor_in1 = PinMock :: new ( & motor_in1_expectations) ;
331
353
let mut motor_in2 = PinMock :: new ( & motor_in2_expectations) ;
@@ -347,9 +369,11 @@ mod tests {
347
369
fn test_motor_drive_forward ( ) {
348
370
let max_duty = 100 ;
349
371
let speed: u8 = 100 ;
350
- let motor_in1_expectations = [ PinTransaction :: set ( High ) ] ;
351
- let motor_in2_expectations = [ PinTransaction :: set ( Low ) ] ;
372
+ let motor_in1_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( High ) ] ;
373
+ let motor_in2_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( Low ) ] ;
352
374
let motor_pwm_expectations = [
375
+ PwmTransaction :: max_duty_cycle ( max_duty) ,
376
+ PwmTransaction :: set_duty_cycle ( 0 ) ,
353
377
PwmTransaction :: max_duty_cycle ( max_duty) ,
354
378
PwmTransaction :: set_duty_cycle ( speed as u16 ) ,
355
379
] ;
@@ -373,9 +397,11 @@ mod tests {
373
397
fn test_motor_drive_backwards ( ) {
374
398
let max_duty = 100 ;
375
399
let speed = 100 ;
376
- let motor_in1_expectations = [ PinTransaction :: set ( Low ) ] ;
377
- let motor_in2_expectations = [ PinTransaction :: set ( High ) ] ;
400
+ let motor_in1_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( Low ) ] ;
401
+ let motor_in2_expectations = [ PinTransaction :: set ( Low ) , PinTransaction :: set ( High ) ] ;
378
402
let motor_pwm_expectations = [
403
+ PwmTransaction :: max_duty_cycle ( max_duty) ,
404
+ PwmTransaction :: set_duty_cycle ( 0 ) ,
379
405
PwmTransaction :: max_duty_cycle ( max_duty) ,
380
406
PwmTransaction :: set_duty_cycle ( speed as u16 ) ,
381
407
] ;
@@ -397,16 +423,20 @@ mod tests {
397
423
398
424
#[ test]
399
425
fn test_motor_drive_invalid_speed ( ) {
400
- let motor_in1_expectations = [ ] ;
401
- let motor_in2_expectations = [ ] ;
402
- let motor_pwm_expectations = [ ] ;
426
+ let max_duty = 100 ;
427
+ let motor_in1_expectations = [ PinTransaction :: set ( Low ) ] ;
428
+ let motor_in2_expectations = [ PinTransaction :: set ( Low ) ] ;
429
+ let motor_pwm_expectations = [
430
+ PwmTransaction :: max_duty_cycle ( max_duty) ,
431
+ PwmTransaction :: set_duty_cycle ( 0 ) ,
432
+ ] ;
403
433
let mut motor_in1 = PinMock :: new ( & motor_in1_expectations) ;
404
434
let mut motor_in2 = PinMock :: new ( & motor_in2_expectations) ;
405
435
let mut motor_pwm = PwmMock :: new ( & motor_pwm_expectations) ;
406
436
407
437
let mut motor = Motor :: new ( motor_in1. clone ( ) , motor_in2. clone ( ) , motor_pwm. clone ( ) ) ;
408
438
409
- let current_drive_command = motor. current_drive_command ( ) . clone ( ) ;
439
+ let current_drive_command = * motor. current_drive_command ( ) ;
410
440
let current_speed = motor. current_speed ( ) ;
411
441
412
442
assert_eq ! (
0 commit comments