At this point, I have been able to retrieve readings from the Lidar on the Arduino, and have the Arduino and Jonbot communicate. I plan to today send over the Lidar sensor readings from the Arduino to Jonbot.
Note: All code written today was for testing purposes, there is a lot of clean up that can be done, I plan to do all that soon.
My goal was to first send one full sensor reading at a time to Jonbot. I was able to start the scan easily with a push of trigger, causing the roborio to send a signal to the Arduino which would then begin a scan, code which I wrote when I last worked with the devices. Currently, this process of collecting a scan would include setting the speed of the sensor and confirming the correct speed before actually beginning the scan. I knew I could change this to occur in the setup() method, but I wanted to eliminate the variable of me interferring with correct code from being a factor in something not working. With or without this speed confirmation, the I2C transaction() method could not be used to send the start message and recieve the response because the data collection would take longer on the Arduino side. So instead, I created a command which would continuously run as the defualt command of the Arduino subsystem and repeatedly call the I2C read() method in execute(). If the first value of the byte[] the read command recieved was less than 0, this would mean the Arduino had not collected the reading yet, and the read command would do nothing.
For the Arduino code of sending the reading data, my first attempt was to edit the printCollectedData() method so that it would be called by the onRequest() method to send data to Jonbot. The original program was written to have states in order to determine which method should be running, so since the onRequest() method would get called over and over again (since the read command in Jonbot would be currently calling it continuously), I made it so that if the state was not STATE_REPORT_COLLECTED_DATA, then a byte array only containing -1 would be sent to Jonbot. I then adjusted the code so that it printed the angle, distance, and signal strength values each from their respective 500 length byte arrays, it would store the values in a 1500 length byte array. Upon completion it would send that byte array to Jonbot. I wasn't sure whether an array of that length could be sent over and sure enough Jonbot's driverstation gave an error when I tried to enable it. I had never seen this error before and I wasn't sure whether it was unable to create the error in memory or if the I2C was able to send over an array of that length. I tried sending the array of angles of length 500 as a byte array from the arduino to Jonbot, but that gave the same error. Through these trials I understood that the best procedure would be to send over each scan point as a byte array of length 3 consisting of the angle, distance, and signal strength.
Knowing I should send the data in groups of three, I thought it may be best to send it directly from the gatherSensorReading method, rather than the printCollectedData() method. So I switched the gather method to be called by the onRequest() and inserted the same arduino state if statement, as described in the previous paragraph. I then added code to store the values in a length 3 byte array to then send to Jonbot. When I tested this, I saw that only a few scan points were sent over (Thinking back, this could have possibly happened due to errors on my part, I will revist this when I clean up my code). Due to frustration, I decided to go about a slightly different approach of sending this data.
I decided to create a seperate method, sendToRobot(), which would serve as the method called by onRequest(). It would use two global variables, a byte array of length 500 (not 1500 because I am only sending 1 scan rather than the three scans as in the default example code) and an int value of the last index of the array sent. The byte array would get filled with the scan point values in the printCollectedData() method and would set the lastIndex variable to 0. Then the state of the arduino would change to my added state of STATE_SEND_TO_ROBOT. During this state, the sendToRobot() method would send the a byte array of the next three values from the lastIndexSent value onwards to Jonbot (E.X. if lastIndexSent = 0, arduino would send values in indexes 0, 1, 2, corresponding to angle, distance, and strength ). The lastIndexSent value would then get updated by having three added to it, and once it was greater than or equal to the value 500, the state of the arduino would then change. This worked! The values would get sent to the robot, and on the Robot's code I stored an ArrayList of byte arrays. I then created a command in which each scan point would get printed when I clicked a joystick button. When examining these vlaues, I realized that I had failed to remember by sending the scan point values as bytes --- when they were originally ints --- I was losing the true values of that info. I would have to deal with this issue next time I worked on the project.
I need to fix the loss of info from int to byte and I can do this by sending the info as strings rather than bytes. After applying this fix, I will need to see if I can have a gui to display the data on the driver station. Then I can work to clean my arduino side code. After all this, I should have lidar readings being sent to Jonbot quickly and I can then begin to write logic for the robot to follow a path based on what is far and what is close to it. I initially wanted the robot to have all the reading data and determine which direction it should turn so that I could also display the environment it sees in a gui, but I am considering having the arduino determining the direction for the robot and then sending that to the robot instead.
© All Rights Reserved.