openreroc_pwm.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include <openreroc_pwm/openreroc_pwm.h>
00003 #include <stdio.h>
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 #include <errno.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <fcntl.h>
00010 #include <termio.h>
00011 #include <signal.h>
00012 
00013 void config_console();
00014 void allwrite(int fd, unsigned char *buf, int len);
00015 int fd_32;
00016 
00017 unsigned int convert_state(unsigned char dir_left,
00018                           unsigned int para_left,
00019                           unsigned char dir_right,
00020                           unsigned int para_right);
00021 
00022 void chatterCallback(const openreroc_pwm::openreroc_pwm msg)
00023 {
00024 
00025     unsigned int state;
00026     int rc;
00027     unsigned char dir_left;
00028     unsigned int para_left;
00029     unsigned char dir_right;
00030     unsigned int para_right;
00031 
00032     dir_left = msg.dir_left;
00033     para_left = msg.para_left;
00034     dir_right = msg.dir_right;
00035     para_right = msg.para_right;
00036 
00037     state = convert_state(dir_left,para_left,dir_right,para_right);
00038     printf("input value = %x\n", state);
00039     printf("dir_left = %d para_left = %d\n", dir_left,para_left);
00040     printf("dir_right = %d para_right = %d\n", dir_right,para_right);
00041     rc = write(fd_32, &state, 4);
00042     // allwrite(fd_32, (unsigned char *)&state, 4);
00043 }
00044 
00045 int main(int argc, char **argv)
00046 {
00047 
00048   config_console();
00049   fd_32 = open("/dev/xillybus_write_32", O_WRONLY);
00050   ros::init(argc, argv, "openreroc_pwm");
00051   ros::NodeHandle n;
00052   ros::Subscriber sub_openreroc_pwm = n.subscribe("openreroc_pwm", 1000, chatterCallback);
00053   ros::spin();
00054 
00055   close(fd_32);
00056   return 0;
00057 }
00058 
00059 
00060 
00061 void config_console() {
00062   struct termio console_attributes;
00063 
00064   if (ioctl(0, TCGETA, &console_attributes) != -1) {
00065     // If we got here, we're reading from console
00066 
00067     console_attributes.c_lflag &= ~ICANON; // Turn off canonical mode
00068     console_attributes.c_cc[VMIN] = 1; // One character at least
00069     console_attributes.c_cc[VTIME] = 0; // No timeouts
00070 
00071     if (ioctl(0, TCSETAF, &console_attributes) == -1)
00072       fprintf(stderr, "Warning: Failed to set console to char-by-char\n");    
00073   }
00074 }
00075 
00076 void allwrite(int fd, unsigned char *buf, int len) {
00077   int sent = 0;
00078   int rc;
00079 
00080   while (sent < len) {
00081     rc = write(fd, buf + sent, len - sent);
00082   
00083     if ((rc < 0) && (errno == EINTR))
00084       continue;
00085 
00086     if (rc < 0) {
00087       perror("allwrite() failed to write");
00088       exit(1);
00089     }
00090   
00091     if (rc == 0) {
00092       fprintf(stderr, "Reached write EOF (?!)\n");
00093       exit(1);
00094     }
00095  
00096     sent += rc;
00097   }
00098 } 
00099 
00100 unsigned int convert_state(unsigned char dir_left,
00101                           unsigned int para_left,
00102                           unsigned char dir_right,
00103                           unsigned int para_right){
00104   unsigned int state=0, temp;
00105 
00106   temp = para_left << 17;
00107   state = state + temp;
00108   temp = dir_left << 16;
00109   state = state + temp;
00110   temp = para_right << 1;
00111   state = state + temp;
00112   temp = dir_left;
00113   state = state + temp;
00114 
00115   return state;
00116 
00117 }           


openreroc_pwm
Author(s): Kazushi Yamashina
autogenerated on Thu Feb 11 2016 23:57:32