Subversion Repositories svnkaklik

Rev

Rev 371 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 371 Rev 372
1
/*****************************************************************************/
1
/*****************************************************************************/
2
/*
2
/*
3
 *  vector.cpp - Control program for Vector robot
3
 *  vector.cpp - Control program for Vector robot
4
 *
4
 *
5
 *      Copyright (C) 2007 KAKL
5
 *      Copyright (C) 2007 KAKL
6
 *
6
 *
7
 *      This program is free software; you can redistribute it and/or modify
7
 *      This program is free software; you can redistribute it and/or modify
8
 *      it under the terms of the GNU General Public License as published by
8
 *      it under the terms of the GNU General Public License as published by
9
 *      the Free Software Foundation; either version 2 of the License, or
9
 *      the Free Software Foundation; either version 2 of the License, or
10
 *      (at your option) any later version.
10
 *      (at your option) any later version.
11
 *
11
 *
12
 *      This program is distributed in the hope that it will be useful,
12
 *      This program is distributed in the hope that it will be useful,
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *      GNU General Public License for more details.
15
 *      GNU General Public License for more details.
16
 *
16
 *
17
 */
17
 */
18
/*****************************************************************************/
18
/*****************************************************************************/
19
 
19
 
20
#include <iostream>
20
#include <iostream>
21
#include <getopt.h>
21
#include <getopt.h>
22
#include <errno.h>
22
#include <errno.h>
23
#include <string.h>
23
#include <string.h>
24
#include <pthread.h>
24
#include <pthread.h>
25
#include <stdio.h>
25
#include <stdio.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
#include <unistd.h>
27
#include <unistd.h>
28
#include "linux/i2c-dev.h"
28
#include "linux/i2c-dev.h"
29
#include "linux/i2c.h"
29
#include "linux/i2c.h"
30
#include <sys/ioctl.h>
30
#include <sys/ioctl.h>
31
#include <sys/types.h>
31
#include <sys/types.h>
32
#include <sys/stat.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
33
#include <fcntl.h>
34
#include "geocalc.h"
34
#include "geocalc.h"
35
#include "track.h"
35
#include "track.h"
36
 
36
 
37
using namespace std;
37
using namespace std;
38
 
38
 
39
#define CMPS03_SOFTWARE_REVISION 0x0
-
 
40
#define SRF02_SOFTWARE_REVISION 0x0
-
 
41
 
39
 
42
#define BC_Addr	    0x0B
40
#define BC_Addr	    0x0B
43
#define US3_Addr	    0x70 // 0xE0 in fact; Sonar na doprovod
41
#define US3_Addr	0x70    // 0xE0 in fact; Sonar na doprovod
-
 
42
#define CMPS_Addr   0x60    // 0xC0
44
#define M1	0x50 // 0xA0 in fact
43
#define M1	0x50 // 0xA0 in fact
45
#define M2	0x51 // 0xA2 in fact
44
#define M2	0x51 // 0xA2 in fact
46
 
45
 
-
 
46
#define SEVER 0
-
 
47
 
47
char vystup[50];
48
char vystup[50];
48
pthread_t thread_1, thread_2, thread_3;
49
pthread_t thread_1, thread_2, thread_3;
49
FILE *pRouraO,*pRouraI;
50
FILE *pRouraO,*pRouraI;
50
unsigned int vzdalenost;
51
unsigned int vzdalenost;
51
char command,ble;
52
char command,ble;
52
int file;
53
int file;
53
double n, e;
54
double n, e;
-
 
55
unsigned char azimut_mag;
-
 
56
 
54
 
57
 
55
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
58
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56
 
59
 
57
void *print_tele(void *unused);
60
void *print_tele(void *unused);
58
void *gps(void *unused);
61
void *gps(void *unused);
59
//void *sensors(void *unused);
62
//void *sensors(void *unused);
60
 
63
 
61
void I2C_addr (int Addr)
64
void I2C_addr (int Addr)
62
{
65
{
63
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
66
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
64
    {
67
    {
65
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
68
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
66
        exit(-5);
69
        exit(-5);
67
    }
70
    }
68
}
71
}
69
 
72
 
70
void go (int Addr, int speed)
73
void go (int Addr, int speed) // nastavi rychlost motoru
71
{
74
{
72
    char Buf[1];
75
    char Buf[1];
73
 
76
 
74
    I2C_addr (Addr);
77
    I2C_addr (Addr);
75
    Buf[0]=speed;
78
    Buf[0]=speed;
76
    write(file, Buf, 1);
79
    write(file, Buf, 1);
77
}
80
}
78
 
81
 
79
unsigned int echo(int Addr)
82
unsigned int echo(int Addr)  // precte vzdalenost z US cidla
80
{
83
{
81
    char Buf[3];
84
    char Buf[3];
82
 
85
 
83
    I2C_addr(Addr);
86
    I2C_addr(Addr);
84
    Buf[0]=0x0;
87
    Buf[0]=0x0;
85
    Buf[1]=0x51;
88
    Buf[1]=0x51;
86
    write(file, Buf, 2);
89
    write(file, Buf, 2);
87
    usleep(80000);
90
    usleep(80000);
88
    read(file, Buf, 3);
91
    read(file, Buf, 3);
89
    return (Buf[1]*256+Buf[2]);
92
    return (Buf[1]*256+Buf[2]);
90
}
93
}
91
 
94
 
92
void FindNearestCrossing(void)
95
void FindNearestCrossing(void)
93
{
96
{
94
    int n;
97
    int n;
95
 
98
 
96
    n=0;
99
    n=0;
97
    do
100
    do
98
    {
101
    {
99
        if (0==cros[n].id) break;
102
        if (0==cros[n].id) break;
100
        if (GeoCalc::EllipsoidDistance(n, e, cros[n].n, cros[n].e) <= cros[n].dia)
103
        if (GeoCalc::EllipsoidDistance(n, e, cros[n].n, cros[n].e) <= cros[n].dia)
101
        {
104
        {
102
            #include "nav.h"
105
            #include "nav.h"
103
            break;
106
            break;
104
        }
107
        }
105
    } while(++n<POINTS);
108
    } while(++n<POINTS);
106
}
109
}
107
 
110
 
108
int main(int argc, char *argv[], char *envp[])
111
int i2c_init()   // zinicializuje i2c
109
{
112
{
110
    int filtr;
-
 
111
 
-
 
112
	fprintf(stdout, "**** Vector Control Programm ****\n");
-
 
113
 
-
 
114
	file = open("/dev/i2c-0", O_RDWR);
113
	file = open("/dev/i2c-0", O_RDWR);
115
	if (file < 0)
114
	if (file < 0)
116
	{
115
	{
117
		cerr << "Could not open /dev/i2c-0." << endl;
116
		cerr << "Could not open /dev/i2c-0." << endl;
118
		return -1;
117
		return -1;
119
	}
118
	}
-
 
119
	return 0;
-
 
120
}
-
 
121
 
-
 
122
int read_azimut_mag()  // precte azimut z kompasu
-
 
123
{
-
 
124
char Buf[3];      // promena pro manipulaci s i2c
-
 
125
    I2C_addr(CMPS_Addr);
-
 
126
    Buf[0]=1;
-
 
127
    write(file,Buf,1);
-
 
128
    read(file, Buf,1);
-
 
129
    return Buf[0];
-
 
130
}
-
 
131
 
-
 
132
void turnL(unsigned char angle, signed char speed)    // otoci robota o zadany uhel
-
 
133
{
-
 
134
int azimut;
-
 
135
 
-
 
136
    go(M1, speed);
-
 
137
    go(M2, -speed);
-
 
138
    azimut=read_azimut_mag();
-
 
139
    while(read_azimut_mag() >= (azimut + angle)) usleep(10000);
-
 
140
    go(M1, 0);
-
 
141
    go(M2, 0);
-
 
142
    command=0;
-
 
143
}
-
 
144
 
-
 
145
void turnR()    // otoci robota o zadany uhel
-
 
146
{
-
 
147
}
-
 
148
 
-
 
149
int main(int argc, char *argv[], char *envp[])
-
 
150
{
-
 
151
    int filtr;
-
 
152
    signed char test;
-
 
153
 
-
 
154
	fprintf(stdout, "\n **** Starting Vector Control Programm **** \n \r");
-
 
155
 
-
 
156
    i2c_init();
120
 
157
 
121
    pthread_create(&thread_1, NULL, print_tele, NULL);
158
    pthread_create(&thread_1, NULL, print_tele, NULL);
122
    pthread_create(&thread_3, NULL, gps, NULL);
159
    pthread_create(&thread_3, NULL, gps, NULL);
123
//    pthread_create(&thread_2, NULL, sensors, NULL);
160
//    pthread_create(&thread_2, NULL, sensors, NULL);
124
 
161
 
125
    command=0;
162
    command=0;
126
    filtr=0;
163
    filtr=0;
127
 
164
 
128
    while(true)
165
    while(true)
129
    {
166
    {
130
        switch (command)
167
        switch (command)
131
        {
168
        {
132
            case 'f':   // forward
169
            case 'f':   // forward
133
                go(M1, 70);
170
                go(M1, 70);
134
                go(M2, 70);
171
                go(M2, 70);
135
                command=0;
172
                command=0;
136
                break;
173
                break;
137
            case 'b':   // backward
174
            case 'b':   // backward
138
                go(M1, -70);
175
                go(M1, -70);
139
                go(M2, -70);
176
                go(M2, -70);
140
                command=0;
177
                command=0;
141
                break;
178
                break;
-
 
179
            case 'v':   // volnobeh
-
 
180
                go(M1, -128);
-
 
181
                go(M2, -128);
-
 
182
                command=0;
-
 
183
                break;
142
            case 'v':   // stop
184
            case 's':   // stop
143
                go(M1, 0);
185
                go(M1, 0);
144
                go(M2, 0);
186
                go(M2, 0);
145
                command=0;
187
                command=0;
146
                break;
188
                break;
147
            case 's':   // stop
189
            case 'a':   // test otaceni
-
 
190
                go(M1, 100);
-
 
191
                go(M2, -100);
-
 
192
                azimut_mag=read_azimut_mag();
-
 
193
                while(read_azimut_mag() >= (azimut_mag + 45)) usleep(10000);
148
                go(M1, 1);
194
                go(M1, 0);
149
                go(M2, 1);
195
                go(M2, 0);
150
                command=0;
196
                command=0;
151
                break;
197
                break;
-
 
198
 
-
 
199
            case 't':   // test
-
 
200
                for(test=0;test<127;test++)
-
 
201
                {
-
 
202
                    go(M1, test);
-
 
203
                    go(M2, test);
-
 
204
                    usleep(10000);
-
 
205
                };
-
 
206
                go(M1, 127);
-
 
207
                go(M2, 127);
-
 
208
                for(test=127;test>-128;test--)
-
 
209
                {
-
 
210
                    go(M1, test);
-
 
211
                    go(M2, test);
-
 
212
                    usleep(10000);
-
 
213
                };
-
 
214
                go(M1, -128);
-
 
215
                go(M2, -128);
-
 
216
                command=0;
-
 
217
                break;
-
 
218
 
152
            case 'g':
219
            case 'g':
153
usleep(180000);
220
    usleep(180000);  // simulace ostatnich  cidel (zdrzeni)
-
 
221
                azimut_mag=read_azimut_mag();
-
 
222
 
-
 
223
                FindNearestCrossing();
-
 
224
 
154
                vzdalenost=echo(US3_Addr);
225
                vzdalenost=echo(US3_Addr);
155
                if ((vzdalenost>60)&&(vzdalenost<80))
226
                if ((vzdalenost>60)&&(vzdalenost<80))
156
                {
227
                {
157
                    go(M1, 60);
228
                    go(M1, 60);
158
                    go(M2, 60);
229
                    go(M2, 60);
159
                    filtr=0;
230
                    filtr=0;
160
                    break;
231
                    break;
161
                };
232
                };
162
                if ((vzdalenost>30)&&(vzdalenost<130))
233
                if ((vzdalenost>30)&&(vzdalenost<130))
163
                {
234
                {
164
                    filtr=0;
235
                    filtr=0;
165
                    if (vzdalenost<55)
236
                    if (vzdalenost<55)
166
                    {
237
                    {
167
                        go(M1, 30);
238
                        go(M1, 30);
168
                        go(M2, 60);
239
                        go(M2, 60);
169
                    }
240
                    }
170
                    else
241
                    else
171
                    {
242
                    {
172
                        go(M1, 60);
243
                        go(M1, 60);
173
                        go(M2, 30);
244
                        go(M2, 30);
174
                    }
245
                    }
175
                }
246
                }
176
                else
247
                else
177
                {
248
                {
178
                    filtr++;
249
                    filtr++;
179
                    if (filtr>5)
250
                    if (filtr>5)
180
                    {
251
                    {
181
                        go(M1, 1);  // zastav, neni videt doprovod
252
                        go(M1, 1);  // zastav, neni videt doprovod
182
                        go(M2, 1);
253
                        go(M2, 1);
183
                        filtr=6;
254
                        filtr=6;
184
                    }
255
                    }
185
                };
256
                };
186
                FindNearestCrossing();
-
 
187
                break;
257
                break;
188
        }
258
        }
189
    };
259
    };
190
 
260
 
191
	close(file);
261
	close(file);
192
    pthread_join(thread_1, NULL);
262
    pthread_join(thread_1, NULL);
193
    pthread_join(thread_2, NULL);
263
    pthread_join(thread_2, NULL);
194
    pthread_join(thread_3, NULL);
264
    pthread_join(thread_3, NULL);
195
 
265
 
196
	return 0;
266
	return 0;
197
}
267
}
198
 
268
 
199
 
269
 
200
void *print_tele(void *unused)
270
void *print_tele(void *unused)
201
{
271
{
202
    char string[2];
272
    char string[2];
203
 
273
 
204
    while(true)
274
    while(true)
205
    {
275
    {
206
        pRouraI = fopen("/home/ble/pipe","r");
276
        pRouraI = fopen("/home/ble/pipe","r");
207
        command=fgetc(pRouraI);
277
        command=fgetc(pRouraI);
208
        string[0]=command;
278
        string[0]=command;
209
        string[1]=0;
279
        string[1]=0;
210
        fclose(pRouraI);
280
        fclose(pRouraI);
211
        pRouraO = fopen("/home/ble/pipe","w");
281
        pRouraO = fopen("/home/ble/pipe","w");
212
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %s\n",vzdalenost,string);
282
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %s\n",vzdalenost,string);
213
 
283
 
214
    pthread_mutex_lock(&mutex);
284
    pthread_mutex_lock(&mutex);
215
   	fprintf(pRouraO,"%f N %f E\n", n, e);
285
   	fprintf(pRouraO,"%f N %f E\n", n, e);
216
   	fprintf(pRouraO,"Vzdalenost: %.1f m\n", GeoCalc::EllipsoidDistance(n, e, cros[1].n, cros[1].e));
286
   	fprintf(pRouraO,"Vzdalenost: %.1f m\n", GeoCalc::EllipsoidDistance(n, e, cros[1].n, cros[1].e));
217
	fprintf(pRouraO,"Azimut: %.2f Deg\n", GeoCalc::GCAzimuth(n, e, cros[1].n, cros[1].e));
287
	fprintf(pRouraO,"Azimut: %.2f Deg\n", GeoCalc::GCAzimuth(n, e, cros[1].n, cros[1].e));
-
 
288
	fprintf(pRouraO,"AzimutMag: %d (0-255)\n", azimut_mag);
218
    pthread_mutex_unlock(&mutex);
289
    pthread_mutex_unlock(&mutex);
219
 
290
 
220
        fclose(pRouraO);
291
        fclose(pRouraO);
221
    }
292
    }
222
}
293
}
223
 
294
 
224
void *gps(void *unused)
295
void *gps(void *unused)
225
{
296
{
226
    FILE *pRS232;
297
    FILE *pRS232;
227
    double N, E, pomN, pomE, nn, ee;
298
    double N, E, pomN, pomE, nn, ee;
228
 
299
 
229
    while(true)
300
    while(true)
230
    {
301
    {
231
        pRS232 = fopen("/dev/ttyS1","r");
302
        pRS232 = fopen("/dev/ttyS1","r");
232
        fscanf(pRS232,"$GPGGA,%*f,%lf,N,%lf,E,*", &N, &E); // parser NMEA
303
        fscanf(pRS232,"$GPGGA,%*f,%lf,N,%lf,E,*", &N, &E); // parser NMEA
233
        fclose(pRS232);
304
        fclose(pRS232);
234
        nn=ldiv((long)N,100).quot;   // prepocet DDMM.MM na DD.DD
305
        nn=ldiv((long)N,100).quot;   // prepocet DDMM.MM na DD.DD
235
        pomN=(N-nn*100)/60+nn;
306
        pomN=(N-nn*100)/60+nn;
236
        ee=ldiv((long)E,100).quot;
307
        ee=ldiv((long)E,100).quot;
237
        pomE=(E-ee*100)/60+ee;
308
        pomE=(E-ee*100)/60+ee;
238
        pthread_mutex_lock(&mutex);         // prepis souradnic do sdilenych promennych
309
        pthread_mutex_lock(&mutex);         // prepis souradnic do sdilenych promennych
239
        n=pomN; e=pomE;
310
        n=pomN; e=pomE;
240
        pthread_mutex_unlock(&mutex);
311
        pthread_mutex_unlock(&mutex);
241
        usleep(800000);                     // NMEA nechodi castejc nez 1x za 1s
312
        usleep(800000);                     // NMEA nechodi castejc nez 1x za 1s
242
    }
313
    }
243
}
314
}
244
 
315
 
245
/*
316
/*
246
void *sensors(void *unused)
317
void *sensors(void *unused)
247
{
318
{
248
	char Buf[64];
319
	char Buf[64];
249
 
320
 
250
    while(true)
321
    while(true)
251
    {
322
    {
252
        I2C_addr(US_Addr);
323
        I2C_addr(US_Addr);
253
        Buf[0]=0x0;
324
        Buf[0]=0x0;
254
        Buf[1]=0x51;
325
        Buf[1]=0x51;
255
        write(file, Buf, 2);
326
        write(file, Buf, 2);
256
        usleep(80000);
327
        usleep(80000);
257
        read(file, Buf, 3);
328
        read(file, Buf, 3);
258
        vzdalenost=(Buf[1]*256+Buf[2]);
329
        vzdalenost=(Buf[1]*256+Buf[2]);
259
        usleep(300000);
330
        usleep(300000);
260
 
331
 
261
        I2C_addr(PIC_Addr);
332
        I2C_addr(PIC_Addr);
262
        Buf[0]=command;
333
        Buf[0]=command;
263
        write(file, Buf, 1);
334
        write(file, Buf, 1);
264
        read(file, Buf, 1);
335
        read(file, Buf, 1);
265
        ble=Buf[0];
336
        ble=Buf[0];
266
    }
337
    }
267
}
338
}
268
*/
339
*/