Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions robotside/hexConverterv2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <string>
#include <stdio.h>
#include <ros/ros.h>
#include <stdlib.h>
#include <std_msgs/MultiArrayLayout.h>
#include <std_msgs/MultiArrayDimension.h>
#include <std_msgs/UInt16MultiArray.h>



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"<<endl;
return converted;
}



int main(int argc, char **argv) {
//Open i2c bus

if((file_i2c = open(filename, O_RDWR)) < 0) {
printf("\tFailed to open the i2c bus\n");
}
if(ioctl(file_i2c, I2C_SLAVE, addr1) < 0) {
printf("\tFailed to acquire bus access and/or talk to slave.\n");
}


ros::init(argc, argv, "pwmSubscriber");
ros::NodeHandle n;
ros::Subscriber subPWM = n.subscribe("array",1,arrayCallback);
while(ros::ok()){
ros::spinOnce();
}
return 0;
}



void arrayCallback(const std_msgs::UInt16MultiArray::ConstPtr& array){
int i = 0;
for(std::vector<short unsigned int>::const_iterator it = array->data.begin(); it != array->data.end(); ++it){
pwmVals[i] = *it;
i++;
}
cout << pwmVals <<endl;
char hexVals[32];

string sinput1;
for (int i =0; i<8; i++){
int valueOf = pwmVals[i];
sinput1 = sinput1 + convertTo3BitHex(valueOf);
if(i<7){sinput1 = sinput1 + "~";}
}
cout << sinput1;
cout << endl;
writeStream (file_i2c, sinput1);
char buf2[32];
readStream(file_i2c, buf2);
return;
}
31 changes: 31 additions & 0 deletions robotside/i2c setup readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
STEP I:
sudo apt-get install libi2c-dev i2c-tools
STEP II:
sudo i2cdetect -y -r 1

should output...
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: — — — — — — — —

STEP III:
connect the arduino to the nvidia with the provided arduino script and rerun
sudo i2cdetect -y -r 1

should now output
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — 04 — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: — — — — — — — —

2 changes: 0 additions & 2 deletions robotside/src/placeholder.txt

This file was deleted.

42 changes: 42 additions & 0 deletions robotside/tester.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>

#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<std_msgs::UInt16MultiArray>("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);
}

}