Sonar library for Project Lab
Doxygen Documentation
- Control of HC-SR04 Sonar
- Control of Power HD 1160A Servo
- Many work modes
- Constant measurement at fixed position
- Constant measurement in two different sweep modes
- Single measurement at given angles using interupts and polling
- Support for averaging of measurements
- Pseudo feedback for servo.
- Labview interface
Connect sonar and servo pins as follows:
Sonar:
- PTE20 <-> Sonar Echo pin
- PTE21 <-> Sonar Trigger pin
- 5V <-> Sonar Vcc
- Gnd <-> Sonar Gnd
We recommend using voltage divider on Echo pin
Servo:
- PTE22 <-> Servo Yellow cable
- 5V <-> Servo Red Cable
- Gnd <-> Servo Brown Cable
We recommend connecting 470uF capacitor near Servo power connectors.
-
Copy
sonar.h, sonar.c, servo.c
andservo.h
files to your project. -
Include both headers in your main.c file
#include "sonar.h" #include "servo.h"
-
In your main function initialize Sonar anf Servo with proper options.
int main(void){
Sonar_init(SINGLE);
Servo_init(MANUAL, SCAN_AND_GO);
(...)
}
Check documentation for details: Servo_init() and Sonar_init().
-
Decide how you want to retrieve measurements results
-
Via Interupt (Both Single and Sweep measurements modes) In
sonar.c
file at the bottom, there is a function called SonarDistHandler. This function is called when Sonar obtains some usable data. For example if you want to send the results you can use it like this: To trigger measurement you can use CONTINUOUS Sonar mode or SonarStartMeas function.void SonarDistHandler(uint16_t distance_cm, int32_t angle){ /* Your code here */ char buffor[12]; sprintf(buffor, "%04d,%04hu\n",angle,distance_cm); bt_sendStr(buffor); }
-
Via Pooling If you just want to easly get single measurement, use SonarGetDistance function somewhere in your code. For example, to get distance measurement at angle 45deg use
int result = SonarGetDistance(new_deg);
-
-
Change Sonar and Servo work modes during runtime if needed. You should only use these functions for this purpose. Do NOT modify the content of any Servo or Sonar variables by hand!
-
In
sonar.h
andservo.h
there are few parameters which can be changed, depending on needs. For example, how many samples to average or measurement interval in continous mode. Before you try and modyfy any of them, read the comments!
There are sevral work modes available.
The easiest way to get distance is to use SonarGetDistance or SonarStartMeas function. Difference between those two funtions is simple: The first one will constantly pool the Sonar until it retrieves the result. Not very efficient, but very simple to use. The second one will trigger a measurement and exit. When Sonar gets the result, it will trigger SonarDistHandler.
If you want to get a steady stream of measurements, use Sonar in CONTINUOUS mode. Use SonarDistHandler to retrieve data. If you want to change sonar position, use ServoMoveByDegree. Remember to set Servo to manual mode first!
There are two Sweep modes available:
- Sweep and Go Sonar will move constantly by angle defined by SERVO_STEP_DEG. At each step it will try to measure the distance and return the result via SonarDistHandler.
- Sweep and Lock Sonar will move until it finds something closer than ServoLockRange. You can change this value using ServoChangeLockRange function. When the lock is lost, servo will resume sweep until it locks again.
We created an intreface for testing sonar. LINK - EXE.
**Note: It requires bluetooth module to work.
If you need labview files, send us an email.