Subversion Repositories svnkaklik

Rev

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

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