-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (98 loc) · 2.45 KB
/
Copy pathmain.cpp
File metadata and controls
118 lines (98 loc) · 2.45 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bcm2835.h>
#include <csignal>
#include <sys/types.h>
#include <unistd.h>
// include threading library
#include <thread>
#include "motor.cpp"
#include "MPU6050.h"
bool isvalid = true;
//motor is a pointer to be deletable
Motor1 *motor1;
Motor2 *motor2;
void signalHandler(int signum)
{
#ifdef DEBUG
std::cout << "\nsignal " << signum << " triggered" << std::endl;
#endif
//disable motor
bcm2835_gpio_write(ENABLE, HIGH);
// disable the run loop for motors
motor2->valid = false;
motor1->valid = false;
// set isvalid to false
isvalid = false;
#ifdef DEBUG
//std::cout << "Sigterm signal ended" <<std::endl;
#endif
}
void runmotor(Motor *motor)
{
motor->run();
}
int main(int argc, char **argv)
{
std::cout << "Started ...\n";
// print the process id
std::cout << "PID: " << getpid() << std::endl;
if (!bcm2835_init())
return (1);
I2Cdev::initialize();
unsigned int delay = 00;
int steps = 0;
bool dir = true;
//motor is a pointer to be deletable
motor1 = new Motor1();
motor2 = new Motor2();
MPU6050 gyro;
gyro.initialize();
int16_t ax, az;
motor1->dir = dir;
motor2->dir = dir;
//motor1->delay=delay;
//motor2->delay=delay;
// set the signal handler
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
// run the two motors in different threads
std::thread tm1(runmotor, motor1);
std::thread tm2(runmotor, motor2);
for (int i = 10000; i > 12; i *= 0.99)
{
motor1->delay = i;
motor2->delay = i;
// print the delay
printf("delay: %d \r", motor1->delay);
fflush(stdout);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
// if i is lower than 100, wait more
if (i < 80)
{
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
if (!isvalid)
{
break;
}
// print the gyro value
gyro.getAccelerationXZ(&ax, &az);
std::cout << "Gyro: " << ax << " " << az << " " << std::endl;
}
while (isvalid)
{
gyro.getAccelerationXZ(&ax, &az);
printf(" %d \t %d \r", ax, az);
//fflush(stdout);
}
tm1.join();
tm2.join();
/*
runmotor(motor1,isvalid,delay,steps,dir);
isvalid=true;
runmotor(motor2,isvalid,delay,steps,dir);
*/
delete motor1;
delete motor2;
bcm2835_close();
return 0;
}