MurphyBat_LPC

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
SamuelEarnshawARM
Date:
Sun Aug 06 09:53:07 2017 +0000
Commit message:
06/08/2017 Murphybat_LPC

Changed in this revision

MurphyBat.cpp Show annotated file Show diff for this revision Revisions of this file
MurphyBat.hpp 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 a20f005ea7ae MurphyBat.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MurphyBat.cpp	Sun Aug 06 09:53:07 2017 +0000
@@ -0,0 +1,76 @@
+/*
+ * MurphyBat.cpp
+ *
+ *  Created on: 5 Aug 2017
+ *      Author: ciscer01
+ */
+
+#include <MurphyBat.hpp>
+#include <mbed.h>
+
+MurphyBat::MurphyBat(PinName Ir1, PinName Ir2, PinName Ir3, PinName Ir4, PinName Actuator):
+		mIr1(Ir1),
+		mIr2(Ir2),
+		mIr3(Ir3),
+		mIr4(Ir4),
+		BatI(0),
+		batActuator(Actuator),
+		batActuratorTimout(),
+		Fired(false) {
+
+	BatIrIn[0] = &mIr1;
+	BatIrIn[1] = &mIr2;
+	BatIrIn[2] = &mIr3;
+	BatIrIn[3] = &mIr4;
+	//BatIrTh[0] = 0.5; 0.5, 0.5, 0.5),
+	for (unsigned int i = 0; i < 4; i++){
+		for (unsigned int j = 0; j < avg_size; j++) {
+			BatIrVal[i][j] = 0;
+		}
+
+	}
+	BatIrTh[0] = 0.4f - 0.05f;
+	BatIrTh[1] = 0.3f - 0.05f;
+	BatIrTh[2] = 0.4f - 0.05f;
+	BatIrTh[3] = 0.3f - 0.05f;
+	batActuator.write(0);
+}
+
+void MurphyBat::update() {
+	bool fire = false;
+	float BatIrAvg[4];
+	for (unsigned int i = 0; (i < 4) && (fire == false); i++) {
+		//Read the Ir value
+		BatIrVal[i][BatI] = BatIrIn[i]->read();
+		//start the avarage computation
+		BatIrAvg[i] = BatIrVal[i][0];
+		for (unsigned int j = 1; j < avg_size; j++) {
+			BatIrAvg[i] += BatIrVal[i][j];
+		}
+		//Compute avarage
+		BatIrAvg[i] = BatIrAvg[i] / avg_size;
+		if (BatIrVal[i][BatI] > BatIrTh[i]) {
+			fire = true;
+		}
+	}
+	BatI = (BatI + 1) % avg_size;
+	if (fire) {
+		Fire();
+	}
+}
+
+void MurphyBat::FireCb(MurphyBat *mb){
+	mb->Fire();
+}
+
+void MurphyBat::Fire(void) {
+	if (Fired == false) {
+		Fired = true;
+		batActuator.write(1);
+		batActuratorTimout.attach_us(this,MurphyBat::FireCb, 200000);
+	} else {
+		Fired = false;
+		batActuator.write(0);
+		batActuratorTimout.detach();
+	}
+}
diff -r 000000000000 -r a20f005ea7ae MurphyBat.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MurphyBat.hpp	Sun Aug 06 09:53:07 2017 +0000
@@ -0,0 +1,37 @@
+/*
+ * MurphyBat.h
+ *
+ *  Created on: 5 Aug 2017
+ *      Author: ciscer01
+ */
+
+#ifndef MURPHYARM_MURPHYBAT_HPP_
+#define MURPHYARM_MURPHYBAT_HPP_
+
+#include <mbed.h>
+
+#define avg_size 1
+
+class MurphyBat {
+public:
+	MurphyBat(PinName Ir1, PinName Ir2, PinName Ir3, PinName Ir4, PinName Actuator);
+
+	void update();
+	static void FireCb(MurphyBat *mb);
+	void Fire(void);
+private:
+
+	AnalogIn mIr1;
+	AnalogIn mIr2;
+	AnalogIn mIr3;
+	AnalogIn mIr4;
+	AnalogIn *BatIrIn[4];
+	float BatIrVal[4][avg_size];
+	float BatIrTh[4];
+	int BatI;
+	DigitalOut batActuator;
+	Timeout batActuratorTimout;
+	bool Fired;
+};
+
+#endif /* MURPHYARM_MURPHYBAT_HPP_ */
diff -r 000000000000 -r a20f005ea7ae main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Aug 06 09:53:07 2017 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+#include "MurphyBat.hpp"
+
+MurphyBat Bat(p15, p16, p17, p18, p21);
+
+
+DigitalOut Led1(LED1);
+int main()
+{
+    Led1 = 1;
+    while(true)
+    {
+        Bat.update();
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r a20f005ea7ae mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Aug 06 09:53:07 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/fd96258d940d
\ No newline at end of file