Project

General

Profile

Revision 85

Wireless library now works on teh charging station.

View differences:

branches/charging_station/code/projects/recharging/charging_station/lights.h
1
/*
2
lights.h
3

  
4
most of this is shamelessly copied from FWR's orb.h (Tom Lauwers and Steven Shamlian)
5

  
6
author: CMU Robotics Club, Colony Project
7

  
8
*/
9

  
10
#ifndef _LIGHTS_H_
11
#define _LIGHTS_H_
12

  
13
//user LED
14
#define USERLED PING2
15

  
16
//ORB
17
#define RED       0xE0
18
#define ORANGE    0xE8
19
#define YELLOW    0xFC
20
#define LIME      0x7C
21
#define GREEN     0x1C
22
#define CYAN      0x1F
23
#define BLUE      0x03
24
#define PINK      0x63
25
#define PURPLE    0x23
26
#define MAGENTA   0xE3
27
#define WHITE     0xFF
28
#define ORB_OFF   0x00
29

  
30
//LEDs are on bank E
31
#define REDLED   PE3
32
#define GREENLED PE4
33
#define BLUELED  PE5
34

  
35
//?
36
#define COUNT_START 0x8000
37

  
38

  
39
//user LED
40
void led_init( void );
41
void led_user(int value);
42

  
43

  
44
// For function descriptions see orb.c 
45
void orb_init(void);
46
void orb_set(unsigned int red_led, unsigned int green_led, unsigned int blue_led); 
47
void orb_set_color(int col);
48
void orb_disable(void);
49
void orb_enable(void);
50

  
51
void orb_set_dio(int red, int green, int blue);
52

  
53
#endif
branches/charging_station/code/projects/recharging/charging_station/wireless.h
1
/**
2
 * @file wireless.h
3
 * @brief Contains definitions for the wireless library.
4
 *
5
 * Contains functions for the wireless library.
6
 *
7
 * @author Brian Coltin, Colony Project, CMU Robotics Club
8
 **/
9
 
10
//Note: If this is raised above 16, we will need to do
11
//something about frame numbers for TX Status packets.
12
/**
13
 * The maximum number of packet groups.
14
 **/
15
#define WL_MAX_PACKET_GROUPS 16
16

  
17
/**
18
 * @defgroup wireless Wireless
19
 * @brief Wireless definitions.
20
 *
21
 * Contains functions and definitions for dealing with wireless functionality.<br><br>
22
 *
23
 * The wireless library provides a modular method for dealing with 
24
 * wireless packets, by allowing packet groups to be registered.
25
 * A packet group is a collection of packets which share a packet
26
 * group code. Each packet in the group also has a type. A packet 
27
 * group code and type are sent with each packet. When a packet
28
 * with a group code registered in the wireless library is 
29
 * received, the corresponding event handler is called. The
30
 * event handler uses the packet type and other information
31
 * stored in the packet to respond.<br><br>
32
 *
33
 * This architecture allows different wireless functionality to be
34
 * defined and handled separately, making it simpler and more
35
 * efficient to take advantage of the XBee's wireless functionality.
36
 *
37
 * @{
38
 **/
39

  
40
/**
41
 * @struct PacketGroupHandler
42
 * A PacketGroupHandler represents a packet group, and is used to 
43
 * register a packet group with the wireless library. It contains
44
 * handlers for various events which can occur related to a packet
45
 * group.
46
 **/
47
typedef struct
48
{
49
	/**
50
	 * The group code for this packet group. This number
51
	 * must be unique. The maximum number of packet groups
52
	 * is defined by WL_MAX_PACKET_GROUPS.
53
	 **/
54
	unsigned int groupCode;
55

  
56
	/**
57
	 * Called every half second (not in interrupt,
58
	 * but in wl_do).
59
	 **/
60
	void (*timeout_handler) (void);
61
	
62
	/**
63
	 * Called when a transmit status packet is received
64
	 * from the XBee where the first four bits of the frame
65
	 * are  the group code.
66
	 *
67
	 * @param frame the last four bits of the frame
68
	 * @param received is true if we received an ack, 0 if
69
	 * we did not.
70
	 **/
71
	void (*handle_response) (int frame, int received);
72
	
73
	/**
74
	 * Called when we receive a packet from this group.
75
	 *
76
	 * @param type the packet type
77
	 * @param source the 16-bit address of the XBee this
78
	 * packet was sent from
79
	 * @param packet the packet received
80
	 * @param length the length of the packet
81
	 **/
82
	void (*handle_receive) (char type, int source, unsigned char* packet,
83
							int length);
84
	
85
	/**
86
	 * Called for any cleanup when the network is turned off.
87
	 **/
88
	void (*unregister) (void);
89
	
90
} PacketGroupHandler;
91

  
92
/**@brief Initialize the wireless library **/
93
void wl_init(void);
94
/**@brief Uninitialize the wireless library **/
95
void wl_terminate(void);
96
/**@brief Perform wireless library functionality **/
97
void wl_do(void);
98
/**@brief Register a packet group with the wireless library **/
99
void wl_register_packet_group(PacketGroupHandler* h);
100
/**@brief Unregister a packet group with the wireless library **/
101
void wl_unregister_packet_group(PacketGroupHandler* h);
102

  
103
/**@brief Send a packet to a specific robot in any PAN **/
104
void wl_send_robot_to_robot_global_packet(char group, char type,
105
		char* data, int len, int dest, char frame);
106
/**@brief Send a packet to a specific robot in our PAN **/
107
void wl_send_robot_to_robot_packet(char group, char type,
108
		char* data, int len, int dest, char frame);
109
/**@brief Send a packet to all robots **/
110
void wl_send_global_packet(char group, char type,
111
		char* data, int len, char frame);
112
/**@brief Send a packet to all robots in our PAN **/
113
void wl_send_pan_packet(char group, char type,
114
		char* data, int len, char frame);
115

  
116
/**@brief Set the PAN we are using **/
117
void wl_set_pan(int pan);
118
/**@brief Get the PAN we are using **/
119
int wl_get_pan(void);
120
/**@brief Set the channel we are using **/
121
void wl_set_channel(int channel);
122
/**@brief Get the channel we are using **/
123
int wl_get_channel(void);
124
/**@brief Get the 16-bit address of the XBee module **/
125
unsigned int wl_get_xbee_id(void);
126

  
127
/** @} **/ // end defgroup
128

  
branches/charging_station/code/projects/recharging/charging_station/linear_bom.h
1
/*Plug the molex for each BOM in with the BLACK wire to the
2
io pin and the red wire to the positive channel going down
3
the middle of the PINA/E/I2C block*/
4

  
5

  
6
#define LINEAR_BOM_PIN0 PIN_AN0
7
#define LINEAR_BOM_PIN1 PIN_AN1
8
#define LINEAR_BOM_PIN2 PIN_AN2
9
#define LINEAR_BOM_PIN3 PIN_AN3
10

  
11
#define lbom_set(which) \
12
    digital_output(LINEAR_BOM_PIN0 + which, 0)
13
#define lbom_off() \
14
    digital_output(LINEAR_BOM_PIN0, 1); \
15
    digital_output(LINEAR_BOM_PIN1, 1); \
16
    digital_output(LINEAR_BOM_PIN2, 1); \
17
    digital_output(LINEAR_BOM_PIN3, 1);
branches/charging_station/code/projects/recharging/charging_station/serial.c
1
/*
2
	serial.c - Functions for using the RS232 serial port
3
	
4
	author: Robotics Club, Colony Project
5
	
6
	much code taken from FWR's library, author: Tom Lauwers
7
	
8
	
9
	general note - serial -> uart
10
				   serial1 -> uart1
11
		this is done for clarity purposes
12
*/
13

  
14

  
15
#include <avr/io.h>
16
#include <stdio.h>
17
#include "serial.h"
18

  
19
//setup uart0 (serial)
20
void xbee_init(unsigned int ubrr)
21
{
22
	/*Set baud rate - baud rates under 4800bps unsupported */
23
	UBRR0H = 0x00;
24
	UBRR0L = (unsigned char)ubrr;
25

  
26
	/*Enable receiver and transmitter */
27
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
28
	
29
	/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
30
	UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
31
}
32

  
33
// uart_putchar - Low-level function which puts a value into the 
34
// Tx buffer for transmission. 
35
int xbee_putc(char c)
36
{
37
	// Wait until buffer is clear for sending
38
    loop_until_bit_is_set(UCSR0A, UDRE0);
39
	// Load buffer with your character
40
    UDR0 = c;
41
    return 0;
42
}
43

  
44
int xbee_puts(char *s)
45
{
46
        char *t = s;
47
        while (*t != 0)
48
        {
49
                xbee_putc(*t);
50
                t++;
51
        }
52
  return 0;
53
}
54

  
55
int xbee_puti(int value ) {
56
	unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
57

  
58
        /* convert int to ascii  */ 
59
        if(value<0) { xbee_putc('-'); value=-value; }    
60
        do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
61

  
62
    
63
        /* start displaying the number */
64
        for(;position<=(sizeof(usb_data)-1);position++)
65
          {
66
            
67
            xbee_putc(usb_data[position]);
68
          }
69

  
70
	return 0;
71
}
72

  
73
// uart_getchar - Low-level function which waits for the Rx buffer
74
// to be filled, and then reads one character out of it.
75
// Note that this function blocks on read - it will wait until
76
// something fills the Rx buffer.
77
int xbee_getc(void)
78
{
79
	char c;
80
	// Wait for the receive buffer to be filled
81
    loop_until_bit_is_set(UCSR0A, RXC0);
82
	
83
	// Read the receive buffer
84
    c = UDR0;
85
	return c;
86
}
87

  
88
// uart_getchar_nb - Low-level function which checks if the Rx buffer
89
// is filled, and then reads one character out of it.
90
// This is a non blocking version uart_getchar
91
int xbee_getc_nb(void)
92
{
93
	char c;
94
	// Wait for the receive buffer to be filled
95
    //loop_until_bit_is_set(UCSR0A, RXC0);
96
	if (UCSR0A & _BV(RXC0)){
97
		// Read the receive buffer
98
		c = UDR0;
99
		return c;
100
	}
101
	return 0;
102
		
103
}
104

  
105
//setup uart1 (serial1)
106
void usb_init( unsigned int ubrr)
107
{
108
	/*Set baud rate - baud rates under 4800bps unsupported */
109
	UBRR1H = 0x00;
110
	UBRR1L = (unsigned char)ubrr;
111

  
112
	/*Enable receiver and transmitter */
113
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
114
	
115
	/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
116
	UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
117
}
118

  
119
//put to uart1 (serial1)
120
int usb_putc(char c)
121
{
122
	// Wait until buffer is clear for sending
123
    loop_until_bit_is_set(UCSR1A, UDRE1);
124
	// Load buffer with your character
125
    UDR1 = c;
126
    return 0;
127
}
128

  
129
int usb_puts(char *s)
130
{
131
        char *t = s;
132
        while (*t != 0)
133
        {
134
                usb_putc(*t);
135
                t++;
136
        }
137
  return 0;
138
}
139

  
140
int usb_puti(int value ) {
141
	unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
142

  
143
        /* convert int to ascii  */ 
144
        if(value<0) { usb_putc('-'); value=-value; }    
145
        do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
146

  
147
    
148
        /* start displaying the number */
149
        for(;position<=(sizeof(usb_data)-1);position++)
150
          {
151
            
152
            usb_putc(usb_data[position]);
153
          }
154

  
155
	return 0;
156
}
157

  
158
//get from uart1 (serial1)
159
int usb_getc(void)
160
{
161
	char c;
162
	// Wait for the receive buffer to be filled
163
    loop_until_bit_is_set(UCSR1A, RXC1);
164
	
165
	// Read the receive buffer
166
    c = UDR1;
167
	return c;
168
}
169

  
170
//get from uart1 non-block
171
int usb_getc_nb(void)
172
{
173
	char c;
174
	// Wait for the receive buffer to be filled
175
    //loop_until_bit_is_set(UCSR1A, RXC1);
176
	if (UCSR1A & _BV(RXC1)){
177
		// Read the receive buffer
178
		c = UDR1;
179
		return c;
180
	}
181
	return 0;
182
		
183
}
184

  
branches/charging_station/code/projects/recharging/charging_station/buzzer.c
1
/*
2
	buzzer.c - Contains the functions necessary for running the buzzer
3
	
4
	author: Robotics Club, Colony Project
5
	
6
	much taken from FWR's firefly library (author: Tom Lauwers)
7

  
8
*/
9

  
10
#include <avr/io.h>
11
#include "buzzer.h"
12
#include "time.h"
13

  
14
/*
15
initializes buffer
16

  
17
this function must be called before the buzzer can be used
18
*/
19
void buzzer_init( void )
20
{
21
	// Set pin D7 to output - D7 is buzzer pin
22
	DDRB |= 0x80;
23
	
24
	// Set to Fast PWM mode, toggle the OC0A pin (D6) on output compare
25
	// matches, and select a clock prescalar of 64 for the counter.
26
	// Counter FREQ = 8000000/64 = 125000
27
	//01011000
28
	TCCR2 = 0x1B;
29
	OCR2=100;
30
  
31
  buzzer_off();
32
	
33
}
34

  
35
// Set the buzzer value - takes a value from 0-255
36
// Higher values are lower frequency.  Take a look at 
37
// the buzzer frequency table to see how a given value
38
// correlates to a frequency.
39
void buzzer_set_val(unsigned int buzz_value)
40
{ 
41
	OCR2 = buzz_value;
42
}
43

  
44
// Set the buzzer frequency - takes any value and tries to find the nearest buzzer frequency
45
void buzzer_set_freq(unsigned int buzz_freq)
46
{
47
	int buzz_value;
48
	
49
	buzz_value = 62500/buzz_freq - 1;
50

  
51
	if(buzz_value > 255)
52
		buzz_value = 255;
53
	else if(buzz_value < 0)
54
		buzz_value = 0;
55
	
56
	buzzer_set_val(buzz_value);
57
}
58

  
59
// Chirps the buzzer for a number of milliseconds
60
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
61
{	
62
  
63
	buzzer_set_freq(buzz_freq);
64
	
65
	delay_ms(ms);
66
		
67
	buzzer_off();
68
}
69

  
70
// Disables timer0 clock counting, turning off the buzzer
71
void buzzer_off() 
72
{
73
	// Disable clock, to halt counter
74
	TCCR2 &= 0xF8;
75
	
76
	// Set buzzer pin low in case it's high
77
	PORTB &= 0x7F;
78
}
79

  
branches/charging_station/code/projects/recharging/charging_station/xbee.c
1
#include "xbee.h"
2
#include "wl_defs.h"
3

  
4
#ifndef ROBOT
5

  
6
#include <fcntl.h>
7
#include <unistd.h>
8
#include <pthread.h>
9
#include <errno.h>
10

  
11
#else
12

  
13
#include <serial.h>
14
#include <avr/interrupt.h>
15

  
16
#endif
17

  
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21

  
22
#include <queue.h>
23

  
24
#define XBEE_FRAME_START 0x7E
25

  
26
/*Frame Types*/
27
#define XBEE_FRAME_STATUS 0x8A
28
#define XBEE_FRAME_AT_COMMAND 0x08
29
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88
30
#define XBEE_FRAME_TX_REQUEST_64 0x00
31
#define XBEE_FRAME_TX_REQUEST_16 0x01
32
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS
33
#define XBEE_FRAME_RX_64 0x80
34
#define XBEE_FRAME_RX_16 XBEE_RX
35

  
36
/*Internal Function Prototypes*/
37

  
38
/*I/O Functions*/
39
void xbee_send(char* buf, int size);
40
void xbee_send_string(char* c);
41

  
42
#ifndef ROBOT
43
void xbee_read(char* buf, int size);
44
#endif
45

  
46
/*Command Mode Functions
47
 * Called during initialization.
48
 */
49
void xbee_enter_command_mode(void);
50
void xbee_exit_command_mode(void);
51
void xbee_enter_api_mode(void);
52
void xbee_wait_for_string(char* s, int len);
53
void xbee_wait_for_ok(void);
54

  
55
/*API Mode Functions*/
56

  
57
int xbee_handle_packet(char* packet, int len);
58
void xbee_handle_at_command_response(char* command, char result,
59
	char* extra, int extraLen);
60
void xbee_handle_status(char status);
61
int xbee_verify_checksum(char* packet, int len);
62
char xbee_compute_checksum(char* packet, int len);
63
void xbee_send_frame(char* buf, int len);
64
void xbee_send_read_at_command(char* command);
65
void xbee_send_modify_at_command(char* command, char* value);
66

  
67
/*Global Variables*/
68

  
69
#ifndef ROBOT
70
int xbee_stream;
71
pthread_t* xbee_listen_thread;
72
#endif
73

  
74
Queue* xbee_queue;
75

  
76
//used to store packets as they are read
77
char xbee_buf[128];
78
int currentBufPos = 0;
79

  
80
//XBee status
81
unsigned int xbee_panID = XBEE_PAN_DEFAULT;
82
unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
83
int xbee_channel = XBEE_CHANNEL_DEFAULT;
84
int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
85
unsigned int xbee_address = 0;
86

  
87
/*Function Implementations*/
88

  
89
#ifdef ROBOT
90

  
91
/**
92
 * Interrupt for the robot. Adds bytes received from the xbee
93
 * to the queue.
94
 **/
95
#ifndef FIREFLY
96
ISR(USART1_RX_vect)
97
{
98
	char c = UDR1;
99
	queue_add(xbee_queue, (void*)(int)c);
100
}
101
#else
102
SIGNAL(SIG_USART0_RECV)
103
{
104
	char c = UDR0;
105
	queue_add(xbee_queue, (void*)(int)c);
106
}
107
#endif
108

  
109
#else
110

  
111
/**
112
 * Thread that listens to the xbee.
113
 **/
114
void* listen_to_xbee(void* x)
115
{
116
	char c;
117
	while (1)
118
	{
119
		xbee_read(&c, 1);
120
		queue_add(xbee_queue, (void*)(int)c);
121
	}
122
	return 0;
123
}
124

  
125
#endif
126

  
127
/**
128
 * Initializes the XBee library so that other functions may be used.
129
 *
130
 * @param pan_id the PAN to join initially. Use XBEE_PAN_DEFAULT
131
 * to leave the PAN as it is initially.
132
 **/
133
void xbee_lib_init(void)
134
{
135
	xbee_queue = queue_create();
136
	
137
	#ifdef ROBOT
138

  
139
	//enable the receiving interrupt
140
	#ifdef FIREFLY
141
	UCSR0B |= _BV(RXCIE) | _BV(RXEN);
142
	#else
143
	UCSR1B |= _BV(RXCIE);
144
	#endif
145
	sei();
146
	#else
147
	xbee_stream = open("/dev/ttyUSB0", O_RDWR);
148
	if (xbee_stream == -1 || lockf(xbee_stream, F_TEST, 0) != 0)
149
		xbee_stream = open("/dev/ttyUSB1", O_RDWR);
150
	if (xbee_stream == -1 || lockf(xbee_stream, F_TEST, 0) != 0)
151
	{
152
		printf("Failed to open connection to XBee.\r\n");
153
		exit(0);
154
	}
155
	lockf(xbee_stream, F_LOCK, 0);
156
	
157
	xbee_listen_thread =
158
		(pthread_t*)malloc(sizeof(pthread_t));
159
	
160
	int ret = pthread_create(xbee_listen_thread, NULL, 
161
		listen_to_xbee, NULL);
162
	if (ret)
163
	{
164
		printf("Failed to create listener thread.\r\n");
165
		exit(0);
166
	}
167
	#endif
168
	xbee_enter_command_mode();
169
	xbee_enter_api_mode();
170
	xbee_exit_command_mode();
171
	xbee_send_read_at_command("MY");
172
	
173
	//wait to return until the address is set
174
	while (xbee_address == 0) xbee_get_packet(NULL);
175
}
176

  
177
/**
178
 * Call when finished using the XBee library. This releases
179
 * all sued resources.
180
 **/
181
void xbee_terminate()
182
{
183
	#ifndef ROBOT
184
	pthread_cancel(*xbee_listen_thread);
185
	free(xbee_listen_thread);
186
	lockf(xbee_stream, F_ULOCK, 0);
187
	close(xbee_stream);
188
	#endif
189
	queue_destroy(xbee_queue);
190
}
191

  
192
/**
193
 * Send a buffer buf of size bytes to the XBee.
194
 * 
195
 * @param buf the buffer of data to send
196
 * @param size the number of bytes to send
197
 **/
198
void xbee_send(char* buf, int size)
199
{
200
	#ifdef ROBOT
201
	int i;
202
	for (i = 0; i < size; i++)
203
		xbee_putc(buf[i]);
204
	#else
205
	int ret = write(xbee_stream, buf, size);
206
	//success
207
	if (ret == size)
208
		return;
209
	if (ret == -1)
210
	{
211
		//interrupted by system signal, probably timer interrupt.
212
		//just try again
213
		if (errno == 4)
214
		{
215
			xbee_send(buf, size);
216
			return;
217
		}
218
		printf("Failed to write to xbee, error %i.\r\n", errno);
219
		return;
220
	}
221

  
222
	//write was interrupted after writing ret bytes
223
	xbee_send(buf + ret, size - ret);
224
	#endif
225
}
226

  
227
/**
228
 * Sends a string to the XBee.
229
 *
230
 * @param c the string to send to the XBEE
231
 **/
232
void xbee_send_string(char* c)
233
{
234
	xbee_send(c, strlen(c));
235
}
236

  
237
#ifndef ROBOT
238
void xbee_read(char* buf, int size)
239
{
240
	if (read(xbee_stream, buf, size) == -1)
241
		printf("Failed to read from xbee.\r\n");
242
}
243
#endif
244

  
245
/**
246
 * Enter into command mode.
247
 **/
248
void xbee_enter_command_mode()
249
{
250
	xbee_send_string("+++");
251
	xbee_wait_for_ok();
252
}
253

  
254
/**
255
 * Exit from command mode.
256
 **/
257
void xbee_exit_command_mode()
258
{
259
	xbee_send_string("ATCN\r");
260
	xbee_wait_for_ok();
261
}
262

  
263
/**
264
 * Enter API mode.
265
 **/
266
void xbee_enter_api_mode()
267
{
268
	xbee_send_string("ATAP 1\r");
269
	xbee_wait_for_ok();
270
}
271

  
272
/**
273
 * Wait until the string "OK\r" is received from the XBee.
274
 **/
275
void xbee_wait_for_ok()
276
{
277
	xbee_wait_for_string("OK\r", 3);
278
}
279

  
280
/**
281
 * Delay until the specified string is received from
282
 * the XBee. Discards all other XBee data.
283
 *
284
 * @param s the string to receive
285
 * @param len the length of the string
286
 **/
287
void xbee_wait_for_string(char* s, int len)
288
{
289
	char* curr = s;
290
	while (curr - s < len)
291
	{
292
		if (queue_is_empty(xbee_queue))
293
			continue;
294
		char c = (char)(int)queue_remove(xbee_queue);
295
		if (c == *curr)
296
			curr++;
297
		else
298
			curr = s;
299
	}
300
}
301

  
302
/**
303
 * Verifies that the packets checksum is correct.
304
 * (If the checksum is correct, the sum of the bytes
305
 * is 0xFF.)
306
 *
307
 * @param packet the packet received. This includes the first
308
 * three bytes, which are header information from the XBee.
309
 *
310
 * @param len The length of the packet received from the XBee
311
 *
312
 * @return 0 if the checksum is incorrect, nonzero
313
 * otherwise
314
 **/
315
int xbee_verify_checksum(char* packet, int len)
316
{
317
	unsigned char sum = 0;
318
	int i;
319
	for (i = 3; i < len; i++)
320
		sum += (unsigned char)packet[i];
321
	return sum == 0xFF;
322
}
323

  
324
/**
325
 * Returns the checksum of the given packet.
326
 *
327
 * @param buf the data for the packet to send
328
 * @param len the length of the packet in bytes
329
 *
330
 * @return the checksum of the packet, which will
331
 * become the last byte sent in the packet
332
 **/
333
char xbee_compute_checksum(char* buf, int len)
334
{
335
	int i;
336
	unsigned char sum = 0;
337
	for (i = 0; i < len; i++)
338
		sum += (unsigned char)buf[i];
339
	return 0xFF - sum;
340
}
341

  
342
/**
343
 * Adds header information and checksum to the given
344
 * packet and sends it. Header information includes
345
 * XBEE_FRAME_START and the packet length, as two bytes.
346
 *
347
 * @param buf the packet data
348
 * @param len the size in bytes of the packet data
349
 *
350
 **/
351
void xbee_send_frame(char* buf, int len)
352
{
353
	char prefix[3];
354
	prefix[0] = XBEE_FRAME_START;
355
	prefix[1] = (len & 0xFF00) >> 8;
356
	prefix[2] = len & 0xFF;
357
	char checksum = xbee_compute_checksum(buf, len);
358
	xbee_send(prefix, 3);
359
	xbee_send(buf, len);
360
	xbee_send(&checksum, 1);
361
}
362

  
363
/**
364
 * Sends an AT command to read a parameter.
365
 *
366
 * @param command the AT command to send. For exmaple,
367
 * use ID to read the PAN ID and MY to return the XBee ID.
368
 * See the XBee reference guide for a complete listing.
369
 **/
370
void xbee_send_read_at_command(char* command)
371
{
372
	xbee_send_modify_at_command(command, NULL);
373
}
374

  
375
/**
376
 * Sends the given AT command.
377
 *
378
 * @param command the AT command to send (e.g., MY, ID)
379
 * @param value the value to pass as a parameter
380
 * (or NULL if there is no parameter)
381
 **/
382
void xbee_send_modify_at_command(char* command, char* value)
383
{
384
	char buf[16];
385
	int i;
386
	
387
	buf[0] = XBEE_FRAME_AT_COMMAND;
388
	buf[1] = 1;
389
	buf[2] = command[0];
390
	buf[3] = command[1];
391
	int valueLen = 0;
392
	if (value != NULL)
393
	{
394
		valueLen = strlen(value);
395
		if (valueLen > 8)
396
		{
397
			WL_DEBUG_PRINT("AT Command too large.\r\n");
398
			return;
399
		}
400
		for (i = 0; i < valueLen; i++)
401
			buf[4 + i] = value[i];
402
	}
403
	xbee_send_frame(buf, 4 + valueLen);
404
}
405

  
406
/**
407
 * Send the specified packet.
408
 * 
409
 * @param packet the packet data to send
410
 * @param len the number of bytes in the packet
411
 * 
412
 * @param dest the ID of the XBee to send the packet to,
413
 * or XBEE_BROADCAST to send the message to all robots
414
 * in the PAN.
415
 * 
416
 * @param options a combination of the flags
417
 * XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and 
418
 * XBEE_OPTIONS_BROADCAST_ALL_PANS
419
 *
420
 * @param frame the frame number to associate this packet
421
 * with. This will be used to identify the response when
422
 * the XBee alerts us as to whether or not our message
423
 * was received.
424
 **/
425
void xbee_send_packet(char* packet, int len, int dest,
426
	char options, char frame)
427
{
428
	char buf[5];
429
	char prefix[3];
430
	int i;
431
	unsigned char checksum = 0;
432

  
433
	if (len > 100)
434
	{
435
		WL_DEBUG_PRINT("Packet is too large.\r\n");
436
		return;
437
	}
438

  
439
	//data for sending request
440
	buf[0] = XBEE_FRAME_TX_REQUEST_16;
441
	buf[1] = frame;
442
	buf[2] = (dest >> 8) & 0xFF;
443
	buf[3] = dest & 0xFF;
444
	buf[4] = options;
445

  
446
	//packet prefix, do this here so we don't need an extra buffer
447
	prefix[0] = XBEE_FRAME_START;
448
	prefix[1] = ((5 + len) & 0xFF00) >> 8;
449
	prefix[2] = (5 + len) & 0xFF;
450

  
451
	for (i = 0; i < 5; i++)
452
		checksum += (unsigned char)buf[i];
453
	for (i = 0; i < len; i++)
454
		checksum += (unsigned char)packet[i];
455
	checksum = 0xFF - checksum;
456
	xbee_send(prefix, 3);
457
	xbee_send(buf, 5);
458
	xbee_send(packet, len);
459
	xbee_send((char*)&checksum, 1);
460
}
461

  
462
/**
463
 * Reads a packet received from the XBee. This function
464
 * is non-blocking. The resulting packet is stored in dest.
465
 * Only returns transmission response packets and
466
 * received packets. The returned packet does not include
467
 * header information or the checksum. This method also
468
 * handles special packets dealt with by the XBee library,
469
 * and so should be called frequently while the XBee is in
470
 * use.<br><br>
471
 *
472
 * The first byte of the packet will be either
473
 * XBEE_TX_STATUS or XBEE_RX to indicated
474
 * a response to a sent message or a received message, 
475
 * respectively.<br><br>
476
 *
477
 * For a status response packet:<br>
478
 * The first byte will be XBEE_TX_STATUS.<br>
479
 * The second byte will be the frame number.<br>
480
 * The third byte will be the result. 0 indicates success,
481
 * and nonzero indicates that an error ocurred in 
482
 * transmitting the packet.<br><br>
483
 *
484
 * For a received packet:<br>
485
 * The first byte will be XBEE_RX.<br>
486
 * The second and third bytes will be the 16-bit
487
 * address of the packet's sender.<br>
488
 * The fourth byte is the signal strength.<br>
489
 * The fifth byte is 1 if the packet were sent to
490
 * a specific address, and 2 if it is a broadcast packet.<br><br>
491
 * 
492
 * @param dest set to the packet data
493
 * @return the length of the packet, or -1 if no packet
494
 * is available
495
 **/
496
int xbee_get_packet(unsigned char* dest)
497
{
498
	//start reading a packet with XBEE_FRAME_START
499
	if (currentBufPos == 0)
500
	{
501
		do
502
			if (queue_is_empty(xbee_queue))
503
				return -1;
504
		while ((char)(int)queue_remove(xbee_queue) != XBEE_FRAME_START);
505
		xbee_buf[0] = XBEE_FRAME_START;
506
		currentBufPos++;
507
	}
508

  
509
	int len = -1;
510
	if (currentBufPos >= 3)
511
		len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
512
		
513
	while (len == -1 //packet length has not been read yet
514
			|| currentBufPos < len + 4)
515
	{
516
		if (currentBufPos == 3)
517
			len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
518
		if (queue_is_empty(xbee_queue))
519
			return -1;
520
		xbee_buf[currentBufPos++] = (char)(int)queue_remove(xbee_queue);
521
	}
522
	
523
	currentBufPos = 0;
524
	
525
	if (!xbee_verify_checksum(xbee_buf, len + 4))
526
	{
527
		WL_DEBUG_PRINT("XBee checksum failed.\r\n");
528
		return -1;
529
	}
530

  
531
	//we will take care of the packet
532
	if (xbee_handle_packet(xbee_buf + 3, len))
533
		return -1;
534
	
535
	if (dest == NULL)
536
		return -1;
537
	
538
	int i;
539
	for (i = 3; i < len + 3; i++)
540
		dest[i - 3] = xbee_buf[i];
541
	return len;
542
}
543

  
544
/**
545
 * Handles modem status packets.
546
 *
547
 * @param status the type of status packet received.
548
 **/
549
void xbee_handle_status(char status)
550
{
551
	switch (status)
552
	{
553
		case 0:
554
			WL_DEBUG_PRINT("XBee hardware reset.\r\n");
555
			break;
556
		case 1:
557
			WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
558
			break;
559
		case 2:
560
			WL_DEBUG_PRINT("Associated.\r\n");
561
			break;
562
		case 3:
563
			WL_DEBUG_PRINT("Disassociated.\r\n");
564
			break;
565
		case 4:
566
			WL_DEBUG_PRINT("Synchronization lost.\r\n");
567
			break;
568
		case 5:
569
			WL_DEBUG_PRINT("Coordinator realignment.\r\n");
570
			break;
571
		case 6:
572
			WL_DEBUG_PRINT("Coordinator started.\r\n");
573
			break;
574
	}
575
}
576

  
577
/**
578
 * Handles AT command response packets.
579
 * @param command the two character AT command, e.g. MY or ID
580
 * @param result 0 for success, 1 for an error
581
 * @param extra the hex value of the requested register
582
 * @param extraLen the length in bytes of extra
583
 **/
584
void xbee_handle_at_command_response(char* command, char result,
585
	char* extra, int extraLen)
586
{
587
	if (result == 1)
588
	{
589
		WL_DEBUG_PRINT("Error with AT");
590
		WL_DEBUG_PRINT(command);
591
		WL_DEBUG_PRINT(" packet.\r\n");
592
	}
593
	WL_DEBUG_PRINT("AT");
594
	WL_DEBUG_PRINT(command);
595
	WL_DEBUG_PRINT(" command was successful.\r\n");
596
		
597
	if (command[0] == 'I' && command[1] == 'D')
598
	{
599
		xbee_panID = xbee_pending_panID;
600
		WL_DEBUG_PRINT("PAN ID set to ");
601
		WL_DEBUG_PRINT_INT(xbee_panID);
602
		WL_DEBUG_PRINT(".\r\n");
603
		return;
604
	}
605

  
606
	if (command[0] == 'C' && command[1] == 'H')
607
	{
608
		xbee_channel = xbee_pending_channel;
609
		WL_DEBUG_PRINT("Channel set to ");
610
		WL_DEBUG_PRINT_INT(xbee_channel);
611
		WL_DEBUG_PRINT(".\r\n");
612
		return;
613
	}
614
	
615
	if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
616
	{
617
		xbee_address = 0;
618
		int i;
619
		for (i = 0; i < extraLen; i++)
620
			xbee_address = (xbee_address << 8) + extra[i];
621

  
622
		WL_DEBUG_PRINT("XBee address is ");
623
		WL_DEBUG_PRINT_INT(xbee_address);
624
		WL_DEBUG_PRINT(".\r\n");
625

  
626
		if (xbee_address == 0)
627
		{
628
			WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
629
			exit(0);
630
		}
631
	}
632
}
633

  
634
/**
635
 * Attempts to handle the packet if it is dealt with
636
 * by the library.
637
 * We will handle the following packet types:
638
 *    Modem Status
639
 *    AT Command Response
640
 *
641
 * @param packet the packet to handle
642
 * @param len the length of the packet
643
 * 
644
 * @return 1 if we have handled the packet, 0 otherwise
645
 */
646
int xbee_handle_packet(char* packet, int len)
647
{
648
	char command[3] = {1, 2, 3};
649
	if (len <= 0) //this should not happend
650
	{
651
		WL_DEBUG_PRINT("Non-positive packet length.\r\n");
652
		return 0;
653
	}
654
	
655
	switch ((unsigned char)packet[0]) //packet type
656
	{
657
		case XBEE_FRAME_STATUS:
658
			xbee_handle_status(packet[1]);
659
			return 1;
660
		case XBEE_FRAME_AT_COMMAND_RESPONSE:
661
			command[0] = packet[2];
662
			command[1] = packet[3];
663
			command[2] = 0;
664
			xbee_handle_at_command_response(command,
665
				packet[4], packet + 5, len - 5);
666
			return 1;
667
	}
668
	return 0;
669
}
670

  
671
/**
672
 * Sets the personal area network id.
673
 *
674
 * @param id the new personal area network (PAN) id
675
 **/
676
void xbee_set_pan_id(int id)
677
{
678
	char s[3];
679
	s[0] = (id >> 8) & 0xFF;
680
	s[1] = id & 0xFF;
681
	s[2] = 0;
682
	xbee_pending_panID = id;
683
	xbee_send_modify_at_command("ID", s);
684
}
685

  
686
/**
687
 * Get the PAN ID for the XBee.
688
 * 
689
 * @return the personal area network id, or
690
 * XBEE_PAN_DEFAULT if it has not yet been set.
691
 **/
692
unsigned int xbee_get_pan_id()
693
{
694
	return xbee_panID;
695
}
696

  
697
/**
698
 * Set the channel the XBee is using.
699
 *
700
 * @param channel the channel the XBee will not use, 
701
 * between 0x0B and 0x1A
702
 *
703
 * @see xbee_get_channel
704
 **/
705
void xbee_set_channel(int channel)
706
{
707
	if (channel < 0x0B || channel > 0x1A)
708
	{
709
		WL_DEBUG_PRINT("Channel out of range.\r\n");
710
		return;
711
	}
712
	char s[3];
713
	s[0] = channel & 0xFF;
714
	s[1] = 0;
715
	xbee_pending_channel = channel;
716
	xbee_send_modify_at_command("CH", s);
717
}
718

  
719
/**
720
 * Returns the channel which the XBee is currently using.
721
 *
722
 * @return the channel the XBee is using
723
 *
724
 * @see xbee_set_channel
725
 **/
726
int xbee_get_channel(void)
727
{
728
	return xbee_channel;
729
}
730

  
731
/**
732
 * Get the 16-bit address of the XBee.
733
 * This is used to specify who to send messages to
734
 * and who messages are from.
735
 *
736
 * @return the 16-bit address of the XBee.
737
 **/
738
unsigned int xbee_get_address()
739
{
740
	return xbee_address;
741
}
742

  
branches/charging_station/code/projects/recharging/charging_station/serial.h
1
/*
2
	serial.h - Contains definitions and function prototypes for the RS232 serial port
3
*/
4

  
5
#ifndef _SERIAL_H
6
#define _SERIAL_H
7

  
8
// Tested baud rates
9
#define BAUD9600    103
10
#define BAUD115200  8  //Warning--3.5% error
11

  
12
// Untested baud rates that might be right --aaron
13
#define BAUD1M	     0
14
#define BAUD500K     1
15
#define BAUD250K     3
16
#define BAUD230400   3   //Warning--8.5% error
17
#define BAUD76800    12
18
#define BAUD57600    16  //Warning--2.1% error
19
#define BAUD38400    25
20
#define BAUD28800    34
21
#define BAUD19200    51
22
#define BAUD14400    68
23
#define BAUD4800     207
24
#define BAUD2400     416  //Might not work, since it needs some high bits set
25

  
26
// Function descriptions are available in serial.c
27

  
28
//zigbee
29
void xbee_init( unsigned int ubrr);
30
int xbee_putc(char c);
31
int xbee_puts(char *s);
32
int xbee_getc(void);
33
int xbee_getc_nb(void);
34
int xbee_puti(int x);
35

  
36
//db9
37
void usb_init( unsigned int ubrr);
38
int usb_putc(char c);
39
int usb_getc(void);
40
int usb_getc_nb(void);
41
int usb_puts(char *s);
42
int usb_puti(int x);
43
#endif
44

  
branches/charging_station/code/projects/recharging/charging_station/buzzer.h
1
/*
2
	buzzer.h - Contains function prototypes for running the buzzer
3
	
4
	
5
	author: Robotics Club, Colony Project
6
*/
7
#ifndef _BUZZER_H_
8
#define _BUZZER_H_
9

  
10
//Musical note definitions
11
//Source: http://www.answers.com/topic/piano-key-frequencies
12
#define	C4	260 //Middle C
13
#define	C4s	277 //C sharp
14
#define	D4	294
15
#define	D4s	311
16
#define	E4	330
17
#define	F4	349
18
#define	F4s	370
19
#define	G4	392
20
#define	G4s	415
21
#define	A4	440
22
#define	A4s	466
23
#define	B4	494
24
#define	C5	523
25

  
26
// Function descriptions can be found in buzzer.c
27
void buzzer_init(void);
28
void buzzer_set_val(unsigned int buzz_value);
29
void buzzer_set_freq(unsigned int buzz_freq);
30
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq);
31
void buzzer_off(void); 
32

  
33
#endif
branches/charging_station/code/projects/recharging/charging_station/xbee.h
1
/**
2
 * @file xbee.h
3
 * @brief Contains definitions for using the XBee
4
 *
5
 * Contains definitions for interfacing with the 
6
 * XBee module, from either a robot or a computer.
7
 * To use a robot, define ROBOT in wl_defs.h, and
8
 * to use a computer, don't define ROBOT.
9
 *
10
 * @author Brian Coltin, Colony Project, CMU Robotics Club
11
 **/
12

  
13
/**
14
 * @defgroup xbee XBee
15
 * @brief Interface with the XBee module
16
 *
17
 * Interface with the XBee module.
18
 *
19
 * @{
20
 **/
21

  
22
/*Definitions*/
23
/**@brief Unset PAN, uses XBee default **/
24
#define XBEE_PAN_DEFAULT 0xFFFF
25
/**@brief Unset channel, uses XBee default **/
26
#define XBEE_CHANNEL_DEFAULT 0
27
/**@brief Broadcast to all robots in the PAN **/
28
#define XBEE_BROADCAST 0xFFFF
29
/**@brief No special options **/
30
#define XBEE_OPTIONS_NONE 0x00
31
/**@brief Do not receive a TX_STATUS message from this packet **/
32
#define XBEE_OPTIONS_DISABLE_RESPONSE 0x01
33
/**@brief Send the packet to all PANS **/
34
#define XBEE_OPTIONS_BROADCAST_ALL_PANS 0x04
35
/**@brief A transmit status packet **/
36
#define XBEE_TX_STATUS 0x89
37
/**@brief A packet received from another XBee **/
38
#define XBEE_RX 0x81
39

  
40
/**@brief Initialize the XBee library **/
41
void xbee_lib_init(void);
42
/**@brief Uninitialize the XBee library **/
43
void xbee_terminate(void);
44
/**@brief Get a packet from the XBee **/
45
int xbee_get_packet(unsigned char* packet);
46
/**@brief Send a packet to the XBee **/
47
void xbee_send_packet(char* packet, int len, int dest,
48
	char options, char frame);
49
/**@brief Set the PAN ID for the XBee **/
50
void xbee_set_pan_id(int id);
51
/**@brief Get the XBee's PAN ID **/
52
unsigned int xbee_get_pan_id(void);
53
/**@brief Set the channel the XBee is currently using **/
54
void xbee_set_channel(int channel);
55
/**@brief Get the channel the XBee is currently using **/
56
int xbee_get_channel(void);
57
/**@brief Get the XBee's 16-bit address **/
58
unsigned int xbee_get_address(void);
59

  
60
/**@}**/ //end defgroup
61

  
branches/charging_station/code/projects/recharging/charging_station/Makefile
1
# Hey Emacs, this is a -*- makefile -*-
2
#----------------------------------------------------------------------------
3
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al.
4
#
5
# Released to the Public Domain
6
#
7
# Additional material for this makefile was written by:
8
# Peter Fleury
9
# Tim Henigan
10
# Colin O'Flynn
11
# Reiner Patommel
12
# Markus Pfaff
13
# Sander Pool
14
# Frederik Rouleau
15
#
16
#----------------------------------------------------------------------------
17
# On command line:
18
#
19
# make all = Make software.
20
#
21
# make clean = Clean out built project files.
22
#
23
# make coff = Convert ELF to AVR COFF.
24
#
25
# make extcoff = Convert ELF to AVR Extended COFF.
26
#
27
# make program = Download the hex file to the device, using avrdude.
28
#                Please customize the avrdude settings below first!
29
#
30
# make debug = Start either simulavr or avarice as specified for debugging, 
31
#              with avr-gdb or avr-insight as the front end for debugging.
32
#
33
# make filename.s = Just compile filename.c into the assembler code only.
34
#
35
# make filename.i = Create a preprocessed source file for use in submitting
36
#                   bug reports to the GCC project.
37
#
38
# To rebuild project do "make clean" then "make all".
39
#----------------------------------------------------------------------------
40

  
41

  
42
# MCU name
43
MCU = atmega128
44

  
45

  
46
# Processor frequency.
47
#     This will define a symbol, F_CPU, in all source code files equal to the 
48
#     processor frequency. You can then use this symbol in your source code to 
49
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
50
#     automatically to create a 32-bit value in your source code.
51
F_CPU = 16000000
52

  
53

  
54
# Output format. (can be srec, ihex, binary)
55
FORMAT = ihex
56

  
57

  
58
# Target file name (without extension).
59
TARGET = main
60

  
61

  
62
# List C source files here. (C dependencies are automatically generated.)
63
SRC = $(wildcard *.c)
64
#$(TARGET).c 
65

  
66

  
67
# List Assembler source files here.
68
#     Make them always end in a capital .S.  Files ending in a lowercase .s
69
#     will not be considered source files but generated files (assembler
70
#     output from the compiler), and will be deleted upon "make clean"!
71
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
72
#     it will preserve the spelling of the filenames, and gcc itself does
73
#     care about how the name is spelled on its command-line.
74
ASRC = 
75

  
76

  
77
# Optimization level, can be [0, 1, 2, 3, s]. 
78
#     0 = turn off optimization. s = optimize for size.
79
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
80
OPT = s
81

  
82

  
83
# Debugging format.
84
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
85
#     AVR Studio 4.10 requires dwarf-2.
86
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
87
DEBUG = 
88

  
89

  
90
# List any extra directories to look for include files here.
91
#     Each directory must be seperated by a space.
92
#     Use forward slashes for directory separators.
93
#     For a directory that has spaces, enclose it in quotes.
94
EXTRAINCDIRS = C:\WinAVR\include\fwr
95

  
96

  
97
# Compiler flag to set the C Standard level.
98
#     c89   = "ANSI" C
99
#     gnu89 = c89 plus GCC extensions
100
#     c99   = ISO C99 standard (not yet fully implemented)
101
#     gnu99 = c99 plus GCC extensions
102
CSTANDARD = -std=gnu99
103

  
104

  
105
# Place -D or -U options here
106
CDEFS = -DF_CPU=$(F_CPU)UL -DROBOT -DFIREFLY
107

  
108

  
109
# Place -I options here
110
CINCS =
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff