Subversion Repositories svnkaklik

Rev

Rev 366 | 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"
35
 
36
using namespace std;
37
 
38
#define CMPS03_SOFTWARE_REVISION 0x0
39
#define SRF02_SOFTWARE_REVISION 0x0
40
 
41
#define BC_Addr	    0x0B
42
#define US_Addr	    0x70 // 0xE0 in fact
367 kakl 43
#define M1	0x50 // 0xA0 in fact
44
#define M2	0x51 // 0xA2 in fact
365 kakl 45
 
367 kakl 46
char vystup[50];
47
pthread_t thread_1, thread_2, thread_3;
365 kakl 48
FILE *pRouraO,*pRouraI;
49
unsigned int vzdalenost;
50
char command,ble;
51
int file;
367 kakl 52
double n, e;
365 kakl 53
 
367 kakl 54
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
55
 
365 kakl 56
void *print_tele(void *unused);
367 kakl 57
void *gps(void *unused);
366 kakl 58
//void *sensors(void *unused);
365 kakl 59
 
60
void I2C_addr (int Addr)
61
{
62
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
63
    {
64
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
65
        exit(-5);
66
    }
67
}
68
 
367 kakl 69
void go (int Addr, int speed)
70
{
71
    char Buf[1];
72
 
73
    I2C_addr (Addr);
74
    Buf[0]=speed;
75
    write(file, Buf, 1);
76
}
77
 
365 kakl 78
int main(int argc, char *argv[], char *envp[])
79
{
80
	fprintf(stdout, "**** Vector Control Programm ****\n");
81
 
82
	file = open("/dev/i2c-0", O_RDWR);
83
	if (file < 0)
84
	{
85
		cerr << "Could not open /dev/i2c-0." << endl;
86
		return -1;
87
	}
88
 
89
    pthread_create(&thread_1, NULL, print_tele, NULL);
90
//    pthread_create(&thread_2, NULL, sensors, NULL);
91
 
366 kakl 92
    char Buf[64];
367 kakl 93
    command=0;
366 kakl 94
 
365 kakl 95
    while(true)
96
    {
367 kakl 97
        switch (command)
366 kakl 98
        {
367 kakl 99
            case 'f':   // forward
100
                go(M1, 20);
101
                go(M2, 20);
102
                command=0;
103
                break;
104
            case 'b':   // backward
105
                go(M1, -20);
106
                go(M2, -20);
107
                command=0;
108
                break;
109
            case 's':   // stop
110
                go(M1, 0);
111
                go(M2, 0);
112
                command=0;
113
                break;
114
            case 'g':
115
usleep(180000);
116
                I2C_addr(US_Addr);
117
                Buf[0]=0x0;
118
                Buf[1]=0x51;
119
                write(file, Buf, 2);
120
                usleep(80000);
121
                read(file, Buf, 3);
122
                vzdalenost=(Buf[1]*256+Buf[2]);
123
                if ((vzdalenost>50)&&(vzdalenost<80))
124
                {
125
                    go(M1, 20);
126
                    go(M2, 20);
127
                    break;
128
                };
129
                if ((vzdalenost>30)&&(vzdalenost<120))
130
                {
131
                    if (vzdalenost<50)
132
                    {
133
                        go(M1, 20);
134
                        go(M2, 40);
135
                    }
136
                    else
137
                    {
138
                        go(M1, 40);
139
                        go(M2, 20);
140
                    }
141
                }
142
                else
143
                {
144
                    go(M1, 0);  // zastav, neni videt doprovod
145
                    go(M2, 0);
146
                };
147
                break;
366 kakl 148
        }
365 kakl 149
    };
366 kakl 150
 
365 kakl 151
	close(file);
152
    pthread_join(thread_1, NULL);
153
    pthread_join(thread_2, NULL);
154
 
155
	return 0;
156
}
157
 
366 kakl 158
 
365 kakl 159
void *print_tele(void *unused)
160
{
367 kakl 161
    char string[2];
366 kakl 162
 
365 kakl 163
    while(true)
164
    {
165
        pRouraI = fopen("/home/ble/pipe","r");
367 kakl 166
        command=fgetc(pRouraI);
167
        string[0]=command;
168
        string[1]=0;
365 kakl 169
        fclose(pRouraI);
170
        pRouraO = fopen("/home/ble/pipe","w");
367 kakl 171
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %s\n",vzdalenost,string);
172
 
173
    pthread_mutex_lock(&mutex);
174
   	fprintf(pRouraO,"%f N %f E\n", n, e);
175
   	fprintf(pRouraO,"Vzdalenost: %.1f m\n", GeoCalc::EllipsoidDistance(n, e, 49.266667, 14.716667));
176
	fprintf(pRouraO,"Azimut: %.2f Deg\n", GeoCalc::GCAzimuth(n, e, 49.266667, 14.716667));
177
    pthread_mutex_unlock(&mutex);
178
 
365 kakl 179
        fclose(pRouraO);
180
    }
181
}
182
 
367 kakl 183
void *gps(void *unused)
184
{
185
    FILE *pRS232;
186
    double pomN, pomE;
187
 
188
    pRS232 = fopen("/dev/ttyS1","r");
189
 
190
    while(true)
191
    {
192
        fscanf(pRS232,"$GPGGA,*,%lf,N,%lf,E", &pomN, &pomE);
193
        pthread_mutex_lock(&mutex);
194
        n=pomN; e=pomE;
195
        pthread_mutex_unlock(&mutex);
196
        usleep(500000);
197
    }
198
}
199
 
366 kakl 200
/*
365 kakl 201
void *sensors(void *unused)
202
{
203
	char Buf[64];
204
 
205
    while(true)
206
    {
207
        I2C_addr(US_Addr);
208
        Buf[0]=0x0;
209
        Buf[1]=0x51;
210
        write(file, Buf, 2);
211
        usleep(80000);
212
        read(file, Buf, 3);
213
        vzdalenost=(Buf[1]*256+Buf[2]);
214
        usleep(300000);
215
 
216
        I2C_addr(PIC_Addr);
217
        Buf[0]=command;
218
        write(file, Buf, 1);
219
        read(file, Buf, 1);
220
        ble=Buf[0];
221
    }
222
}
366 kakl 223
*/