-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotor.cpp
More file actions
86 lines (84 loc) · 2.43 KB
/
Motor.cpp
File metadata and controls
86 lines (84 loc) · 2.43 KB
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
#include "Motor.h"
Motor::Motor( ){
}
/*
* Creating a motor instantiates four integers, *and that is all*, besides for
* the functions that move the motors.
* in1, in2 -> digital output pin connecting to (A/B)IN(1/2) of the driver
* pwm -> analog output pin connecting to PWM(A/B)
* stby -> digital output bridged to standby of motor driver
* encoder -> the an encoder object to account for this motor's hertz
*/
Motor::Motor( int in1, int in2, int pwm, Encoder *encoder ){
this -> in1 = in1;
this -> in2 = in2;
this -> pwm = pwm;
this -> encoder = encoder;
}
Motor::Motor( int in1, int in2, int pwm ){
this -> in1 = in1;
this -> in2 = in2;
this -> pwm = pwm;
}
/*
* Activates the motor clockwise, where speed is reflected as an integer 'pwmv'
* between 0 and 255. 255 is max, 0 is none.
*/
void Motor::clockwise( int pwmv ){
digitalWrite( in1, HIGH );
digitalWrite( in2, LOW );
analogWrite( pwm, pwmv );
}
/*
* Activates the motor anticlockwise, speed is reflected by 'pwmv', and integer
* between 0 and 255. 255 is max, 0 is none.
*/
void Motor::anticlockwise( int pwmv ){
digitalWrite( in1, LOW );
digitalWrite( in2, HIGH );
analogWrite( pwm, pwmv );
}
/*
* Don't use this, we don't know what it does yet.
* Important: the slower a motor moves, the less braking
* torque available. StackOverflow electronics suggests this is a method of
* braking involving short-circuiting.
*/
void Motor::short_brake( int pwmv ){
digitalWrite( in1, HIGH );
digitalWrite( in2, HIGH );
analogWrite( pwm, pwmv );
}
/*
* Cuts power to the motor (no braking, expect the motor to continue to go
* for a little
*/
void Motor::stop( ){
digitalWrite( in1, HIGH );
digitalWrite( in2, HIGH );
analogWrite( pwm, 0 );
}
/*
* An extended call to this motor's encoder's get_hertz( ) function. -2 is
* returned in the case that this motor doesn't have an encoder.
*/
int Motor::get_hertz( ){
return encoder ? this -> encoder -> get_hertz( ) : -2;
}
/*
* An extended call to this motor's encoder's get_hertz( ) function. -2 is
* returned in the case that this motor doesn't have an encoder.
*/
int Motor::get_hertz( int timeout ){
return encoder ? this -> encoder -> get_hertz( timeout ): -2;
}
/*
* setup function in coherence with Component class. Sets all four pins to
* OUTPUT.
*/
void Motor::setup( ){
pinMode( in1, OUTPUT );
pinMode( in2, OUTPUT );
pinMode( pwm, OUTPUT );
if ( encoder ) encoder -> setup( );
}