-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (56 loc) · 1.8 KB
/
main.cpp
File metadata and controls
71 lines (56 loc) · 1.8 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
/*
*/
#include "main.h"
#include "locomotion/cmotion.h"
#include "control/controlthread.h"
// Thread scheduling variables (semaphores, mutexes, etc)
sem_t NewSensorDataSemaphore;
sem_t ControlReadySemaphore;
void createThreadScheduling();
void createControlThread(CMotion* motion);
int main()
{
createThreadScheduling();
CMotion* Motion = new CMotion();
createControlThread(Motion);
return 0;
}
void createThreadScheduling()
{
// create and initialise thread scheduling tools
int result = 0;
result = sem_init(&NewSensorDataSemaphore, 0, 0);
if (result == -1)
{
cout << "MAIN: Failed to initialise NewSensorDataSemaphore. Error code: " << errno << endl;
return;
}
result = sem_init(&ControlReadySemaphore, 0, 1);
if (result == -1)
{
cout << "MAIN: Failed to initialise ControlReadySemaphore. Error code: " << errno << endl;
return;
}
}
void createControlThread(CMotion* motion)
{
// create control thread
cout << "MAIN: Creating controlthread as realtime, with priority " << CONTROLTHREAD_PRIORITY << endl;
int err;
pthread_t controlthread;
err = pthread_create(&controlthread, NULL, runControlThread, motion); // the control thread needs the motion
if (err > 0)
{
cout << "MAIN: Failed to create controlthread. Error code: " << err << endl;
return;
}
sched_param param;
param.sched_priority = CONTROLTHREAD_PRIORITY;
pthread_setschedparam(controlthread, SCHED_FIFO, ¶m);
// double check
int actualpolicy;
sched_param actualparam;
pthread_getschedparam(controlthread, &actualpolicy, &actualparam);
cout << "MAIN: Actual settings for controlthread; Policy: " << actualpolicy << " Priority: " << actualparam.sched_priority << endl;
pthread_join(controlthread, NULL);
}