diff --git a/robotside/hexConverterv2.cpp b/robotside/hexConverterv2.cpp new file mode 100644 index 0000000..b3dea15 --- /dev/null +++ b/robotside/hexConverterv2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +int addr1 = 0x04; //primary address //successive i2c devices +int file_i2c; //primary address +int pwmVals[8]; +char *filename = (char*)"/dev/i2c-1"; +int i2c_slave_overflow = 32; + +void arrayCallback(const std_msgs::UInt16MultiArray::ConstPtr& array); + +bool writeStream(int file_i2c, string output) { + if (output.length() <= i2c_slave_overflow) { + if (write(file_i2c, output.c_str(), output.length()) != output.length()) { + printf("\tFailed to write to the i2c bus.\n"); + } + } + else { + printf("\t\tinput too long |:| overflow bounds exceeded and message voided\n"); + } + return true; +} + +bool readStream(int file_i2c, char buf[]) { + int n=32; //this should be the number of expected characters + if (read(file_i2c, buf, n) != n) { + printf("\ti2c read transaction |:| failed\n"); + } + else { + printf("\t%s\n", buf); + } +} + +string intToHexDig(int value) { + string cHex;//this block can be written as a case statement, but im lazy + if (value == 15) {cHex = 'f';} + else if (value == 14) {cHex = 'e';} + else if (value == 13) {cHex = 'd';} + else if (value == 12) {cHex = 'c';} + else if (value == 11) {cHex = 'b';} + else if (value == 10) {cHex = 'a';} + else if (value == 9) {cHex = '9';} + else if (value == 8) {cHex = '8';} + else if (value == 7) {cHex = '7';} + else if (value == 6) {cHex = '6';} + else if (value == 5) {cHex = '5';} + else if (value == 4) {cHex = '4';} + else if (value == 3) {cHex = '3';} + else if (value == 2) {cHex = '2';} + else if (value == 1) {cHex = '1';} + else if (value == 0) {cHex = '0';} + return cHex; +} + +string convertTo3BitHex(int decimal) { + string converted; + int firstChar = (int)(decimal/ (16 * 16)); + int firstRemainder = (int)(decimal % (16 * 16)); + int secondChar = (int)(firstRemainder / (16)); + int secondRemainder = (int)(firstRemainder % (16)); + int thirdChar = secondRemainder; + string cfirstChar, csecondChar, cthirdChar; + cfirstChar = intToHexDig(firstChar); + csecondChar = intToHexDig(secondChar); + cthirdChar = intToHexDig(thirdChar); + + converted = converted + cfirstChar; + converted = converted + csecondChar; + converted = converted + cthirdChar; +// cout << converted << endl; +// cout << "done"<::const_iterator it = array->data.begin(); it != array->data.end(); ++it){ + pwmVals[i] = *it; + i++; + } + cout << pwmVals < +#include + +#include "ros/ros.h" + +#include "std_msgs/MultiArrayLayout.h" +#include "std_msgs/MultiArrayDimension.h" + +#include "std_msgs/UInt16MultiArray.h" + +int main(int argc, char **argv) +{ + + + ros::init(argc, argv, "arrayPublisher"); + + ros::NodeHandle n; + + ros::Publisher pub = n.advertise("array", 1); + + while (ros::ok()) + { + std_msgs::UInt16MultiArray array; + //Clear array + array.data.clear(); + //for loop, pushing data in the size of the array + for (int i = 0; i < 8; i++) + { + //assign array a random number between 0 and 255. + array.data.push_back(rand() % 255); + } + //Publish array + pub.publish(array); + //Let the world know + ROS_INFO("I published something!"); + //Do this. + ros::spinOnce(); + //Added a delay so not to spam + sleep(2); + } + +}