-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaw-servo.cpp
90 lines (70 loc) · 1.55 KB
/
yaw-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
/*
* yaw_servo.cpp
*
* Created on: Jan 1, 2012
* Author: jairo
*/
#include "main.h"
#include "yaw-servo.h"
#include "wirish.h"
HardwareTimer timer2(2);
YawServo::YawServo() {
_center = 0;
_offset = 0;
_duty = 0;
_offset_min = 0;
_offset_max = 0;
}
void YawServo::init(float center, float offset_min, float offset_max) {
// set pin mode
timer2.setMode(TIMER_CH4, TIMER_PWM);
// set up timer prescale
timer2.setPrescaleFactor(SERVO_PPM_TIMER_PRESCALE_FACTOR);
// store variables
_center = center;
_offset_min = offset_min;
_offset_max = offset_max;
// move servo to an offest of 0 (middle)
set_offset(0.0);
}
void YawServo::set_offset(float offset) {
offset *= -1;
// bound angle
_offset = constrain(offset, _offset_min, _offset_max);
_duty = SERVO_MIN + (int)( (_center + _offset) * SERVO_ANGLE_TO_DUTY);
_duty = constrain(_duty, SERVO_MIN, SERVO_MAX);
pwmWrite(YAW_SERVO_PIN, _duty);
}
float YawServo::get_offset() {
return _offset;
}
void YawServo::manual_control()
{
SerialUSB.println("Press \'j\' to lower speed.");
SerialUSB.println("Press \'k\' to increase speed.");
SerialUSB.println("Press \'z\' to zero command.");
SerialUSB.println("Any other key zeroes command and exits");
SerialUSB.println();
uint8 input;
while (1) {
input = SerialUSB.read();
if (input == 'j')
{
_offset -= 1;
}
else if(input == 'k')
{
_offset += 1;
}
else if(input == 'z')
{
_offset = 0;
}else{
break;
}
set_offset(_offset);
SerialUSB.println(get_offset());
delay(20);
}
set_offset(0.0);
}