Thesis Day 2

Previous Work

Previously, I had confirmed that the Lidar sensor does work and provides accurate readings. I have also been able to properly wire an arduino to my team's Jonbot. The plan for today was to have data be sent and recieved from both Jonbot and the arduino.

Programming

After doing some research, I determined that the best way to have communications between the two devices would be to use the Arduino Wire library for the Arduino and to use the FRC I2C class on Jonbot.

Jonbot Reading Code


//Inside TeleopPeriodic
int READ_SIZE = 32;
byte[] read = new byte[READ_SIZE];//create a byte array to hold the incoming data
RobotMap.arduino.read(4, read.length, read);//use address 4 on i2c and store it in data
String arduinoMessage = new String(read);//create a string from the byte array
int stop = arduinoMessage.indexOf(""+ (char)255);
//Finds ending of message arduinoMessage = arduinoMessage.substring(0, stop < 0 ? arduinoMessage.length() : stop);
//Stops the message where needed System.out.println("Data: " + arduinoMessage);

Jonbot Writing Code


String messageToArduino = "Hello Arduino";
byte[] messageArray = new byte[messageToArduino.length()];
for (int i = 0; i < messageArray.length; i++)
Array[i]=(byte) messageToArduino.charAt(i);
RobotMap.arduino.writeBulk(messageArray, messageArray.length);

Arduino Setup


void setup() {
Wire.begin(4); // join i2c bus with address #4
Serial.begin(9600);
Wire.onReceive(receiveEvent); // Method to run when Jonbot sends a message
Wire.onRequest(requestEvent); // Method to run when Jonbot asks for a message
Serial.println("Starting");
}

Arduino Reading Code


void receiveEvent(int n) {
Serial.println("Recieve Called");
String message = "";
Serial.print("Message from Jonbot: ")
while(Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
}

Arduino Writing Code


void requestEvent() {
Wire.write("How are you Jonbot?");
}

After some research I was able to write the above code for both devices. I was sucessfully able to have "Hello Arduino" appear on the serial monitor for the arduino and "How are you Jonbot?" on the drivestation terminal. To test whether I could change the method which the Arduino runs depending on the message which Jonbot sends, I simply replaced the Arduino reading code with the following:
char c = Wire.read();
Serial.print(c);
if(c == 'A')
     Wire.onRequest(secondRequestEvent);

And then had secondRequestEvent() send a different message. The code worked, when Jonbot sent the char 'A' it would call the secondRequestEvent, and if he did not send it, it would continue sending the original message.

Adjusting the Lidar Code

After I confirmed I was able to send messages between the two devices and change what was sent, I decided to adjust the Lidar code, so that the lidar would report a scan when Jonbot told it too. The example code that came with the Sweep library initiated a scan when the user typed in "start". To have it begin a scan when Jonbot wanted, I first added the same arduino setup code written above to the setup method in this code. I set the Wire.onReceive() input to the listenForUserInput method and commented out the Wire.onRequest() line for the time being. I then rewrote the listenForUserInput method to the following:
void listenForUserInput(int n){
int robotMessage = Wire.read();
if(robotMessage == 1 && currentState == STATE_WAIT_FOR_USER_INPUT){
Serial.println("Going to adjust settings");
currentState = STATE_ADJUST_DEVICE_SETTINGS;
}
}

In the Jonbot's code, I added a command which sent an int value of 1 to the Arduino in the execute method and had isFinished() return true, so that it would run once. I then had the command run .whenPressed() for a joystick trigger. The code worked, when I would click the trigger, the arduino would go through the same process as if I had typed "start" and would collect and print three scans of the environment with the lidar.

Future Work

Being able to run scans after recieving messages from the robot was only the first step. Next time I work with the robot I hope to be able to send the data collected in the scans to Jonbot. After doing so, I will optimize the arduino side code wherever possible and then attempt to launch a gui on the driverstation. I have been working on the code for this gui seperatley, and will write about that soon.


© All Rights Reserved.