Ultrasonic sensor for Robot

Dependents:   Robot

Files at this revision

API Documentation at this revision

Comitter:
azadmehr
Date:
Thu Sep 01 08:54:38 2016 +0000
Commit message:
La til ultralyd sensor

Changed in this revision

ultrasonic.cpp Show annotated file Show diff for this revision Revisions of this file
ultrasonic.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r e2f25a48d4d9 ultrasonic.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ultrasonic.cpp	Thu Sep 01 08:54:38 2016 +0000
@@ -0,0 +1,86 @@
+ #include "ultrasonic.h"
+    
+    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin)
+    {
+        _updateSpeed = updateSpeed;
+        _timeout = timeout;
+    }
+    
+    ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int))
+    :_trig(trigPin), _echo(echoPin)
+    {
+        _onUpdateMethod=onUpdate;
+        _updateSpeed = updateSpeed;
+        _timeout = timeout;
+        _t.start ();
+    }
+    void ultrasonic::_startT()
+    { 
+        if(_t.read()>600)
+        {
+            _t.reset ();
+        }
+        start = _t.read_us ();
+    }
+        
+    void ultrasonic::_updateDist()
+    {
+        end = _t.read_us ();
+        done = 1;
+        _distance = (end - start)/6;       
+        _tout.detach();
+        _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);   
+    }
+    void ultrasonic::_startTrig(void)
+    {
+            _tout.detach();
+            _trig=1;             
+            wait_us(10);        
+            done = 0;            
+            _echo.rise(this,&ultrasonic::_startT);   
+            _echo.fall(this,&ultrasonic::_updateDist);
+            _echo.enable_irq ();
+            _tout.attach(this,&ultrasonic::_startTrig,_timeout);
+            _trig=0;                     
+    }
+    
+    int ultrasonic::getCurrentDistance(void)
+    {
+        return _distance;
+    }
+    void ultrasonic::pauseUpdates(void)
+    {
+        _tout.detach();
+        _echo.rise(NULL);
+        _echo.fall(NULL);
+    }
+    void ultrasonic::startUpdates(void)
+    {
+        _startTrig();
+    }
+    void ultrasonic::attachOnUpdate(void method(int))
+    {
+        _onUpdateMethod = method;
+    }
+    void ultrasonic::changeUpdateSpeed(float updateSpeed)
+    {
+        _updateSpeed = updateSpeed;
+    }
+    float ultrasonic::getUpdateSpeed()
+    {
+        return _updateSpeed;
+    }
+    int ultrasonic::isUpdated(void)
+    {
+        //printf("%d", done);
+        d=done;
+        done = 0;
+        return d;
+    }
+    void ultrasonic::checkDistance(void)
+    {
+        if(isUpdated())
+        {
+            (*_onUpdateMethod)(_distance);
+        }
+    }
diff -r 000000000000 -r e2f25a48d4d9 ultrasonic.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ultrasonic.h	Thu Sep 01 08:54:38 2016 +0000
@@ -0,0 +1,47 @@
+#ifndef MBED_ULTRASONIC_H
+#define MBED_ULTRASONIC_H
+
+#include "mbed.h"
+
+class ultrasonic
+{
+    public:
+        /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/
+        ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout);
+        /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/
+        ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int));
+        /** returns the last measured distance**/
+        int getCurrentDistance(void);
+        /**pauses measuring the distance**/
+        void pauseUpdates(void);
+        /**starts mesuring the distance**/
+        void startUpdates(void);
+        /**attachs the method to be called when the distances changes**/
+        void attachOnUpdate(void method(int));
+        /**changes the speed at which updates are made**/
+        void changeUpdateSpeed(float updateSpeed);
+        /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/
+        int isUpdated(void);
+        /**gets the speed at which updates are made**/
+        float getUpdateSpeed(void);
+        /**call this as often as possible in your code, eg. at the end of a while(1) loop,
+        and it will check whether the method you have attached needs to be called**/
+        void checkDistance(void);
+    private:
+        DigitalOut _trig;
+        InterruptIn _echo;
+        Timer _t;
+        Timeout _tout;
+        int _distance;
+        float _updateSpeed;
+        int start;
+        int end;
+        volatile int done;
+        void (*_onUpdateMethod)(int);
+        void _startT(void);
+        void _updateDist(void);
+        void _startTrig(void);
+        float _timeout;
+        int d;
+};
+#endif
\ No newline at end of file