This instructable will go over the LabVIEW code that I wrote to control my Sumobot. This will be an in-depth explanation of the code. If you're not interested in the explanation, the LabVIEW VI is linked in the last step. LabVIEW 2014 and Linx 3.0 are required to run this code. The code shown is the code for the BeagleBone BlackSumobot, it is very similar to the code used for the Raspberry PiSumobot. The only difference is the Motor control loop, since the Raspberry Pi 2 does not have built-in PWM. Here is a link showing the way we used LabVIEW to mimic a PWM signal.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Front Panel
Step 2: Code Overview
The top while loop uses a UDP read to collect the data sent over from the controller, the middle loop gathers information from the sensors by reading from PmodAD2 and the bottom loop uses information from the top 2 loops to control the motors of the Sumobot.
Step 3: LINX and Sensor Initialization
We start with a LINX open set to Local I/O because we are using a Raspberry Pi 2 and the BeagleBone Black and the code that you write in LabVIEW actually gets deployed to the board. We then open up a reference to the PmodAD2 and feed in the I2C channel and address that we specified on the front panel. After we enter the While loop, it splits into three different While loops, each of the loops is explained in detail in the next steps.
Step 4: Sensors
The middle while loop starts by reading from the PmodAD2; we specify that we will be reading from 3 channels since we have 3 line sensors. The output is unbundled into each specific voltage value here, and they are compared to 1.7, if one of them is greater than 1.7 then the robot is on white. We also compare the entire bundle to 1.2 to make sure all of the sensors are being read. The last part of this loop is a case structure that will determine what will happen in your bottom loop. If Auto is set to true this case structure will be entered.
This will set Auto Error to true if one of the sensors reads below 1.2 and will set Auto Sensor to true if either of the front 2 sensors detects white. It will also set Sensor to false. If Auto is set to false this case structure will be entered.
This case sets Sensor to true if one of the sensors reads below 1.2 or above 1.7 volts. It also sets Auto Sensor and Auto Error to false.
Step 5: WiFi Communication
The top while loop is used to collect the data from the controller. Before entering the loop, a UDP open block opens a UDP socket. The IP address and port fed in here match the one fed into the UDP write in the controller’s code. The UDP read outputs the string we sent over, and we feed it into a “unflatten from string” and specify its type to output our data bundle. Next, it is fed into an unbundle block which outputs each value from the Pmod Joysticks. The Joysticks Y values are on the outside, and the button values are in the middle. The values for the joysticks should always be between 190 and 900. The button values will be 1 if the joystick is pressed down, 2 if one of the top buttons is pressed down and 0 if none of the buttons are pressed.
In the case where none of the buttons are pressed the 0 output from the left button will trigger this case. It then checks to see if the right button is equal to 2, if it isn’t then it outputs false which is turned into a 0 here.
The 0 then triggers the case above which adjusts the value so the right and left speed output are between 0 and 1 which is the input range for the duty cycle.It also checks the direction of the thumbstick, setting the direction true if it is forward and false if backward.
If one of the top buttons is pressed it will output a 2 and the case above will be triggered, this case will trigger the 1 case shown below which will put both motors to full speed and then set the direction.
If only the right bumper is pressed it will set the left direction to positive and the right direction will stay negative which will spin the robot to the right. The robot spins left if the left button is being pushed and it moves straight if both buttons are pushed.
If both buttons thumb sticks are pressed down at once the Sumobot switches to autonomous mode. When the left thumbstick is pressed this value(elaborate) becomes a one which triggers the 1 case shown above. If the other thumbstick is pressed this value will be a 1 which will be multiplied by 2 and will then trigger the 2 case shown below.
If the bots aren’t currently set to auto then the case structure shown below will have X set to 1 which will activate a case that will turn auto on.
The next time it loops through the true case here will now be activated and X will then be set to 0.
The next time you hit both triggers the 0 case will be activated which will then turn off autonomous mode.
If bot stops receiving signals from the controller the bot it will make the Error Boolean true.
Step 6: Motor Control
The bottom loop controls the speed and direction of the Sumobot. A case structure around the outside checks the state of the Sumobot.
If there is a problem with a connection to the controller or a sensor or one of the sensors is connecting white it will move to the true case(shown above) which sets the duty cycle of each to 0 and a while loop prevents the case from being reset until both top buttons are pressed and then it checks again.If none of these Booleans are true it will move to the false case.
This case sets the Direction and Duty Cycle for each of the motors, the input into the direction will be true for forward and false for reverse. A digital write is opened for the right direction and the left direction. The input into the direction will be true for forward and false for reverse. Then we have a PWM block for each side that sets the duty cycle, a value between 0(rest) and 1(max speed). If Auto is set to true, the Sumobot will be in this case.
It will move forward until one of the front sensors detect white when it does you can see that the auto sensor Boolean will change to true which will switch to the true case. The right direction will be set to true and the left will be set to false. (show the robot turning) This means the robot will turn when it senses white, I have a time delay that allows it to keep turning farther than the moment it stops seeing white, the lower the time delay is set the less it will turn after the sensor stops seeing white and the closer it will follow the outer border.
If auto is set to false, the Sumobot will enter this case.
Inside this case structure, you can see that the direction and speed for both motors is being set by a local variable. These local variables are being set in the top loop.
Step 7: Final Steps
To end this code, we would press the stop button which will exit all of the loops, then when it's out of the outer while loop we close the reference to the PmodAD2 and to LINX as well as the UDP socket.
The code for the BeagleBone Black and Raspberry Pi 2 Sumobot are attached below. The bottom motor control loop is the only difference between the BeagleBone Black, for a better explanation of the Raspberry Pi code go to this link and for the Sumobot setup instructable go to this link. Thank you for reading my instructable, please comment with any questions you may have.