TF2 Sentry - Page Work in Progress


Ultrasonic Sensor System

This system will be used for object detection. It will use a total of 3 ultrasonic sensors. I will use these to see how far an object is from the sentry, and once an object is known to be within a certain distance, I can tell the STM controller to carry out certain routines. The reason I have 3 ultrasonic sensors rather than 1 is to provide a 90 degree view. One will be mounted looking 45 degrees to the left, one 45 degrees to the right, then one centered.

Sensor Selection
This was pretty straight forward. I saw a sensor commonly used in projects on youtube and in fact saw this specific one when I did my senior capstone project. It's the HC-SR04.
Calculations for the motor
Object Detection
There are 4 pins for the sensor. VDD, GND, TRIG and ECHO. VDD will be at 5V. According to the datasheet, the trigger pin needs to be sent a 10uS pulse for the sensor to operate. The sensor will then send out a sonic burst. Then the echo pin will go high with a range in proportion. So TRIG will be an output and ECHO an input (on the stm32 that is). I will have to use the STM to see how long the ECHO pin is high to calculate the distance.
Calculations
The calculations are as follows. The STM is gonna tell me how long ECHO was high to a variable totalTime. Since the totalTime is the time it took for the signal to go to the object and come back, we need to divide it by 2. So I have a variable totalFinal = totalTime/2. Why is time needed? To calculate the distance, we need the totalFinal and velocity of sound because Distance = totalFinal * Vs. Since I am using cm units in the code, velSound = 34300 cm/s. So distance = totalFinal * velSound.
Programming the STM32F746ZG
This is the fun part and one of the first things I programmed apart from a blinking led routine.

The first thing I noticed is there is no supported function for delaying in microseconds. So I will have to write one. This actually isn't too difficult. I can use of the many available timers. I will use timer 3. What I basically need to do is just set the prescalar and keep the counter period to the max. For example, if if the APB is set to 80Mhz, I set the presclar to 80-1 which gives me 1Mhz. Why 1Mhz? Freq = 1/Time. 1Mhz = 1/1uS. The period is left at the max so I can delay up to that amount. For the code, it's straight forward. I'll have the function input an amount to delay by. Then the function will reset the counter to 0, and then have a while loop where while the counter is less than the inputted delay, just stay on the while loop until it reaches that condition.

Now the next part is my sensor routine. It will basically make the TRIG go high, delay by 10 us, then go low.

Now to read the ECHO pin's duration of when it is high, I need to use interrupts and special timers. These timers on this stm32 are TIM1 and TIM8. The reason being is they have something called input capture. It detects a transition by a signal and counts for me so I can have the pulse width of the echo pin, using two channels. There are 6 channels available on TIM1, but channel 5 and 6 do not have this functionality, so I can only count for 2 sensors on TIM1. TIM8 will be used for a 3rd. Channel 1 and 3 will use direct mode and Channel 2 and 4 will use indirect mode, respectively. The reason being is indirect mode means to use the same pin Channel 1 and Channel 3 are connected to, rather then a different pin. Since I only have the echo pin, it is required. I then set channel 1 and 3 as a rising edge, the start of a echo signal and channels 2 and 4 to falling.

Everytime this happens, an interrupt occurs, so I have a callback function ready. In my callback function, I detect if this is the first capture (which is when the echo goes high). If so, just increment the firstCapture variable, so the next time I know the duration has been recorded and can use the value. Now let's say I have the duration. I call a function to calculate the distance using the calculations I discussed in the above section. Then I reset the firstCapture variable. Since I have three sensors and two different timers, my code reflects this. For example I check which timer is calling the callback, and I have different variables for the distance calculated for each sensor!

Finally after all of this, we are returned to the while loop in main. All I have to do now is check if the distance is within a range I set, and carry out the functions I want, those being to play audio, make the motor turn the sentry, make the sentry fire, etc. Also, there comes the question what happens if more than 1 sensor has an object in range? Well I just turn to the closest object, which is how it works in the game.
Results
Below is a video of a LED turning on and off depending on the distance my Metrocard is from the sensor.

Back to Motor System