-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motor.cpp
133 lines (114 loc) · 3.46 KB
/
Motor.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* File: Motor.cpp
* Author: josh
*
* Created on March 11, 2020, 9:30 PM
*/
#include "Motor.h"
#define DEBUG
Motor::Motor(
int DrivePWMPin,
int DriveIn1Pin,
int DriveIn2Pin,
int SteeringPWMPin,
const unsigned short minInput,
const unsigned short maxInput) {
GPIO::setmode(GPIO::BOARD);
GPIO::setup(DrivePWMPin, GPIO::OUT, GPIO::HIGH);
drive_pwm = std::make_shared<GPIO::PWM>(DrivePWMPin, 50);
GPIO::setup(SteeringPWMPin, GPIO::OUT, GPIO::HIGH);
steer_pwm = std::make_shared<GPIO::PWM>(SteeringPWMPin, 1500);
drive = std::make_unique<L298N_Jetson> (drive_pwm,
DriveIn1Pin,
DriveIn2Pin,
true,
MIN_THROTTLE,
MAX_THROTTLE);
steer = std::make_unique<Servo_Jetson> (steer_pwm,
1000, 2000,
minInput, maxInput, true);
currentSpeed = 0;
drive->setSpeed(currentSpeed);
currentDirection = STOP;
}
Motor::~Motor() {
this->steer_pwm->stop();
this->drive_pwm->stop();
GPIO::cleanup();
}
short Motor::setDriveSpeed(const unsigned short speed) {
// currentSpeed = map(speed, MIN_TURN, MAX_TURN, CENTER_STEER, MAX_LEFT_STEERING_ANGLE);
currentSpeed = speed;
#ifdef DEBUG
std::cout << "Motor Driving Speed: " << speed << std::endl;
#endif
drive->setSpeed(speed);
drive->run();
}
void Motor::run(Direction d) {
currentDirection = d;
switch(currentDirection){
case FORWARD:
drive->forward();
break;
case BACKWARD:
drive->backward();
break;
case STOP:
drive->stop();
break;
};
drive->run();
}
void Motor::run() {
drive->run();
}
void Motor::stop() {
drive->stop();
}
void Motor::setDirection(Direction d) {
currentDirection = d;
switch(currentDirection){
case FORWARD:{
drive->forward();
}
break;
case BACKWARD:{
drive->backward();
}
break;
case STOP:{
drive->stop();
}
break;
};
drive->run();
}
void Motor::turnRight(short angle) {
unsigned short turn_val = map(angle, MIN_TURN, MAX_TURN, CENTER_STEER, MAX_RIGHT_STEERING_ANGLE);
steer->writeMappedValue(turn_val);
#ifdef DEBUG
std::cout << "Turning value commanded: " << angle << " mapped value: " << turn_val << std::endl;
#endif
}
void Motor::turnLeft(short angle) {
unsigned short turn_val = map(angle, MIN_TURN, MAX_TURN, CENTER_STEER, MAX_LEFT_STEERING_ANGLE);
steer->writeMappedValue(turn_val);
#ifdef DEBUG
std::cout << "Turning value commanded: " << angle << " mapped value: " << turn_val << std::endl;
#endif
}
void Motor::turnAbsolute(short angle) {
#ifdef DEBUG
std::cout << "Turning value commanded: " << angle << std::endl;
#endif
steer->writeMappedValue(angle);
}
unsigned short Motor::map(const unsigned short val, const unsigned short in_min, const unsigned short in_max,
const unsigned short out_min, const unsigned short out_max) {
if ((val <= in_max) && (val >= in_min)) {
{
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
} else return 0;
}