Difference between revisions of "POLARIS Serial Communication Protocol"

From Lofaro Lab Wiki
Jump to: navigation, search
(Created page with "Upon detection detection of USB->Serial connection http://wiki.lofarolabs.com/index.php/Creation_of_special_Kernel_event_handling_(USB_auto_connection_for_serial_communicatio...")
 
Line 2: Line 2:
 
  http://wiki.lofarolabs.com/index.php/Creation_of_special_Kernel_event_handling_(USB_auto_connection_for_serial_communication)
 
  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
+
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.
  
Location Module recieves message to send the robot the map
+
'''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"
 
  "req: Map\n"
Location module sends the map to the robot
+
'''Location module sends the map to the robot'''
  "ans: "+Map.pgm+"\n"
+
  FILE *fp;
Location Module recieves message to send the robot the meta data file
+
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"
 
  "req: Meta Data\n"
Location Module sends meta file information line by line
+
'''Location Module finds the meta file and transmits the information'''
  "ans: Meta Data Transmitting"
+
  FILE *fp;
  char line[255];//buffer
+
fp = fopen("~/Documents/meta.txt","r");
  //send data line by line
+
  size_t br;
  while(fgets(line,255,mfile)!=NULL){
+
p_tx_buffer = &tx_buffer[0];
  std::string send(line);
+
  int buf[256];
   asio::write(port,asio::buffer(send.c_str(), send.length()));
+
int i;
  printf("%s",send.c_str());
+
  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
 +
  }
 +
  }
 
  }
 
  }
Location Module recives message to send location information
+
transmit_bytes();
 +
'''Location Module recives message to send location information'''
 
  "req: Location\n"
 
  "req: Location\n"
Location Module sends the current location of the module with a update rate of 5hz
+
'''Location Module sends the current location of the module'''
  "ans: Location Stream Started"
+
  //IPC
  "Current Location: "+Location+"\n"
+
  int localizerOpen = ach_open(&chan_localizer, CHAN_LOCALIZER_NAME, NULL);
Location is collected using the following
+
assert(ACH_OK == localizerOpen);
ros::Subscriber sub = n.subscribe("pose",1000,LocationCallBack); //subscriber to advertiser pose
+
'''/*Create initial structure to read from*/'''
  ros::Rate r(5); //5hz update rate
+
  struct localizer L_localizer;
  while (ros::ok())
+
memset(&L_localizer, 0, sizeof(L_localizer));
{
+
/*for size check*/
  ros::spinOnce();
+
size_t fs;
   r.sleep();
+
  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();
 +
  }
 +
  }
 
  }
 
  }

Revision as of 05:27, 13 May 2015

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

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

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

//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();
  }
 }
}