Craig Evans / m3pi

Dependents:   3pi_Example_2 3pi_Lab1_Task2_Example1 3pi_Lab2_Task1_Example1 3pi_Line_Follow ... more

Files at this revision

API Documentation at this revision

Comitter:
eencae
Date:
Mon Mar 19 13:06:40 2018 +0000
Parent:
8:92167bd3eb44
Commit message:
Added some simple functions to the library that do not require arrays to be passed by reference. Sensor values are stored in the class instead.

Changed in this revision

m3pi.cpp Show annotated file Show diff for this revision Revisions of this file
m3pi.h Show annotated file Show diff for this revision Revisions of this file
--- a/m3pi.cpp	Wed Jul 05 14:39:33 2017 +0000
+++ b/m3pi.cpp	Mon Mar 19 13:06:40 2018 +0000
@@ -9,10 +9,14 @@
     _reset = new DigitalOut(p8);
     _last_line_position = 0.0;
     
+    // initialise the arrays
     _bar_graph[0] = ' ';
     for (int i = 0; i < 6; i++) {
         _bar_graph[i+1] = i;    
     }
+    for (int i = 0; i < 5; i++) {
+       _values[i]=0;  
+    }
 }
 
 m3pi::~m3pi()
@@ -34,6 +38,10 @@
 
 /////////////////////////////// serial slave commands ////////////////////////////////
 
+void m3pi::scan() 
+{
+    get_calibrated_values(_values);
+}
 
 void m3pi::get_signature(char *signature)
 {
@@ -112,7 +120,6 @@
     }
 }
 
-
 void m3pi::calibrate()
 {
     _serial->putc(0xB4);
@@ -302,6 +309,17 @@
 
 }
 
+void m3pi::display_data() 
+{
+    display_sensor_values(_values,1);   
+    
+    char buffer[8]={0};
+    sprintf(buffer,"% .3f",_last_line_position);
+    lcd_goto_xy(0,0);
+    lcd_print(buffer,6); 
+       
+}
+
 unsigned int m3pi::get_sensor_array_value(unsigned int values[])
 {
     unsigned int value = 0;
@@ -350,6 +368,11 @@
     return _last_line_position;
 }
 
+float m3pi::read_line() {
+    return calc_line_position(_values); 
+}
+
+
 /////////////////////////////// private methods ////////////////////////////////
 
 void m3pi::reset()
--- a/m3pi.h	Wed Jul 05 14:39:33 2017 +0000
+++ b/m3pi.h	Mon Mar 19 13:06:40 2018 +0000
@@ -28,6 +28,11 @@
     * @details code and start 3pi in serial slave mode
     */
     void init();
+    
+    /** Causes the robot to read the IR sensors
+    * @details is an alterative to get_*_values methods if you don't need the values themselves
+    */
+    void scan();
 
     /** Get signature of slave firmware
     * @param signature - array of size 7 to store signature
@@ -75,6 +80,14 @@
     * @details 0.0 for line under sensor 2, 0.5 for line under sensor 3 etc.
     */
     float get_line_position();
+    
+    /** Returns an estimate of the normalised line position
+    * @returns float in the range -1.0 to 1.0
+    * @details uses the values of sensor stored in the class - must call scan() first!
+    * @details -1.0 for line under sensor 0, -0.5 for line under sensor 1
+    * @details 0.0 for line under sensor 2, 0.5 for line under sensor 3 etc.
+    */
+    float read_line(); 
 
     /** Clear LCD
     */
@@ -148,6 +161,11 @@
     */
     void display_signature(int x,int y);
     
+    /** Display line position and bar graph of sensor readings on LCD
+    * @details must call scan() and read_line() first
+    */
+    void display_data();
+    
     /** Display sensor values on LCD
     * @param values - array of calibrated sensor values
     * @param y - line on LCD to display
@@ -175,6 +193,7 @@
     float _last_line_position;
     
     char _bar_graph[7];
+    unsigned int _values[5];
 
     void reset();