1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#define PI = 3.14159265358979323846264338327950288419716
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <cmath>
#include <iostream>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
using namespace std;
const double r=0.125, R=0.55, ratio=127; // initialize constants
int main(int argc, char **argv)
{
unsigned ms1, ms2, ms3, ms4; //motor speeds of omnimaxbot
int i, j, wr, rd, x=0, y=0, z=0, fd, numSent=0;
char parseChar[1], stringIn[50];
// unsigned long bytes_read = 0; //Number of bytes read from port
// unsigned long bytes_written = 0; //Number of bytes written to the port
int bStatus;
struct termios options; //Contains various port settings
// attempt to open serial port
fd = open("/dev/HCS12",O_RDWR | O_NOCTTY | O_NDELAY);
// set settings for serial port
system("stty -F /dev/HCS12 115200 cs8 -cstopb -parity -icanon hupcl -crtscts min 1 time 1");
// check for errors opening port
if (fd == -1 )
{
printf("open_port: Unable to open /dev/HCS12");
}
else // if no error
{
fcntl(fd, F_SETFL,0);
printf("Test Port HCS12 has been successfully opened and %d is the file description\n",fd);
}
// get current settings of serial ports
tcgetattr(fd, &options);
// set the read and write speeds
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
// set parity
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
if (bStatus != 0)
{
cout << "byte status error!" << endl;
}
// initalize node
ros::init(argc, argv, "joyTalker");
ros::NodeHandle joyTalker;
// Publish to topic joyChatter
ros::Publisher pub = joyTalker.advertise<std_msgs::String>("joyChatter", 1000);
ros::Rate r(10);
while (joyTalker.ok())
{
parseChar[0]=0;
for(j=0; j<50; j++)
{
stringIn[j]=0;
}
i=0;
while(parseChar[0] != 10)
{
if(i==50) // exceeds the allowable size
{
break;
}
rd = read(fd,parseChar,1);
// printf("%s ",parseChar);
if ((parseChar[0] > 43) && ((parseChar[0] < 58) || (parseChar[0]==32)))
{
stringIn[i]=parseChar[0];
i++;
}
if (wr != 0)
{
cout << "byte status error!!!" << endl;
}
// kinematics of the omnimaxbot
ms1 = ((x/sqrt(2))+y+(R*tan((z/ratio)*(2*PI))))/((2*PI)*r);
ms2 = (-(x/sqrt(2))+y+(R*tan((z/ratio)*(2*PI))))/((2*PI)*r);
ms3 = ((x/sqrt(2))+y-(R*tan((z/ratio)*(2*PI))))/((2*PI)*r);
ms4 = (-(x/sqrt(2))+y-(R*tan((z/ratio)*(2*PI))))/((2*PI)*r);
fd = open("/dev/Driver1&2",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1 )
{
perror("open_port: Unable to open /dev/Driver1&2");
}
// send to ROS node for communication
numSent = sprintf(stringIn, "%3d,%3d\n", ms1, ms2);
// status[0] = Serial_SendBlock(stringIn, numSent, &numSentS);
fd = open("/dev/Driver3&4",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1 )
{
perror("open_port: Unable to open /dev/Driver3&4");
}
// send to ROS node for communication
numSent = sprintf(stringIn, "%3d,%3d\n", ms3, ms4);
// status[0] = Serial_SendBlock(stringIn, numSent, &numSentS);
}
printf("got this string %s\n",stringIn);
// convert message to string for ROS node communication
std_msgs::String msg;
std::stringstream ss;
ss << stringIn;
ROS_INFO("%s", ss.str().c_str());
msg.data = ss.str();
// publish data under topic joyChatter
pub.publish(msg);
ros::spinOnce();
}
return 0;
}
|