Hello there,
We got 2 robots from university with paired Xbee Pro S1 modules (can only communicate with the paired counterpart). They are attached to a Nibo2 robot and I am trying to solve a maze with mine.
So one of my Nibos drives through a maze and writes down walls and paths (1 for wall, 0 for path).
I am saving them in an twodimensional array and want to send them to my other robot.
I am able to send simple data (like a 1 or a 0) but somehow everything gets ridiculous messed up when trying to loop through the array to send the data. I am getting numbers like -26669 which simply is not possible at all.
I hope someone is able to tell me what I am doing wrong.
My code to receive Data:
// libraries for standard functions of the Nibo2
#include <nibo/niboconfig.h>
#include <nibo/iodefs.h>
#include <nibo/bot.h>
// libary for the use of the display
#include <nibo/display.h>
// libary for the use of the graphical functions
#include <nibo/gfx.h>
// libary for standard input and output operations of the Nibo2
#include <stdio.h>
// includes interrupts
#include <stdint.h>
// includes header file for the use of the UART1-Port
//#include "uart0.h"
// include delay operations
#include <nibo/delay.h>
// includes SPI communication library
#include <nibo/spi.h>
// includes microcontroller hardware interrupts
#include <avr/interrupt.h>
// includes xBee Communication
// be aware to include it locally with ""
#include "xBee.h"
/**
* Main function of the helloXBee.c file.
* Receives and sends chars using the UART1-Port of the Nibo.
*/
int main(){
// initialize the robot
bot_init();
// initialize spi-port
spi_init();
// initialize the display and the graphical operations
display_init();
gfx_init();
// buffer for the chars, is needed to print the chars on the display
char text[20] = "";
// declare and initialize a variable for storing received characters
uint8_t x=52;
// prints text on the Display
sprintf(text, "Hallo XBEE");
gfx_move(0, 0);
gfx_print_text(text);
// initialize xBee Module
xBee_init();
int maze[10][10];
int row = 0, col = 0;
// Operation loop
while (row != 9 && col !=9) {
// if the receive buffer is not empty ->
if (xBee_receivedData())
{
// -> get next received data byte of the buffer
x = xBee_readByte();
if(x == 1){
if(row == 9){
row = 0;
col++;
maze[row][col] = 1;
x = 52;
} else {
maze[row][col] = 1;
x = 52;
row++;
}
}if(x == 0) {
row++;
maze[row][col] = 0;
x = 52;
}
}
// Delay for the operation loop
_delay_ms(5);
}
gfx_move(0, 40);
if(maze[9][9] == 1){
gfx_print_text("Stelle 9 9 ist 1");
} else {
x = maze [9][9];
sprintf(text, "9 9 ist: %c", (char)x);
gfx_move(0, 15);
gfx_print_text(text);
}
return 0;
}
My Code to send Data:
// libraries for standard functions of the Nibo2
#include <nibo/niboconfig.h>
#include <nibo/iodefs.h>
#include <nibo/bot.h>
// libary for the use of the display
#include <nibo/display.h>
// libary for the use of the graphical functions
#include <nibo/gfx.h>
// libary for standard input and output operations of the Nibo2
#include <stdio.h>
// includes interrupts
#include <stdint.h>
// includes header file for the use of the UART1-Port
//#include "uart0.h"
// include delay operations
#include <nibo/delay.h>
// includes SPI communication library
#include <nibo/spi.h>
// includes microcontroller hardware interrupts
#include <avr/interrupt.h>
// includes xBee Communication
// be aware to include it locally with ""
#include "xBee.h"
/**
* Main function of the helloXBee.c file.
* Receives and sends chars using the UART1-Port of the Nibo.
*/
int main() {
// initialize the robot
bot_init();
// initialize spi-port
spi_init();
// initialize the display and the graphical operations
display_init();
gfx_init();
// buffer for the chars, is needed to print the chars on the display
char text[20] = "";
// declare and initialize a variable for storing received characters
uint8_t x;
int maze[10][10] = {{1,0,0,1,1,1,1,1,1,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,0,1}
};
// initialize xBee Module
xBee_init();
int row = 0, col = 0;
// Operation loop
while (1) {
if (xBee_readyToSend()) {
if(col == 9 && row == 8){
gfx_print_text("Habe gesendet!");
}
x = maze[row][col];
xBee_sendByte(x);
row++;
if(row == 9){
row = 0;
col++;
}
}
// Delay for the operation loop
_delay_ms(500);
}
gfx_move(0, 30);
gfx_print_text("Fertig");
return 0;
}
xBee.c:
#include <avr/io.h>
#include "xBee.h"
//#include <nibo/uart0.h>
//#include <nibo/uart0.h>
/**
* Function which initialize the UART0 Port of the Nibo and sets the baudrate of the port
* Default baude rate of the modules is 9600
*/
void initUART0(){
uart0_set_baudrate(9600);
uart0_enable();
}
/**
* Initializes and enables the xBee Module
*/
void xBee_init() {
//LED ausschalten
DDRE &= ~(1 << 0);
initUART0();
}
/**
* Transmits a single byte
*/
void xBee_sendByte(uint8_t data) {
// -> puts the char into the buffer to send the char
uart0_putchar(data);
}
/**
* Checks if xBee is ready to transmit data
*/
uint8_t xBee_readyToSend() {
return !uart0_txfull();
}
/**
* Checks if xBee received something
*/
uint8_t xBee_receivedData() {
return !uart0_rxempty();
}
/**
* Reads the next byte of received data
*/
uint8_t xBee_readByte() {
// -> get char out of the buffer
return uart0_getchar();
}
xBee.h:
#ifndef NIBO_xBee_H_
#define NIBO_xBee_H_
#ifndef NIBO_USE_UART0
#define NIBO_USE_UART0
#endif
// The XBee module communicates over UART
#include <nibo/uart0.h>
#include <stdint.h>
/**
* Initializes and enables the xBee Module
*/
void xBee_init();
/**
* Transmits a single byte
*/
void xBee_sendByte(uint8_t data);
/**
* Checks if xBee is ready to transmit data
*/
uint8_t xBee_readyToSend();
/**
* Checks if xBee received something
*/
uint8_t xBee_receivedData();
/**
* Reads the next byte of received data
*/
uint8_t xBee_readByte();
#endif // NIBO_xBee_H