personal changes

Dependents:   Smart_Car_Seat_v1

Fork of FSR by Chenkai Shao

Files at this revision

API Documentation at this revision

Comitter:
cshao06
Date:
Wed Oct 21 21:14:06 2015 +0000
Child:
1:6b702226a5d3
Commit message:
FSR

Changed in this revision

FSR.cpp Show annotated file Show diff for this revision Revisions of this file
FSR.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FSR.cpp	Wed Oct 21 21:14:06 2015 +0000
@@ -0,0 +1,34 @@
+#include "FSR.h"
+#include "mbed.h"
+
+FSR::FSR(PinName pin, float resistance) : _ain(pin), _r(resistance)
+{
+}
+
+float FSR::readRaw()
+{
+    float read = _ain;
+    return read;
+}
+
+float FSR::readFSRResistance()
+{
+    float read = _ain;
+    return _r * 3.3 / read - _r;
+}
+
+float FSR::readWeight()
+{
+    float read = _ain;
+    float rfsr = _r * 3.3 / read - _r;
+    float slope = (4 - 2) / (log10(6.2) - log10(0.25));
+    float a = log10(rfsr);
+    if (a < log10(6.2))
+    {
+        return pow(10, ((log10(6.2) - a) * slope + 2));
+    }
+    else
+    {
+        return 0;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FSR.h	Wed Oct 21 21:14:06 2015 +0000
@@ -0,0 +1,57 @@
+#ifndef FSR_H
+#define FSR_H
+
+#include "mbed.h"
+
+/** Force sensitive resistor class using an AnalogIn pin
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "FSR.h"
+ * FSR fsr(p17, 10); // a 10k resistor is used
+ * int main(){
+ *     while (1)
+ *     {
+ *         printf("The raw data is %f\n", fsr.readRaw());
+ *         printf("The resistance of the FSR is %f\n", fsr.readFSRResistance());
+ *         printf("The weight on the FSR is %f\n\n", fsr.readWeight());
+ *         wait(0.3); //just here to slow down the output for easier reading
+ *     }
+ * }
+ * @endcode
+ */
+
+class FSR
+{
+public:
+    /** Create an FSR object
+     *
+     * @param AnalogIn pin number
+     * @param resistance of the voltage divider resistor in k
+     */
+    FSR(PinName Pin, float resistance);
+    
+    /** Read the raw data
+     *
+     * @return the raw float data ranging from 0 to 1
+     */
+    float readRaw();
+    
+    /** Read the resistance of the FSR
+     *
+     * @return the resistance of the FSR
+     */
+    float readFSRResistance();   
+    
+    /** Read the weight in N. 0 anyway if the weight is less than 100g
+     *
+     * @return the weight ranging from 100g to 10000g
+     */
+     float readWeight();
+protected:
+    AnalogIn _ain;
+    float _r;
+};
+
+#endif
\ No newline at end of file