-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServo.cpp
114 lines (98 loc) · 4.03 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
104
105
106
107
108
109
110
111
112
113
114
/*
* MIT License
*/
/*
* File: Servo.cpp
* Author: josh - JKI757
*
* Created on May 24, 2020, 12:21 AM
*/
#include "Servo.hpp"
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->mapMin = 0;
this->mapMax = 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 mapMin, const unsigned short mapMax){
//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->mapMin = mapMin;
this->mapMax = mapMax;
this->setup = true;
}
Servo_Jetson::Servo_Jetson(std::shared_ptr<GPIO::PWM> steer, const unsigned short minUs, const unsigned short maxUs, const unsigned short mapMin, const unsigned short mapMax, 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->mapMin = mapMin;
this->mapMax = mapMax;
this->setup = false;
}
void Servo_Jetson::writeAngle(const unsigned short angle){\
const unsigned char mappedVal = mapAngle(angle);
this->Steer_PWM->ChangeFrequency(1.0/mappedVal);
this->Steer_PWM->start(.5);
}
void Servo_Jetson::writeUs(const unsigned short microseconds){
this->Steer_PWM->ChangeFrequency(1.0/microseconds);
this->Steer_PWM->start(.5);
}
unsigned short Servo_Jetson::map(const short val){
if ((val <= mapMax) && (val >= mapMin) && (mapMax != mapMin)) {
return round( (float)minUs + ((float)(maxUs - minUs) / (float)(mapMax - mapMin)) * (float)(val - mapMin) );
// return round(((float) val - (float) mapMin) / ((float) mapMax - (float) mapMin) * ((float) minUs - (float) maxUs) + (float) mapMin);
} else return -1;
}
unsigned short Servo_Jetson::mapAngle(const short val){
if ((val <= 360) && (val >= 0)) {\
return round(((float) val - (float) 0) / ((float) 360 - (float) 0) * ((float) minUs - (float) maxUs) + (float) minUs);
} else return -1;
}
void Servo_Jetson::writeMappedValue(const short val){
std::cout << "value passed into write mapped value: " << val << std::endl;
//takes a value mapped between minRange and maxRange to minUs and maxUs and calculates a microsecond value from there
unsigned short mappedVal = map(val);
std::cout << "mapped value: " << mappedVal << std::endl;
this->Steer_PWM->ChangeFrequency(mappedVal);
this->Steer_PWM->start(.5);
}