Subversion Repositories svnkaklik

Rev

Rev 369 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
365 kakl 1
/*****************************************************************************/
2
/*
3
 *  vector.cpp - Control program for Vector robot
4
 *
5
 *      Copyright (C) 2007 KAKL
6
 *
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
9
 *      the Free Software Foundation; either version 2 of the License, or
10
 *      (at your option) any later version.
11
 *
12
 *      This program is distributed in the hope that it will be useful,
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *      GNU General Public License for more details.
16
 *
17
 */
18
/*****************************************************************************/
19
 
20
#include <iostream>
21
#include <getopt.h>
22
#include <errno.h>
23
#include <string.h>
24
#include <pthread.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <unistd.h>
28
#include "linux/i2c-dev.h"
29
#include "linux/i2c.h"
30
#include <sys/ioctl.h>
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
#include "geocalc.h"
369 kakl 35
#include "track.h"
365 kakl 36
 
37
using namespace std;
38
 
39
#define CMPS03_SOFTWARE_REVISION 0x0
40
#define SRF02_SOFTWARE_REVISION 0x0
41
 
42
#define BC_Addr	    0x0B
371 kakl 43
#define US3_Addr	    0x70 // 0xE0 in fact; Sonar na doprovod
367 kakl 44
#define M1	0x50 // 0xA0 in fact
45
#define M2	0x51 // 0xA2 in fact
365 kakl 46
 
367 kakl 47
char vystup[50];
48
pthread_t thread_1, thread_2, thread_3;
365 kakl 49
FILE *pRouraO,*pRouraI;
50
unsigned int vzdalenost;
51
char command,ble;
52
int file;
367 kakl 53
double n, e;
365 kakl 54
 
367 kakl 55
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56
 
365 kakl 57
void *print_tele(void *unused);
367 kakl 58
void *gps(void *unused);
366 kakl 59
//void *sensors(void *unused);
365 kakl 60
 
61
void I2C_addr (int Addr)
62
{
63
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
64
    {
65
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
66
        exit(-5);
67
    }
68
}
69
 
367 kakl 70
void go (int Addr, int speed)
71
{
72
    char Buf[1];
73
 
74
    I2C_addr (Addr);
75
    Buf[0]=speed;
76
    write(file, Buf, 1);
77
}
78
 
371 kakl 79
unsigned int echo(int Addr)
80
{
81
    char Buf[3];
82
 
83
    I2C_addr(Addr);
84
    Buf[0]=0x0;
85
    Buf[1]=0x51;
86
    write(file, Buf, 2);
87
    usleep(80000);
88
    read(file, Buf, 3);
89
    return (Buf[1]*256+Buf[2]);
90
}
91
 
92
void FindNearestCrossing(void)
93
{
94
    int n;
95
 
96
    n=0;
97
    do
98
    {
99
        if (0==cros[n].id) break;
100
        if (GeoCalc::EllipsoidDistance(n, e, cros[n].n, cros[n].e) <= cros[n].dia)
101
        {
102
            #include "nav.h"
103
            break;
104
        }
105
    } while(++n<POINTS);
106
}
107
 
365 kakl 108
int main(int argc, char *argv[], char *envp[])
109
{
371 kakl 110
    int filtr;
111
 
365 kakl 112
	fprintf(stdout, "**** Vector Control Programm ****\n");
113
 
114
	file = open("/dev/i2c-0", O_RDWR);
115
	if (file < 0)
116
	{
117
		cerr << "Could not open /dev/i2c-0." << endl;
118
		return -1;
119
	}
120
 
121
    pthread_create(&thread_1, NULL, print_tele, NULL);
369 kakl 122
    pthread_create(&thread_3, NULL, gps, NULL);
365 kakl 123
//    pthread_create(&thread_2, NULL, sensors, NULL);
124
 
367 kakl 125
    command=0;
371 kakl 126
    filtr=0;
366 kakl 127
 
365 kakl 128
    while(true)
129
    {
367 kakl 130
        switch (command)
366 kakl 131
        {
367 kakl 132
            case 'f':   // forward
369 kakl 133
                go(M1, 70);
134
                go(M2, 70);
367 kakl 135
                command=0;
136
                break;
137
            case 'b':   // backward
369 kakl 138
                go(M1, -70);
139
                go(M2, -70);
367 kakl 140
                command=0;
141
                break;
369 kakl 142
            case 'v':   // stop
367 kakl 143
                go(M1, 0);
144
                go(M2, 0);
145
                command=0;
146
                break;
369 kakl 147
            case 's':   // stop
148
                go(M1, 1);
149
                go(M2, 1);
150
                command=0;
151
                break;
367 kakl 152
            case 'g':
153
usleep(180000);
371 kakl 154
                vzdalenost=echo(US3_Addr);
155
                if ((vzdalenost>60)&&(vzdalenost<80))
367 kakl 156
                {
371 kakl 157
                    go(M1, 60);
158
                    go(M2, 60);
159
                    filtr=0;
367 kakl 160
                    break;
161
                };
371 kakl 162
                if ((vzdalenost>30)&&(vzdalenost<130))
367 kakl 163
                {
371 kakl 164
                    filtr=0;
165
                    if (vzdalenost<55)
367 kakl 166
                    {
371 kakl 167
                        go(M1, 30);
168
                        go(M2, 60);
367 kakl 169
                    }
170
                    else
171
                    {
371 kakl 172
                        go(M1, 60);
173
                        go(M2, 30);
367 kakl 174
                    }
175
                }
176
                else
177
                {
371 kakl 178
                    filtr++;
179
                    if (filtr>5)
180
                    {
181
                        go(M1, 1);  // zastav, neni videt doprovod
182
                        go(M2, 1);
183
                        filtr=6;
184
                    }
367 kakl 185
                };
371 kakl 186
                FindNearestCrossing();
367 kakl 187
                break;
366 kakl 188
        }
365 kakl 189
    };
366 kakl 190
 
365 kakl 191
	close(file);
192
    pthread_join(thread_1, NULL);
193
    pthread_join(thread_2, NULL);
369 kakl 194
    pthread_join(thread_3, NULL);
365 kakl 195
 
196
	return 0;
197
}
198
 
366 kakl 199
 
365 kakl 200
void *print_tele(void *unused)
201
{
367 kakl 202
    char string[2];
366 kakl 203
 
365 kakl 204
    while(true)
205
    {
206
        pRouraI = fopen("/home/ble/pipe","r");
367 kakl 207
        command=fgetc(pRouraI);
208
        string[0]=command;
209
        string[1]=0;
365 kakl 210
        fclose(pRouraI);
211
        pRouraO = fopen("/home/ble/pipe","w");
367 kakl 212
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %s\n",vzdalenost,string);
213
 
214
    pthread_mutex_lock(&mutex);
215
   	fprintf(pRouraO,"%f N %f E\n", n, e);
369 kakl 216
   	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));
367 kakl 218
    pthread_mutex_unlock(&mutex);
219
 
365 kakl 220
        fclose(pRouraO);
221
    }
222
}
223
 
367 kakl 224
void *gps(void *unused)
225
{
226
    FILE *pRS232;
369 kakl 227
    double N, E, pomN, pomE, nn, ee;
367 kakl 228
 
229
    while(true)
230
    {
371 kakl 231
        pRS232 = fopen("/dev/ttyS1","r");
369 kakl 232
        fscanf(pRS232,"$GPGGA,%*f,%lf,N,%lf,E,*", &N, &E); // parser NMEA
371 kakl 233
        fclose(pRS232);
369 kakl 234
        nn=ldiv((long)N,100).quot;   // prepocet DDMM.MM na DD.DD
235
        pomN=(N-nn*100)/60+nn;
236
        ee=ldiv((long)E,100).quot;
237
        pomE=(E-ee*100)/60+ee;
238
        pthread_mutex_lock(&mutex);         // prepis souradnic do sdilenych promennych
367 kakl 239
        n=pomN; e=pomE;
240
        pthread_mutex_unlock(&mutex);
371 kakl 241
        usleep(800000);                     // NMEA nechodi castejc nez 1x za 1s
367 kakl 242
    }
243
}
244
 
366 kakl 245
/*
365 kakl 246
void *sensors(void *unused)
247
{
248
	char Buf[64];
249
 
250
    while(true)
251
    {
252
        I2C_addr(US_Addr);
253
        Buf[0]=0x0;
254
        Buf[1]=0x51;
255
        write(file, Buf, 2);
256
        usleep(80000);
257
        read(file, Buf, 3);
258
        vzdalenost=(Buf[1]*256+Buf[2]);
259
        usleep(300000);
260
 
261
        I2C_addr(PIC_Addr);
262
        Buf[0]=command;
263
        write(file, Buf, 1);
264
        read(file, Buf, 1);
265
        ble=Buf[0];
266
    }
267
}
366 kakl 268
*/