-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservo.cpp
More file actions
45 lines (30 loc) · 959 Bytes
/
Copy pathservo.cpp
File metadata and controls
45 lines (30 loc) · 959 Bytes
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
// servo.cpp - Servo class
#include "servo.h"
#define NEUTRAL 1500000 // ns pulse width for servo neutral position
#define SIG_FREQ 350 // frequency (Hz) of the pwm signal
#define MAX 2000000 // maximum pulse width in ns
#define MIN 1000000 // minimum pulse width in ms
#define ANGLE 180.0 // the movement angle in degrees
Servo::Servo(void) {
}
/* Servo class constructor
*/
Servo::Servo (PWM pwm_in, int servo_number, string servo_name) {
convert = (MAX - MIN) / ANGLE;
pwm = pwm_in;
servo_no = servo_number;
servo_nm = servo_name;
pwm.set_prd_frq(servo_nm, SIG_FREQ);
pwm.set_duty_ns(servo_nm, NEUTRAL);
pwm.run();
}
/* Sets the joint angle of the given joint
*/
void Servo::set_angle(double angle) {
if (angle < 0 || angle > ANGLE) {
cerr << "Invalid angle into servos.set_angle\n";
exit(EXIT_FAILURE);
}
int ns = MIN + angle * convert;
pwm.set_duty_ns(servo_nm, ns);
}