-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotorControllerWiringpi.cpp
78 lines (64 loc) · 1.87 KB
/
MotorControllerWiringpi.cpp
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
#include "MotorControllerWiringpi.h"
#include <wiringPi.h>
#include <math.h>
#define MOTOR_A_ENABLE_PIN 1
#define MOTOR_A_IN1_PIN 4
#define MOTOR_A_IN2_PIN 5
#define MOTOR_B_ENABLE_PIN 24
#define MOTOR_B_IN1_PIN 22
#define MOTOR_B_IN2_PIN 23
MotorControllerWiringpi* MotorControllerWiringpi::getInstance(void)
{
static MotorControllerWiringpi wiringpiController;
return (&wiringpiController);
}
MotorControllerWiringpi::MotorControllerWiringpi(void)
{
wiringPiSetup();
pinMode(MOTOR_A_ENABLE_PIN, PWM_OUTPUT);
pinMode(MOTOR_A_IN1_PIN, OUTPUT);
pinMode(MOTOR_A_IN2_PIN, OUTPUT);
pinMode(MOTOR_B_ENABLE_PIN, PWM_OUTPUT);
pinMode(MOTOR_B_IN1_PIN, OUTPUT);
pinMode(MOTOR_B_IN2_PIN, OUTPUT);
digitalWrite(MOTOR_A_ENABLE_PIN, LOW);
digitalWrite(MOTOR_A_IN1_PIN, HIGH);
digitalWrite(MOTOR_A_IN2_PIN, LOW);
digitalWrite(MOTOR_B_ENABLE_PIN, LOW);
digitalWrite(MOTOR_B_IN1_PIN, HIGH);
digitalWrite(MOTOR_B_IN2_PIN, LOW);
}
void MotorControllerWiringpi::setMotorA(int8_t power)
{
if(power < 0)
{
digitalWrite(MOTOR_A_IN1_PIN, HIGH);
digitalWrite(MOTOR_A_IN2_PIN, LOW);
}
else
{
digitalWrite(MOTOR_A_IN1_PIN, LOW);
digitalWrite(MOTOR_A_IN2_PIN, HIGH);
}
// wiringpi uses a 1024 range for its pwm
// so our 128 range must be multiplied by 8
int32_t pwmPower = 8 * abs(power);
pwmWrite(MOTOR_A_ENABLE_PIN, pwmPower);
}
void MotorControllerWiringpi::setMotorB(int8_t power)
{
if(power < 0)
{
digitalWrite(MOTOR_B_IN1_PIN, HIGH);
digitalWrite(MOTOR_B_IN2_PIN, LOW);
}
else
{
digitalWrite(MOTOR_B_IN1_PIN, LOW);
digitalWrite(MOTOR_B_IN2_PIN, HIGH);
}
// wiringpi uses a 1024 range for its pwm
// so our 128 range must be multiplied by 8
int32_t pwmPower = 8 * abs(power);
pwmWrite(MOTOR_B_ENABLE_PIN, pwmPower);
}