-
Notifications
You must be signed in to change notification settings - Fork 7
/
DCMotorTest.cpp
77 lines (60 loc) · 1.61 KB
/
DCMotorTest.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
// Compile with
// g++ -lrt -lwiringPi -lpthread -lcrypt -o DCMotorTest PWM.cpp Adafruit_MotorHAT.cpp DCMotorTest.cpp
// Run with
// ./DCMotorTest
#include "Adafruit_MotorHAT.h"
#include "stdio.h"
#include <iostream>
#include <signal.h>
#include <unistd.h>
Adafruit_MotorHAT hat;
void ctrl_c_handler(int s){
std::cout << "Caught signal " << s << std::endl;
hat.resetAll();
exit(1);
}
int main(int argc,char ** argv)
{
if (argc != 2) {
std::cerr << "Not DC index specified!" << std::endl;
exit(-1);
}
signal(SIGINT, ctrl_c_handler);
Adafruit_DCMotor& myMotor = hat.getDC(atoi(argv[1]));
// set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(150);
myMotor.run(FORWARD);
// turn on motor
myMotor.run(RELEASE);
while (true) {
std::cout << "Forward! " << std::endl;
myMotor.run(FORWARD);
std::cout << "\tSpeed up..." << std::endl;
for(int i=0; i <= 255; i++) {
myMotor.setSpeed(i);
usleep(0.01*1000*1000);
}
std::cout << "\tSlow down..." << std::endl;
for(int i=255; i >= 0; i--) {
myMotor.setSpeed(i);
usleep(0.01*1000*1000);
}
std::cout << "Backward! " << std::endl;
myMotor.run(BACKWARD);
std::cout << "\tSpeed up..." << std::endl;
for(int i=0; i <= 255; i++) {
myMotor.setSpeed(i);
usleep(0.01*1000*1000);
}
std::cout << "\tSlow down..." << std::endl;
for(int i=255; i >= 0; i--) {
myMotor.setSpeed(i);
usleep(0.01*1000*1000);
}
std::cout << "Release" << std::endl;
myMotor.run(RELEASE);
usleep(1*1000*1000);
}
// Return value
return 0;
}