Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / robot / joystick / discrete_control / joystick.c @ 1349

History | View | Annotate | Download (4.77 KB)

1
#include <dragonfly_lib.h>
2
#include <wireless.h>
3
#include <wl_defs.h>
4
#include "../../../common/colonet_defs.h"
5
#include <serial.h>
6
#include <stdio.h>
7

    
8
/* Current state codes */
9
#define TRIGGER_DOWN              1
10
#define TRIGGER_UP               (1<<1)
11
#define BUTTON_DOWN              (1<<2)
12
#define BUTTON_UP                (1<<3)
13
#define X_LEFT                   (1<<4)
14
#define X_CENTER                 (1<<5)
15
#define X_RIGHT                  (1<<6)
16
#define Y_UP                     (1<<7)
17
#define Y_CENTER                 (1<<8)
18
#define Y_DOWN                   (1<<9)
19

    
20
/* State change codes */
21
#define TRIGGER_PRESSED           1
22
#define TRIGGER_RELEASED          (1<<1)
23
#define BUTTON_PRESSED            (1<<2)
24
#define BUTTON_RELEASED           (1<<3)
25
#define MOVED_LEFT                (1<<4)
26
#define MOVED_X_CENTER            (1<<5)
27
#define MOVED_RIGHT               (1<<6)
28
#define MOVED_UP                  (1<<7)
29
#define MOVED_Y_CENTER            (1<<8)
30
#define MOVED_DOWN                (1<<9)
31

    
32
int joystick_status (void);
33
int joystick_change (int old, int new);
34

    
35
PacketGroupHandler cntlHandler = {4, NULL, NULL, NULL, NULL};
36

    
37
int main (void) {
38
  dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG);
39
        ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
40
  //xbee_init();
41
  wl_init();
42
  wl_register_packet_group(&cntlHandler);
43

    
44
  orb_set_color(BLUE);
45
  
46
  int change;
47
  int last_status = 0; 
48
  int cur_status = 0;
49
  
50
  while (1) {
51
    last_status = cur_status;
52
    cur_status = joystick_status();
53
    change = joystick_change(last_status, cur_status);
54
    
55
/*    char buf[100];
56
    sprintf(buf, "status: %d\n", cur_status);
57
    usb_puts(buf);
58
*/ 
59
    if (change & MOVED_UP) {
60
      orb_set_color(GREEN);
61
      //xbee_putc('f');
62
      wl_send_global_packet(4, 'f', NULL, 0, 0);
63
      usb_putc('f');
64
    }
65
    if (change & MOVED_Y_CENTER) {
66
      orb_set_color(RED);
67
      //xbee_putc('s');
68
      wl_send_global_packet(4, 's', NULL, 0, 0);
69
      usb_putc('s');
70
    }
71
    if (change & MOVED_DOWN) {
72
      orb_set_color(BLUE);
73
      //xbee_putc('b');
74
      wl_send_global_packet(4, 'b', NULL, 0, 0);
75
      usb_putc('b');
76
    }
77
    if (change & MOVED_LEFT) {
78
      orb_set_color(ORANGE);
79
      //xbee_putc('l');
80
      wl_send_global_packet(4, 'l', NULL, 0, 0);
81
      usb_putc('l');
82
    }
83
    if (change & MOVED_X_CENTER) {
84
      orb_set_color(RED);
85
      //xbee_putc('s');
86
      wl_send_global_packet(4, 's', NULL, 0, 0);
87
      usb_putc('s');
88
    }
89
    if (change & MOVED_RIGHT) {
90
      orb_set_color(YELLOW);
91
      //xbee_putc('r');
92
      wl_send_global_packet(4, 'r', NULL, 0, 0);
93
      usb_putc('r');
94
    }
95
    if (change & TRIGGER_PRESSED) {
96
      orb_set_color(PURPLE);
97
      //xbee_putc('t');
98
      wl_send_global_packet(4, 't', NULL, 0, 0);
99
    }
100
    if (change & BUTTON_PRESSED) {
101
      orb_set_color(YELLOW);
102
      //xbee_putc('u');
103
      wl_send_global_packet(4, 's', NULL, 0, 0);
104
    }
105
    delay_ms(100);
106
  }
107
  
108
}
109

    
110
int joystick_change (int old, int new) {
111
  int trigger_change = 0;
112
  int button_change = 0;
113
  int x_change = 0;
114
  int y_change = 0;
115
  /* Check for trigger change */
116
  if ((old & TRIGGER_DOWN) && (new & TRIGGER_UP))
117
    trigger_change = TRIGGER_RELEASED;
118
  if ((old & TRIGGER_UP) && (new & TRIGGER_DOWN)) 
119
    trigger_change = TRIGGER_PRESSED;
120
  /* Check for button change */
121
  if ((old & BUTTON_DOWN) && (new & BUTTON_UP))
122
    button_change = BUTTON_RELEASED;
123
  if ((old & BUTTON_UP) && (new & BUTTON_DOWN))
124
    button_change = BUTTON_PRESSED;
125
  /* Check for X change */
126
  if ((old & X_CENTER) && (new & X_RIGHT))
127
    x_change = MOVED_RIGHT;
128
  if ((old & X_CENTER) && (new & X_LEFT))
129
    x_change = MOVED_LEFT;
130
  if (!(old & X_CENTER) && (new & X_CENTER))
131
    x_change = MOVED_X_CENTER;
132
  /* Check for Y change */
133
  if ((old & Y_CENTER) && (new & Y_UP))
134
    y_change = MOVED_UP;
135
  if ((old & Y_CENTER) && (new & Y_DOWN))
136
    y_change = MOVED_DOWN;
137
  if (!(old & Y_CENTER) && (new & Y_CENTER))
138
    y_change = MOVED_Y_CENTER;
139
  return trigger_change | button_change | x_change | y_change;
140
}
141

    
142
/**
143
 * Get the current status of the joystick.
144
 * This includes the X and Y axes and the buttons.
145
 */
146
int joystick_status (void) {
147
        int trigger_status = 0;
148
        int button_status = 0;
149
        int x_status = 0; 
150
        int y_status = 0;
151
        int xval, yval;
152
    
153
  if (analog_get8(AN0) > 200)
154
    trigger_status = TRIGGER_DOWN;
155
  else
156
    trigger_status = TRIGGER_UP;
157
    
158
  if (analog_get8(AN1) > 200)
159
    button_status = BUTTON_DOWN;
160
  else
161
    button_status = BUTTON_UP;
162
  
163
  xval = analog_get8(AN4); // actually AN6, AN7 for connection
164
  yval = analog_get8(AN5);
165
  
166
  if (xval > 150)
167
    x_status = X_LEFT;
168
  else if (xval < 75)
169
    x_status = X_RIGHT;
170
  else
171
    x_status = X_CENTER;
172
  
173
  if (yval > 150)
174
    y_status = Y_UP;
175
  else if (yval < 75)
176
    y_status = Y_DOWN;
177
  else
178
    y_status = Y_CENTER;
179

    
180

    
181
  return trigger_status | button_status | x_status | y_status;
182
}
183