-
Notifications
You must be signed in to change notification settings - Fork 0
/
Servo.cpp
103 lines (88 loc) · 3.39 KB
/
Servo.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
/*
* MIT License
*/
/*
* File: Servo.cpp
* Author: josh - JKI757
*
* Created on May 24, 2020, 12:21 AM
*/
#include "Servo.hpp"
#define DEBUG
Servo_Jetson::Servo_Jetson() = default;
Servo_Jetson::~Servo_Jetson() {
this->Steer_PWM->stop();
if (this->setup){
GPIO::cleanup();
}
}
Servo_Jetson::Servo_Jetson(int pin){
GPIO::setmode(GPIO::BOARD);
GPIO::setup(pin, GPIO::OUT, GPIO::HIGH);
this->Steer_PWM = std::make_shared<GPIO::PWM>(pin, 1500);
//1500 is centered for a normal steering servo for RC car
this->minUs = 1000;
this->maxUs = 2000;
this->setup = true;
}
Servo_Jetson::Servo_Jetson(std::shared_ptr<GPIO::PWM> steer, bool setup){
this->Steer_PWM = steer;
this->minUs = 1000;
this->maxUs = 2000;
this->minInput = 0;
this->maxInput = 255;
this->setup = false;
}
Servo_Jetson::Servo_Jetson(int pin, const unsigned short minUs, const unsigned short maxUs){
GPIO::setmode(GPIO::BOARD);
GPIO::setup(pin, GPIO::OUT, GPIO::HIGH);
this->Steer_PWM = std::make_shared<GPIO::PWM>(pin, 1500);
//1500 is centered for a normal steering servo for RC car
this->minUs = minUs;
this->maxUs = maxUs;
this->setup = true;
}
Servo_Jetson::Servo_Jetson(int pin, const unsigned short minUs, const unsigned short maxUs,
const unsigned short minInput, const unsigned short maxInput){
//mapMin and mapMax are the expected range of values coming in as commands
//if you use these values, use the writeMappedValue method with a value in the range (mapMin, mapMax)
//and the value will be mapped to (minUs, maxUs)
GPIO::setmode(GPIO::BOARD);
GPIO::setup(pin, GPIO::OUT, GPIO::HIGH);
this->Steer_PWM = std::make_shared<GPIO::PWM>(pin, 1500);
//1500 is centered for a normal steering servo for RC car
this->minUs = minUs;
this->maxUs = maxUs;
this->minInput = minInput;
this->maxInput = maxInput;
this->setup = true;
}
Servo_Jetson::Servo_Jetson(std::shared_ptr<GPIO::PWM> steer, const unsigned short minUs, const unsigned short maxUs, const unsigned short minInput, const unsigned short maxInput, bool setup){
//mapMin and mapMax are the expected range of values coming in as commands
//if you use these values, use the writeMappedValue method with a value in the range (mapMin, mapMax)
//and the value will be mapped to (minUs, maxUs)
this->Steer_PWM = steer;
//1500 is centered for a normal steering servo for RC car
this->minUs = minUs;
this->maxUs = maxUs;
this->minInput = minInput;
this->maxInput = maxInput;
this->setup = false;
}
unsigned short Servo_Jetson::map(const unsigned short val){
if ((val <= maxInput) && (val >= minInput) && (maxInput != minInput)) {
return (float)((float)(val - minInput) * (float)(maxUs - minUs) / (float)(maxInput - minInput) + minUs);
} else return 0;
}
void Servo_Jetson::writeMappedValue(const short val){
#ifdef DEBUG
std::cout << "value passed into write mapped value: " << val << std::endl;
#endif
//takes a value mapped between minRange and maxRange to minUs and maxUs and calculates a microsecond value from there
unsigned short mappedVal = map(val);
#ifdef DEBUG
std::cout << "mapped value: " << mappedVal << std::endl;
#endif
// this->Steer_PWM->ChangeFrequency(mappedVal);
this->Steer_PWM->start(1000000.0/mappedVal, 20);
}