-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaw-servo.cpp
79 lines (59 loc) · 1.25 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
/*
* yaw_servo.cpp
*
* Created on: Jan 1, 2012
* Author: jairo
*/
#include "main.h"
#include "yaw-servo.h"
#include "wirish.h"
HardwareTimer timer2(2);
void yaw_servo_init()
{
timer2.setMode(TIMER_CH4, TIMER_PWM);
timer2.setPrescaleFactor(SERVO_PPM_TIMER_PRESCALE_FACTOR);
set_servo_angle(0.0);
}
float set_servo_angle(float angle)
{
int duty;
// bound angle
if(angle > YAW_SERVO_ANGLE_MAX)
angle = YAW_SERVO_ANGLE_MAX;
if(angle < YAW_SERVO_ANGLE_MIN)
angle = YAW_SERVO_ANGLE_MIN;
duty = SERVO_MIN + (int)(angle * SERVO_ANGLE_TO_DUTY);
if(duty > SERVO_MAX) duty = SERVO_MAX;
pwmWrite(YAW_SERVO_PIN, duty);
return angle;
}
void yaw_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;
float angle = 0;
while (1) {
input = SerialUSB.read();
if (input == 'j')
{
angle -= 1;
}
else if(input == 'k')
{
angle += 1;
}
else if(input == 'z')
{
angle = 0;
}else{
break;
}
SerialUSB.println(set_servo_angle(angle));
delay(20);
}
set_servo_angle(0.0);
}