forked from adafruit/Adafruit_Seesaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seesaw_motor.h
82 lines (72 loc) · 2.24 KB
/
seesaw_motor.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
#ifndef _SEESAW_MOTOR_H
#define _SEESAW_MOTOR_H
#include "Adafruit_seesaw.h"
/**************************************************************************/
/*!
@brief Class that stores state and functions for seesaw motor interface
*/
/**************************************************************************/
class seesaw_Motor {
public:
/**************************************************************************/
/*!
@brief class constructor
@param ss the seesaw object to use
*/
/**************************************************************************/
seesaw_Motor(Adafruit_seesaw *ss) {
_ss = ss;
_pina = -1;
_pinb = -1;
_throttle = 0;
}
~seesaw_Motor() {}
/**************************************************************************/
/*!
@brief attach the motor to the specified PWM pins
@param pina the positive pin to use
@param pinb the negative pin to use
*/
/**************************************************************************/
void attach(int pina, int pinb) {
_pina = pina;
_pinb = pinb;
}
/**************************************************************************/
/*!
@brief set the throttle
@param value the throttle value to set between -1 and 1. Passing 0 will turn
the motor off.
*/
/**************************************************************************/
void throttle(float value) {
if (_pina < 0 || _pinb < 0)
return;
value = constrain(value, -1.0, 1.0);
_throttle = value;
uint16_t absolute = fabs(value) * 65535;
if (value > 0) {
_ss->analogWrite(_pina, 0);
_ss->analogWrite(_pinb, absolute);
} else if (value < 0) {
_ss->analogWrite(_pina, absolute);
_ss->analogWrite(_pinb, 0);
} else {
// both are off
_ss->analogWrite(_pina, 0);
_ss->analogWrite(_pinb, 0);
}
}
/**************************************************************************/
/*!
@brief get the current throttle value
@returns the current throttle value between -1 and 1
*/
/**************************************************************************/
float getThrottle() { return _throttle; }
private:
Adafruit_seesaw *_ss;
int8_t _pina, _pinb;
float _throttle;
};
#endif