Yeongsoo Kim / Mbed 2 deprecated Mecha_Servo_n_Distance_sensor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
yeongsookim
Date:
Mon Nov 11 00:06:25 2019 +0000
Commit message:
19.11.11 mechatronics lecture distrubution; PSD (distance sensor) and Servo motor control

Changed in this revision

Actuator/Servo.cpp Show annotated file Show diff for this revision Revisions of this file
Actuator/Servo.h Show annotated file Show diff for this revision Revisions of this file
Plotting/Plotting.cpp Show annotated file Show diff for this revision Revisions of this file
Plotting/Plotting.h Show annotated file Show diff for this revision Revisions of this file
Sensor/DistanceSensor.cpp Show annotated file Show diff for this revision Revisions of this file
Sensor/DistanceSensor.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Actuator/Servo.cpp	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,41 @@
+#include "Servo.h"
+
+
+Servo::Servo(PinName IN): m_PWM(IN)
+{
+    m_degree=0;
+    m_width= MID+(0.0005/90.0)*m_degree;
+    m_period_ticker.attach(callback(this, &Servo::setPeriod),0.02);
+    m_width_timeout.attach(callback(this, &Servo::setWidth),m_width);
+    m_PWM=1;
+}
+
+void Servo::setWidth()
+{
+    m_PWM=0;
+}
+
+void Servo::setPeriod()
+{
+    
+    m_width_timeout.attach(callback(this, &Servo::setWidth),m_width);
+    m_PWM=1;
+}
+
+float Servo::getDegree()
+{
+    return m_degree;
+}
+
+void Servo::update(float degree)
+{
+    if(degree>MAX) {
+        degree=MAX;
+    }
+    if(degree<-1*MAX) {
+        degree=MAX*-1;
+    }
+
+    m_degree=degree;
+    m_width= MID+(0.0005/90.0)*degree;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Actuator/Servo.h	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,26 @@
+#ifndef MBED_SERVO_H
+#define MBED_SERVO_H
+
+#include "mbed.h"
+
+#define MAX 20.0
+#define MID 0.001434
+
+class Servo
+{
+private:
+    DigitalOut m_PWM;
+    Ticker m_period_ticker;
+    Timeout m_width_timeout;
+    float m_degree;
+    float m_width;
+    
+    void setPeriod();
+    void setWidth();
+public:
+    Servo(PinName IN);
+    void update(float degree);
+    float getDegree();
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plotting/Plotting.cpp	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,50 @@
+#include "Plotting.h"
+
+Plotting::Plotting()
+{
+    for(int i=0; i<32; i++) {
+        datas[i]=0;
+    }
+    dataCount=0;
+}
+
+void Plotting::reset()
+{
+    for(int i=0; i<32; i++) {
+        datas[i]=0;
+    }
+    dataCount=0;
+}
+
+void Plotting::put(float data, int index)
+{
+    datas[index]=data;
+    dataCount++;
+}
+
+void Plotting::put(int data, int index)
+{
+    datas[index]=(float)data;
+    dataCount++;
+}
+
+void Plotting::put(unsigned int data, int index)
+{
+    datas[index]=(float)data;
+    dataCount++;
+}
+
+void Plotting::send(Serial *port)
+{
+    port->putc(0xAA);
+    port->putc(0xBB);
+    port->putc(0xCC);
+    port->putc((char)(dataCount*4));
+    for(int i=0; i<dataCount; i++) {
+        char *bytePtr = (char *)&(datas[i]);
+        port->putc(*bytePtr);      // reverse the order of these lines if you have endian issues
+        port->putc(*(bytePtr+1));
+        port->putc(*(bytePtr+2));
+        port->putc(*(bytePtr+3));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plotting/Plotting.h	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,21 @@
+#ifndef MBED_PLOTTING_H
+#define MBED_PLOTTING_H
+
+#include "mbed.h"
+
+class Plotting
+{
+private:
+    float datas[32];
+    int dataCount;
+public:
+    Plotting();
+    
+    void reset();
+    void put(float data, int index);
+    void put(int data, int index);
+    void put(unsigned int data, int index);
+    void send(Serial *port);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensor/DistanceSensor.cpp	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,13 @@
+#include "DistanceSensor.h"
+
+#define MAX_DISTANCE (-1.0)
+
+DistanceSensor::DistanceSensor(PinName a): m_analogIn(a)
+{
+    m_distance = MAX_DISTANCE;
+}
+float DistanceSensor::getDistance_cm()
+{
+    
+    return m_analogIn*3.3;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensor/DistanceSensor.h	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,21 @@
+#ifndef MBED_DISTANCESENSOR_H
+#define MBED_DISTANCESENSOR_H
+
+#include "mbed.h"
+
+
+
+
+class DistanceSensor
+{
+public:
+    DistanceSensor(PinName a);
+    float getDistance_cm();
+
+protected:
+    float m_distance;
+    AnalogIn  m_analogIn;
+    
+};
+
+#endif //MBED_DISTANCESENSOR_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include "DistanceSensor.h"
+#include "Plotting.h"
+#include "Servo.h"
+
+DistanceSensor pir (p20);
+Servo servo (p10);
+Serial pc (USBTX, USBRX); // tx, rx
+Plotting plot;
+
+//Interrupt is generated every 1ms and degree is increased by 1
+unsigned int uiFlag_50ms = 0;
+    
+void counter_1ms ()
+{
+    uiFlag_50ms++;
+}
+
+// Align servo 
+int main()
+{
+    float degree = 0.0;
+    pc.printf("Waiting Request\n");
+    servo.update (0.0);
+    
+    pc.printf("Degree: %f\n", servo.getDegree());
+    while(1) {
+        char c = pc.getc();
+        
+        if (c == 'u') degree += 0.5;
+        else if (c == 'd') degree -= 0.5;
+            
+        servo.update (degree);
+        degree = servo.getDegree();
+        pc.printf("Degree: %f\n", servo.getDegree());
+    }
+}
+
+//// Plot distance sensor
+//int main()
+//{
+//    wait(1);
+//
+//    //Set the 1ms thicker.
+//    Ticker ticker_1ms;
+//    ticker_1ms.attach(&counter_1ms,0.001);
+//
+//    Timer time;
+//    time.start();
+//
+//    while(1) {
+//        // Every 50 ms,
+//        if(uiFlag_50ms>=50) {
+//            uiFlag_50ms=0;
+//            
+//            // clear plotting buffer
+//            plot.reset();
+//            
+//            // put data to buffer
+//            plot.put(pir.getDistance_cm(),0);
+//            
+//            // send buffer
+//            plot.send(&pc);
+////            pc.printf ("Distance %f\r\n", pir.getDistance_cm());
+//        }
+//    }
+//}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 11 00:06:25 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file