POLARIS Serial Communication Protocol

From Lofaro Lab Wiki
Revision as of 21:46, 13 May 2015 by Jfogle (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Git hub location --- POLARIS Serial communication location module side

https://github.com/LofaroLabs/POLARIS/blob/master/POLARIS_serial_comm/serial_comm.c

Example of serial communication reciver --- robot side

https://github.com/LofaroLabs/POLARIS/blob/master/robot_serial_com/serial_comm.c

Upon detection detection of USB->Serial connection

http://wiki.lofarolabs.com/index.php/Creation_of_special_Kernel_event_handling_(USB_auto_connection_for_serial_communication)

The port will auto open a serial connection and always remain in a wait state for communication from the robot using open from the stdio.h library

uart0_filestream = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCKING);
O_RDWR - Open for reading and writing.
O_NONBLOCK - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status if there is no input immediately available (instead of blocking). Likewise, write requests can also return immediately with a failure status if the output can't be written immediately.
O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.

The connection is also set to a baud rate of 9600 with a termios struct using the termios.h header file

struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //Baud rate 9600, CSize 8, Ignore Modem Status, Enable Reciver
options.c_iflag = IGNPAR; //Ignore characters with parity errors
options.c_oflag = 0; //
options.c_lflag = 0; //
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);

Location Module recieves message to send the robot the map

"req: Map\n"

Location module sends the map to the robot -----This will return a basic jpg image sourced from the location on the module device

FILE *fp;
fp = fopen("~/Documents/map.jpg","r");
size_t br;
p_tx_buffer = &tx_buffer[0];
int buf[256];
int i;
while((br=fread(buf,1,255,fp))!=0){
 for(i=0;i<br;i++){
  *p_tx_buffer++ = buf[i];
  if(p_tx_buffer==255){
   transmit_bytes();
   p_tx_buffer = &tx_buffer[0];
  }
 }
}
transmit_bytes();

Location Module receives message to send the robot the meta data file

"req: Meta Data\n"

Location Module finds the meta file and transmits the information-----This information will be sourced from the location on the module device parsed in segments of 256 char types and sent to the robot. The user of the robot will be responsible for storing the data as received.

FILE *fp;
fp = fopen("~/Documents/meta.txt","r");
size_t br;
p_tx_buffer = &tx_buffer[0];
int buf[256];
int i;
while((br=fread(buf,1,255,fp))!=0){
 for(i=0;i<br;i++){
  *p_tx_buffer++ = buf[i];
  if(p_tx_buffer==255){         //if the transmit buffer is full
   transmit_bytes();            //transmit the information 
   p_tx_buffer = &tx_buffer[0]; //reset buffer 
  }
 }
}
transmit_bytes();

Location Module recives message to send location information

"req: Location\n"

Location Module sends the current location of the module-----This data will be transmitted in the order: x, y, z, THETA in string type and is required to recast by the robot to desired type for use.

//IPC
int localizerOpen = ach_open(&chan_localizer, CHAN_LOCALIZER_NAME, NULL);
assert(ACH_OK == localizerOpen);
/*Create initial structure to read from*/
struct localizer L_localizer;
memset(&L_localizer, 0, sizeof(L_localizer));
/*for size check*/
size_t fs;
while(1){
 // IPC
 /*Get the current localization values*/
 localizerOpen = ach_get(&chan_localizer, &L_localizer, sizeof(L_localizer), &fs, NULL, ACH_O_LAST);
 if(ACH_OK != localizerOpen){
  assert(sizeof(L_localizer) == fs);
 }
 uint32_t valid = L_localizer.valid;
 double x = L_localizer.x;
 double y = L_localizer.y;
 uint32_t z = L_localizer.z;
 double THETA = L_localizer.THETA;
 int i;
 for(i=0;i<4;i++){   //For each value x,y,z,THETA convert to string and transmit
  if (i==0){
   p_tx_buffer = &tx_buffer[0];
   snprintf(p_tx_buffer,sizeof(tx_buffer),"%f",x);
   int length = (int)floor(log10((float)x)) + 1;
   p_tx_buffer = p_tx_buffer + length;
   transmit_bytes();
  }
  if (i==1){
   p_tx_buffer = &tx_buffer[0];
   snprintf(p_tx_buffer,sizeof(tx_buffer),"%f",y);
   int length = (int)floor(log10((float)y)) + 1;
   p_tx_buffer = p_tx_buffer + length;
   transmit_bytes();
  }
  if (i==2){
   p_tx_buffer = &tx_buffer[0];
   snprintf(p_tx_buffer,sizeof(tx_buffer),"%f",z);
   int length = (int)floor(log10((float)z)) + 1;
   p_tx_buffer = p_tx_buffer + length;
   transmit_bytes();
  }
  if (i==3){
   p_tx_buffer = &tx_buffer[0];
   snprintf(p_tx_buffer,sizeof(tx_buffer),"%f",THETA);
   int length = (int)floor(log10((float)THETA)) + 1;
   p_tx_buffer = p_tx_buffer + length;
   transmit_bytes();
  }
 }
}