Subversion Repositories svnkaklik

Rev

Rev 367 | 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
43
#define US_Addr	    0x70 // 0xE0 in fact
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
 
365 kakl 79
int main(int argc, char *argv[], char *envp[])
80
{
81
	fprintf(stdout, "**** Vector Control Programm ****\n");
82
 
83
	file = open("/dev/i2c-0", O_RDWR);
84
	if (file < 0)
85
	{
86
		cerr << "Could not open /dev/i2c-0." << endl;
87
		return -1;
88
	}
89
 
90
    pthread_create(&thread_1, NULL, print_tele, NULL);
369 kakl 91
    pthread_create(&thread_3, NULL, gps, NULL);
365 kakl 92
//    pthread_create(&thread_2, NULL, sensors, NULL);
93
 
366 kakl 94
    char Buf[64];
367 kakl 95
    command=0;
366 kakl 96
 
365 kakl 97
    while(true)
98
    {
367 kakl 99
        switch (command)
366 kakl 100
        {
367 kakl 101
            case 'f':   // forward
369 kakl 102
                go(M1, 70);
103
                go(M2, 70);
367 kakl 104
                command=0;
105
                break;
106
            case 'b':   // backward
369 kakl 107
                go(M1, -70);
108
                go(M2, -70);
367 kakl 109
                command=0;
110
                break;
369 kakl 111
            case 'v':   // stop
367 kakl 112
                go(M1, 0);
113
                go(M2, 0);
114
                command=0;
115
                break;
369 kakl 116
            case 's':   // stop
117
                go(M1, 1);
118
                go(M2, 1);
119
                command=0;
120
                break;
367 kakl 121
            case 'g':
122
usleep(180000);
123
                I2C_addr(US_Addr);
124
                Buf[0]=0x0;
125
                Buf[1]=0x51;
126
                write(file, Buf, 2);
127
                usleep(80000);
128
                read(file, Buf, 3);
129
                vzdalenost=(Buf[1]*256+Buf[2]);
130
                if ((vzdalenost>50)&&(vzdalenost<80))
131
                {
132
                    go(M1, 20);
133
                    go(M2, 20);
134
                    break;
135
                };
136
                if ((vzdalenost>30)&&(vzdalenost<120))
137
                {
138
                    if (vzdalenost<50)
139
                    {
140
                        go(M1, 20);
141
                        go(M2, 40);
142
                    }
143
                    else
144
                    {
145
                        go(M1, 40);
146
                        go(M2, 20);
147
                    }
148
                }
149
                else
150
                {
151
                    go(M1, 0);  // zastav, neni videt doprovod
152
                    go(M2, 0);
153
                };
154
                break;
366 kakl 155
        }
365 kakl 156
    };
366 kakl 157
 
365 kakl 158
	close(file);
159
    pthread_join(thread_1, NULL);
160
    pthread_join(thread_2, NULL);
369 kakl 161
    pthread_join(thread_3, NULL);
365 kakl 162
 
163
	return 0;
164
}
165
 
366 kakl 166
 
365 kakl 167
void *print_tele(void *unused)
168
{
367 kakl 169
    char string[2];
366 kakl 170
 
365 kakl 171
    while(true)
172
    {
173
        pRouraI = fopen("/home/ble/pipe","r");
367 kakl 174
        command=fgetc(pRouraI);
175
        string[0]=command;
176
        string[1]=0;
365 kakl 177
        fclose(pRouraI);
178
        pRouraO = fopen("/home/ble/pipe","w");
367 kakl 179
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %s\n",vzdalenost,string);
180
 
181
    pthread_mutex_lock(&mutex);
182
   	fprintf(pRouraO,"%f N %f E\n", n, e);
369 kakl 183
   	fprintf(pRouraO,"Vzdalenost: %.1f m\n", GeoCalc::EllipsoidDistance(n, e, cros[1].n, cros[1].e));
184
	fprintf(pRouraO,"Azimut: %.2f Deg\n", GeoCalc::GCAzimuth(n, e, cros[1].n, cros[1].e));
367 kakl 185
    pthread_mutex_unlock(&mutex);
186
 
365 kakl 187
        fclose(pRouraO);
188
    }
189
}
190
 
367 kakl 191
void *gps(void *unused)
192
{
193
    FILE *pRS232;
369 kakl 194
    double N, E, pomN, pomE, nn, ee;
367 kakl 195
 
196
    pRS232 = fopen("/dev/ttyS1","r");
197
 
198
    while(true)
199
    {
369 kakl 200
        fscanf(pRS232,"$GPGGA,%*f,%lf,N,%lf,E,*", &N, &E); // parser NMEA
201
        nn=ldiv((long)N,100).quot;   // prepocet DDMM.MM na DD.DD
202
        pomN=(N-nn*100)/60+nn;
203
        ee=ldiv((long)E,100).quot;
204
        pomE=(E-ee*100)/60+ee;
205
        pthread_mutex_lock(&mutex);         // prepis souradnic do sdilenych promennych
367 kakl 206
        n=pomN; e=pomE;
207
        pthread_mutex_unlock(&mutex);
369 kakl 208
        usleep(500000);                     // NMEA nechodi castejc nez 1x za 1s
367 kakl 209
    }
210
}
211
 
366 kakl 212
/*
365 kakl 213
void *sensors(void *unused)
214
{
215
	char Buf[64];
216
 
217
    while(true)
218
    {
219
        I2C_addr(US_Addr);
220
        Buf[0]=0x0;
221
        Buf[1]=0x51;
222
        write(file, Buf, 2);
223
        usleep(80000);
224
        read(file, Buf, 3);
225
        vzdalenost=(Buf[1]*256+Buf[2]);
226
        usleep(300000);
227
 
228
        I2C_addr(PIC_Addr);
229
        Buf[0]=command;
230
        write(file, Buf, 1);
231
        read(file, Buf, 1);
232
        ble=Buf[0];
233
    }
234
}
366 kakl 235
*/