SELF-BALANCING SEGBOT WITH OBSTACLE DETECTION CAPABILITY
ME 461 – UIUC Final Project
by Sagar Sachdev
INTRODUCTION
The main goal for this project was to create a self-balancing segbot (self-balancing robot) and add an obstacle detection capability to this, i.e., the segbot stops if it detects an obstacle. For this project, I would like to thank Texas Instruments for supplying the kit which included the C2000 Launchpad XL’s TMS320F28379D Microcontroller, as well as Professor Dan Block at the University of Illinois at Urbana-Champaign (d-block@illinois.edu) for supplying the sensors and parts and providing the necessary guidance needed to carry out this project. Any information regarding the sensors and parts should be directed to him.
REQUIRED SOFTWARE
Code Composer Studio 10.1.1, Tera Term (on Windows) or CoolTerm (on Mac); Make sure to set Baudrate in Coolterm/TeraTerm to be 115200
ADDITIONAL SENSORS/PARTS
An additional part that was added to the project was the HC-SR04 Ultrasonic sensor as well as some wires. These wires were connected from the ultrasonic sensor to the microcontroller in the following order:
VCC = +5V – Pin on J3
TRIG = GPIO0 – Pin 40
ECHO = GPIO19 – Pin 3
GND = Any ground pin on the microcontroller
In addition, there were two 3D printed Kickstands that were printed and attached to the green board using screws. The “. stl” CAD files which are used for 3D printing are attached with this project file. Two gear motors (GM25-370) which accepted a max Voltage of DC 6V. The motors were attached, and the entire project can be seen in the images below:
PROCESS:
BALANCE CONTROL:
- This particular aspect of the segbot was developed during a Lab for the ME 461 class. The balance control of the segbot was tuned using MATLAB and Simulink based model which utilizes state-space pole placement tuning using the control law of U = -K*x. More information about this can be found in here along with the sample values for the poles. For the purpose of this project, tuning was done through trial and error and the eigenvectors used were p = [-446.4162, -8.9051, -1.2000]
- The steering for the Segbot uses the key ‘3’ to go forward, ‘q’ to turn left, ‘r’ to turn right and ‘s’ to go backwards. Any other key can be pressed on the keyboard to stop the Segbot.
ULTRASONIC SENSOR INTEGRATION:
For the Ultrasonic sensor integration, the following steps need to have been followed:
- Define the following global variables before the code has started (after the #include):
__interrupt void ecap1_isr(void); (This causes a void interrupt called ecap1_isr)
void hc_sr04_trigger(void); (This initializes the hc_sr04_trigger and creates a void function where no return value needs to be entered)
void setup_led_GPIO(void); (This initializes the setup_led_GPIO and creates a void function where no return value needs to be entered)
int32_t echocount = 0; (This creates a variable of type 32-bit integer called echocount, which counts the number of echos and initializes its value to 0)
float echoduration = 0; (This creates a variable of type float called echoduration, which records the duration of the echos and initializes its value to 0)
float echodistance = 0; (This creates a variable called echodistance, which records the distance the echo has travelled and initializes its value to 0)
uint16_t triggercount = 0; (This creates a variable called triggercount, which records the number of triggers and initializes its value to 0)
uint16_t triggerstate = 0; (This creates a variable called triggerstate, which records the state of the trigger and initializes its value to 0)
- The placement and connections can be found using the PinMUX table that was provided to us as part of the ME461: Computer Control of Mechanical Systems course at the University of Illinois at Urbana-Champaign
- After the initialization, it is important to check the registers in the main() function. This is done to allow the correct GPIO pins are referenced, the correct CPU timers, the correct interrupts are enabled, and correct values are being transmitted from and read in the PieVectTable whether the functions are called in the main() function
- A good practice that I followed in my code is to write two “printf” functions to display the values of echocount, echodistance, echoduration, triggercount as well as triggerstate. These allowed me to print the values of the aforementioned variables in teraterm and see if the ultrasonic sensor was working in the first place before I decided to set the motor control conditions using it. The echodistance was especially helpful as I used it in the motor control to set a condition that if the distance was less than or equal to 15 cm, the segbot must stop moving. This means that if the echo is reflected after travelling a distance of 15 cm or less, the segbot has detected an obstacle (using the ultrasonic sensor) and must cease operations no matter the command given to it by the keyboard controls. The second printf function was used to determine the speed of each wheel of the segbot, the tilt value as well as the gyro values. This can be referenced from the lab report linked in the “Balance Control” section
- The above conditional statement was set in the SWI_isr which is where the motor control for self-balancing takes place. It is imperative that the echo condition be set in the same function where the self-balancing conditions are set
- The hc_sr_04 function must be triggered in one of the timer functions. For the purpose of this project, I chose cpu_timer_0_isr, however any cpu_timer function can be used. The important thing is to go back to main and check the registers and change them so that the correct cpu_timer function is being called
- The hc_sr_04 function is used to set conditions to activate the ultrasonic sensor. If the trigger state is less than 2 and if it is equal to 0, the GPIO0, which corresponds to the Trigger Pin of the Ultrasonic sensor, is cleared and the trigger state is set to 1. The trigger state is then incremented by 1 and the whole hc_sr_04 function runs again. Additionally, if the trigger count is greater than or equal to 2 and is less than 11, and if the trigger state is equal to 1, then the GPIO0 pin is cleared again and the trigger value is set to 2. The trigger count is then incremented by 1 and the function is repeated. And finally, if the triggerstate is not less than 2 and is not between 2 and 11, yet is equal to 2, then the trigger state is set to 2, the trigger count is set to 0 and the trigger state is set to 0
- Another important thing to check is the InitECapture functions and edit the registers within. In this case, the following registers are set:
o The ecap register is first reset
o The ecap register is released from the reset
o All the ecap interrupts are disabled
o The loading of the capture results is disabled
o The counter is stopped
o The counter is cleared
o The counter phase register is cleared
o The following data is derived from Chapter 16 of the TI F28379D Technical Reference Manual
o ECAP control register 1 is set to 0xC144
§ bit 15-14 11: FREE/SOFT, 11 = ignore emulation suspend
§ bit 13-9 00000: PRESCALE, 00000 = divide by 1
§ bit 8 1: CAPLDEN, 1 = enable capture results load
§ bit 7 0: CTRRST4, 0 = do not reset counter on CAP4 event
§ bit 6 1: CAP4POL, 1 = falling edge
§ bit 5 0: CTRRST3, 0 = do not reset counter on CAP3 event
§ bit 4 0: CAP3POL, 0 = rising edge
§ bit 3 0: CTRRST2, 0 = do not reset counter on CAP2 event
§ bit 2 1: CAP2POL, 1 = falling edge
§ bit 1 0: CTRRST1, 0 = do not reset counter on CAP1 event
§ bit 0 0: CAP1POL, 0 = rising edge
o ECAP control register 2 is set to 0x0096
§ bit 15-11 00000: reserved
§ bit 10 0: APWMPOL, not used
§ bit 9 0: CAP/APWM, 0 = capture mode
§ bit 8 0: SWSYNC, 0 = no action (no s/w synch)
§ bit 7-6 10: SYNCO_SEL, 10 = disable sync out signal
§ bit 5 0: SYNCI_EN, 0 = disable Sync-In
§ bit 4 1: TSCTRSTOP, 1 = enable counter
§ bit 3 0: RE-ARM, 0 = don't re-arm
§ bit 2-1 11: STOP_WRAP, 11 = wrap after 4 captures
§ bit 0 0: CONT/ONESHT, 0 = continuous mode
o ECAP all desired interrupts are enabled at a value of 0x0008
§ bit 15-8 0's: reserved
§ bit 7 0: CTR=CMP, 0 = compare interrupt disabled
§ bit 6 0: CTR=PRD, 0 = period interrupt disabled
§ bit 5 0: CTROVF, 0 = overflow interrupt disabled
§ bit 4 0: CEVT4, 0 = event 4 interrupt disabled
§ bit 3 1: CEVT3, 1 = event 3 interrupt enabled
§ bit 2 0: CEVT2, 0 = event 2 interrupt disabled
§ bit 1 0: CEVT1, 0 = event 1 interrupt disabled
§ bit 0 0: reserved
Link to Explanation: https://drive.google.com/file/d/1007BtC-ntMCx-FnDu6jfUxhSbGhzI1Hf/view?usp=sharing
Link to Video of the Segbot: https://drive.google.com/file/d/108hChtUfD9HkCLgPmbLJtW5r2t816ZUH/view?usp=sharing
Comments