this code is for inverted pendulum balancer. it is based on two-loop PID controller. One controller is for Angle and another one is for velocity.

Dependencies:   HCSR04 L298HBridge mbed

Revision:
0:489498e8dae5
diff -r 000000000000 -r 489498e8dae5 distance/distance.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/distance/distance.cpp	Fri Aug 25 21:10:23 2017 +0000
@@ -0,0 +1,27 @@
+//////////////////////////////////////////////////////////////////////////////////
+// Company: edinburgh of university
+// Engineer: ZEjun DU
+// 
+// Create Date: 2017/08/20 13:06:52
+// Design Name: Inverted Pendulum Balancer
+// Module Name: acquiring real-time distance
+// Tool Versions: “Keil 5” or “Mbed Complie Online”
+// Description: this part is to get real-time distance from distance sensor
+//                           
+// 
+//////////////////////////////////////////////////////////////////////////////////
+#include "distance.h"
+
+HCSR04 usensor(p10,p11);//this defines the pins of Distance sensor of HCSR04
+float home = 0;//this is a offset to set the original
+float distance;//this is final distance which should be outputed
+float distance_original;     
+
+////////////////////////////////////////////////////////////// 
+////this part is to get distance without filter
+//////////////////////////////////////////////////////////////
+void get_distance(void){
+    usensor.start();//start the distance sensor
+    distance_original = usensor.get_dist_cm();//acquiring real-time distance
+    distance =  distance_original - home ;//acquiring real-time distance after configuring the original
+    }