Yeongsoo Kim / Mbed 2 deprecated Mecha_Lane_Sensor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
yeongsookim
Date:
Mon Nov 16 22:37:13 2020 +0000
Commit message:
Init commit

Changed in this revision

Sensor/LaneSensor.cpp Show annotated file Show diff for this revision Revisions of this file
Sensor/LaneSensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r a0e9db1628f1 Sensor/LaneSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensor/LaneSensor.cpp	Mon Nov 16 22:37:13 2020 +0000
@@ -0,0 +1,61 @@
+#include "LaneSensor.h"
+
+LaneSensor::LaneSensor(PinName lll,PinName ll,PinName l,PinName m,PinName r,PinName rr,PinName rrr)
+    : sensorsIn_(lll,ll,l,m,r,rr,rrr)
+{
+    error_=0.0;
+    prevError_=0.0;
+    //sensorsIn_.mode(PullNode);
+}
+float LaneSensor::getError()
+{
+    float errorSum=0;
+    int errorCount=0;
+    float errorConstant =22.86;
+
+    int sensorIn=sensorsIn_ & sensorsIn_.mask();
+    if((sensorIn&0x1)==0x0) {
+        errorSum+=errorConstant*3.0;
+        errorCount++;
+    }
+    if((sensorIn&0x2)==0x0) {
+        errorSum+=errorConstant*2.0;
+        errorCount++;
+    }
+    if((sensorIn&0x4)==0x0) {
+        errorSum+=errorConstant*0.7;
+        errorCount++;
+    }
+    if((sensorIn&0x8)==0x0) {
+        errorSum+=0.0;
+        errorCount++;
+    }
+    if((sensorIn&0x10)==0x0) {
+        errorSum+=errorConstant*-0.7;
+        errorCount++;
+    }
+    if((sensorIn&0x20)==0x0) {
+        errorSum+=errorConstant*-2.0;
+        errorCount++;
+    }
+    if((sensorIn&0x40)==0x0) {
+        errorSum+=errorConstant*-3.0;
+        errorCount++;
+    }
+
+
+    if(errorCount==0) {
+        error_=prevError_;
+    } else {
+        error_=errorSum/((float)errorCount);
+    }
+    prevError_=error_;
+
+    return error_;
+}
+
+int LaneSensor::getData()
+{
+
+    return sensorsIn_ & sensorsIn_.mask();;
+}
\ No newline at end of file
diff -r 000000000000 -r a0e9db1628f1 Sensor/LaneSensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensor/LaneSensor.h	Mon Nov 16 22:37:13 2020 +0000
@@ -0,0 +1,21 @@
+#ifndef MBED_LANESENSOR_H
+#define MBED_LANESENSOR_H
+
+#include "mbed.h"
+
+
+
+
+class LaneSensor
+{
+public:
+    LaneSensor(PinName lll,PinName ll,PinName l,PinName m,PinName r,PinName rr,PinName rrr);
+    float getError();
+    int getData();
+
+protected:
+    float error_,prevError_;
+    BusIn  sensorsIn_;
+};
+
+#endif //MBED_DISTANCESENSOR_H
diff -r 000000000000 -r a0e9db1628f1 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 16 22:37:13 2020 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+#include "LaneSensor.h"
+
+//To plot with usb, set as below.
+Serial pc(USBTX,USBRX); // Tx, Rx Pin
+
+//Set each gpio to see the output of the hall sensor as a led
+LaneSensor laneSensor(p11,p12,p13,p14,p15,p16,p17);
+
+//Interrupt is generated every 1ms and count is increased by 1
+unsigned int uiFlag_1ms = 0;
+unsigned int uiFlag_50ms = 0;
+
+void counter_1ms ()
+{
+    uiFlag_1ms++;
+    uiFlag_50ms++;
+}
+
+int main()
+{
+    wait(1);
+
+    ////Set the 1ms thicker.
+    Ticker ticker_1ms;
+    ticker_1ms.attach(&counter_1ms, 0.001);
+    
+
+    while(1) 
+    {
+        // Every 50 ms,
+        if(uiFlag_50ms >= 50) {
+            uiFlag_50ms = 0;
+            
+            int data = laneSensor.getData();
+            float error = laneSensor.getError();
+            pc.printf ("data: %d, error: %f \r\n", data, error);
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r a0e9db1628f1 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 16 22:37:13 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file