Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / mapping / server / test.c @ 979

History | View | Annotate | Download (4.04 KB)

1
#include <stdlib.h>
2
#include <stdio.h>
3
#include <curses.h> // you need to install the ncurses library
4
#include "../../libwireless/lib/wireless.h"
5
#include <unistd.h>
6

    
7
#define CHAN 0xE
8

    
9
#define MAP 1   // packet group for receiving points
10
#define POINT_RAW 1 // packet type for map data points w/ raw encoder data
11
#define POINT_ODO 2 // packet type for map data points w/ odometry data
12

    
13
#define WL_CNTL_GROUP 4
14

    
15
#define CNTL_FORWARD      0
16
#define CNTL_BACK         1
17
#define CNTL_LEFT         2
18
#define CNTL_RIGHT        3
19
#define CNTL_LEFT_CURVE   4
20
#define CNTL_RIGHT_CURVE  5
21
#define CNTL_STOP         6
22
#define CNTL_VEL_UP       7
23
#define CNTL_VEL_DOWN     8
24

    
25
void send_packet (char type, int dst_robot);
26
void packet_receive(char type, int source, unsigned char* packet, int length);
27

    
28
PacketGroupHandler cntlHandler = {WL_CNTL_GROUP, NULL, NULL, NULL, NULL};
29
PacketGroupHandler receiveHandler = {MAP, NULL, NULL, &packet_receive, NULL};
30

    
31
FILE *file;
32
int parent_running = 1;
33

    
34
int main(int argc, char *argv[]) {
35
    if (argc != 2) {
36
        printf("Usage: ./test <robot #>\n");
37
        return 1;
38
    }
39
    
40
    char c;
41
    int robot = atoi(argv[1]);
42

    
43
    printf("Beginning: robot=%d\r\n", robot);
44
    wl_init();
45
        wl_set_com_port("/dev/ttyUSB1");
46
        wl_set_channel(CHAN);
47
        printf("Wireless initialized.\r\n");
48
        wl_register_packet_group(&cntlHandler);
49
        wl_register_packet_group(&receiveHandler);
50
    printf("Packet groups initialized.\r\n");
51
    fflush(stdout);
52
    
53
        file = fopen("input.txt", "w");
54
        if (file == NULL) {
55
            printf("fopen error\n");
56
            return 1;
57
        }
58

    
59
        WINDOW* win = initscr();
60
        nodelay(win, TRUE);
61
        while (1) {
62
            wl_do();
63
            c = getch();
64
            if(c != ERR) {
65
                printf("%c\r\n", c);
66
                fflush(stdout);
67
                if (c=='0')
68
                    break;
69
                switch (c) {
70
                    case 'w':
71
                        send_packet(CNTL_FORWARD, robot);
72
                        break;
73
                    case 's':
74
                        send_packet(CNTL_BACK, robot);
75
                        break;
76
                    case 'a':
77
                        send_packet(CNTL_LEFT, robot);
78
                        break;
79
                    case 'd':
80
                        send_packet(CNTL_RIGHT, robot);
81
                        break;
82
                    case 'q':
83
                        send_packet(CNTL_LEFT_CURVE, robot);
84
                        break;
85
                    case 'e':
86
                        send_packet(CNTL_RIGHT_CURVE, robot);
87
                        break;
88
                    case 'x':
89
                        send_packet(CNTL_STOP, robot);
90
                        break;
91
                    case ']':
92
                        send_packet(CNTL_VEL_UP, robot);
93
                        break;
94
                    case '[':
95
                        send_packet(CNTL_VEL_DOWN, robot);
96
                        break;
97
                    default:
98
                        //send_packet(CNTL_STOP, robot);
99
                        break;
100
                }
101
            }
102
            }
103
            fclose(file);
104
            delwin(win);
105
            endwin();
106
            refresh();
107
        return 0;
108
}
109

    
110
void send_packet (char type, int dst_robot) {
111
    wl_send_robot_to_robot_global_packet(WL_CNTL_GROUP, type,
112
            NULL, 0, dst_robot, 0);
113
}
114

    
115
void packet_receive (char type, int source, unsigned char* packet, int length) {
116
    short x,y,ir1,ir2,ir3,ir4,ir5;
117

    
118
    /* convert received data from 2 chars to 1 short */
119
    x = ((short)packet[1] << 8) | (short)packet[0];
120
    y = ((short)packet[3] << 8) | (short)packet[2];
121
    ir1 = ((short)packet[9] << 8) | (short)packet[8];
122
    ir2 = ((short)packet[11] << 8) | (short)packet[10];
123
    ir3 = ((short)packet[13] << 8) | (short)packet[12];
124
    ir4 = ((short)packet[15] << 8) | (short)packet[14];
125
    ir5 = ((short)packet[17] << 8) | (short)packet[16];
126

    
127
    char tarr[] = {packet[4],packet[5],packet[6],packet[7]};
128
    float *theta_ptr = (float *)tarr;
129
    float theta = *theta_ptr;
130

    
131
    fprintf(file, "%d %d %f %d %d %d %d %d\n", x, y, theta,
132
            ir1, ir2, ir3, ir4, ir5);
133
}