/
main.c
executable file
·144 lines (112 loc) · 2.29 KB
/
main.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdint.h>
#include <pthread.h>
#include "depth_regions.c"
#include "simpletest.c"
#include "roombalib.c"
uint8_t* obstacleGrid;
#define N_COLS (W / REGION_RES)
#define N_ROWS (H / REGION_RES)
int main(int argc, char* argv[]) {
//printf("yes links!\n");
//printf("%d %d\n", N_COLS, N_ROWS);
int ret;
Roomba* roomba = roomba_init(argv[1]);
ret = 0;
ret = pthread_mutex_init(lock, NULL);
if (ret)
{
perror("mutex init");
_exit(1);
}
ret = 0;
pthread_t texc_cmd;
ret = pthread_create(&texc_cmd, NULL, exc_cmd, (void*)roomba);
if (ret)
{
perror("create exc task");
_exit(1);
}
while(1)
{
obstacleGrid = getRegionObstacles();
uint8_t obstacle = 0;
char direction = '-';
uint8_t columns[N_COLS];
for (int i = 0; i < N_COLS; i++)
{
columns[i] = 0;
}
for (int i = 0; i < N_COLS*N_ROWS; i++)
{
//printf("%d",obstacleGrid[i]);
if (obstacleGrid[i])
{
columns[i % N_COLS] = 1;
obstacle = 1;
}
}
//printf("\t");
for (int i = 0; i < N_COLS; i++)
{
printf("%d", columns[i]);
}
printf("\t");
if (obstacle)
{
int sum = 0, count = 0;
for (int i = 0; i < N_COLS; i++) {
if(columns[i])
{
sum += i; count++;
}
}
double weight = sum/count;
//printf("weight: %d %d %f\t", sum, count, (double)(sum/count));
printf("warning, ");
if (weight > 3.5)
{
printf("obstacle RIGHT");
direction = 'L';
}
else
{
printf("obstacle LEFT");
direction = 'R';
}
printf("\n");
}
else
{
printf("Proceed...\n");
}
free(obstacleGrid);
/* process state and decide on command to send */
pthread_mutex_lock(lock);
/*
double x,y,theta;
x = pos.x;
y = pos.y;
theta = pos.theta;
*/
if (!obstacle)
{
cmd.command = 'w';
}
else
{
if (direction == 'R')
{
cmd.command = 'd';
}
else
{
cmd.command = 'a';
}
}
pthread_mutex_unlock(lock);
}
pthread_join(texc_cmd, NULL);
roomba_close( roomba );
roomba_free( roomba );
return 0;
}