MBED Application board used to control frequency and duty cycle of injector valves

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
iwolf32
Date:
Tue Jul 25 14:29:20 2017 +0000
Commit message:
REV1

Changed in this revision

coil-driver.cpp Show annotated file Show diff for this revision Revisions of this file
coil-driver.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-rtos.lib 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/coil-driver.cpp	Tue Jul 25 14:29:20 2017 +0000
@@ -0,0 +1,49 @@
+// Begin sample code
+/*
+#include "mbed.h"
+#include "rtos.h"
+#include "coil-driver.h"
+// Coil parameters [control pin, spike time (us), hold time period (us), hold time pulse width (us)]
+Coil orange(A0, 500, 40, 3); // Valve driven by pin A0, 500us spike time, 25kHz PWM at 7.5% duty cycle
+
+Coil yellow(A1, 5000, 40, 6); // Valve driven by pin A1, 5ms spike time, 25kHz PWM at 15% duty cycle
+
+int main()
+{
+    while (true) {
+        orange.on();
+        yellow.on();
+        Thread::wait(500);
+        orange.off();
+        yellow.off();
+        Thread::wait(500);
+    }
+}
+*/
+// End sample code
+
+#include "coil-driver.h"
+
+// Default constructor
+Coil::Coil(PinName _controlPin, uint16_t _spikeus, uint16_t _holdperiod_us, uint16_t _holdpulse_us)
+    :   controlOut(_controlPin),
+        spikeus(_spikeus),
+        holdperiod_us(_holdperiod_us),
+        holdpulse_us(_holdpulse_us)
+{
+    controlOut.write(0.0); // Ensure coil output is off by default
+    controlOut.period_us(holdperiod_us); // PWM control period (uint16_t microseconds)
+
+};
+
+void Coil::on()
+{
+    controlOut.write(1.0);
+    wait_us(spikeus);
+    controlOut.pulsewidth_us(holdpulse_us);
+}
+
+void Coil::off()
+{
+    controlOut.write(0.0);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/coil-driver.h	Tue Jul 25 14:29:20 2017 +0000
@@ -0,0 +1,22 @@
+#ifndef COIL_DRIVER_H
+#define COIL_DRIVER_H
+
+#include "mbed.h"
+#include "rtos.h"
+
+// Coil driver class. Applies full power for given "spike" time
+// and then applies a PWM signal to keep the coil energized at a lower power.
+class Coil
+{
+public:
+    Coil(PinName _controlPin, uint16_t _spikeus, uint16_t _holdperiod_us, uint16_t _holdpulse_us);
+
+    //InterruptIn controlIn; // Input for external coil "ON" signal
+    PwmOut controlOut; // PWM output pin driving coil
+    uint16_t spikeus, holdperiod_us, holdpulse_us, pwmFrequency;
+
+    void on();
+    void off();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jul 25 14:29:20 2017 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "coil-driver.h"
+
+Coil yellow(A0, 300, 40, 4); // Injector
+InterruptIn dutycycleup(A2);
+InterruptIn dutycycledown(D4);
+InterruptIn frequencyup(A5);
+InterruptIn frequencydown(A4);
+
+Serial pc(USBTX, USBRX);
+
+double frequency=5;
+double dutycycle=0.75;
+float openvalue=(((1/frequency)*dutycycle));
+float offvalue=(1/frequency)-openvalue;
+
+void dutycycleincrease()
+{
+    dutycycle=dutycycle+0.01;
+}
+
+void dutycycledecrease()
+{
+    dutycycle=dutycycle-0.01;
+}
+
+void frequencyincrease()
+{
+    frequency=frequency+1;
+}
+void frequencydecrease()
+{
+    frequency=frequency-1;
+}
+
+int main() {
+ 
+dutycycleup.rise(&dutycycleincrease);
+dutycycledown.rise(&dutycycledecrease);
+frequencyup.rise(&frequencyincrease);
+frequencydown.rise(&frequencydecrease);     
+  
+while(1){
+    
+float openvalue=(((1/frequency)*dutycycle));
+float offvalue=(1/frequency)-openvalue;
+    
+yellow.on();
+wait(openvalue);
+yellow.off();
+wait(offvalue);
+
+pc.printf("Frequency: %f\r\n", frequency);
+pc.printf("\n");
+pc.printf("Duty Cycle: %f\r\n", dutycycle);
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Jul 25 14:29:20 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jul 25 14:29:20 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/a97add6d7e64
\ No newline at end of file