This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actuator-control.cpp
76 lines (62 loc) · 2.1 KB
/
actuator-control.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
#include <cstdint>
//#include <cunistd>
#include <iostream>
#include <cmath>
#include <chrono>
extern "C" {
#include <roboticscape.h>
#include <rc_usefulincludes.h>
}
#define ENA 1 // servo output 1
#define IN1 113 // gpio pin 3*32 + 17 = 113
#define IN2 116 // gpio pin 3*32 + 20 = 116
// forward declaration
void motor_pulse(int servo, double pulsewidth);
int main()
{
rc_initialize();
rc_set_default_pinmux();
rc_gpio_export(IN1);
rc_gpio_set_dir(IN1, OUTPUT_PIN);
rc_gpio_export(IN2);
rc_gpio_set_dir(IN2, OUTPUT_PIN);
// Basically this program makes the linear actuator oscillate
for (int i = 0; i < 40; ++i) {
std::cout << "Iteration no. " << i << std::endl;
// vary this throughout the program to change the PWM length
double duty = 0.99;
//direction of the pwm trend: true => going down, false => going up
bool dir = true;
while (duty <= 1.0) {
motor_pulse(ENA, duty);
duty = (dir)? duty-0.01 : duty+0.01;
if (duty <= -1.0)
dir = false;
}
}
}
// motor_pulse
// Pulse the motor with PWM fraction of pulsewidth, with -1.0 <= pulsewidth <= 1.0
// assume that "servo" has already been properly initialized
void motor_pulse(int servo, double pulsewidth) {
// each pulse cycle is 0.1s, or 100,000us.
static int TOTAL_DUR_US = 100000;
if (pulsewidth < 0) {
pulsewidth *= -1.0;
rc_gpio_set_value(IN2, HIGH);
rc_gpio_set_value(IN1, LOW);
} else {
rc_gpio_set_value(IN1, HIGH);
rc_gpio_set_value(IN2, LOW);
}
int high_time = int(pulsewidth * double(TOTAL_DUR_US));
// send the pulse!
rc_send_servo_pulse_us(servo, high_time);
//std::chrono::time_point start = std::chrono::system_clock::now();
auto start = std::chrono::system_clock::now();
while (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - start).count() < high_time) // sorry
{/* wait until the servo is done pulsing */}
// signal wire is low now, so delay for the remainder of the cycle
while (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - start).count() < TOTAL_DUR_US) // sorry again
{ /* total time elapsed = 10,000us */ }
}