Johannes Otto
/
simple_stm_ignition
programmable ignition for an mz ts 150. The interruptor needs to be replaced by an hall effect sensor
IgnitionModule.h@3:5efd78be4662, 2015-10-25 (annotated)
- Committer:
- johannes_otto
- Date:
- Sun Oct 25 16:58:59 2015 +0000
- Revision:
- 3:5efd78be4662
- Parent:
- 1:f99510926784
constructor requires stroke now, it accepts mm values instead of degree (its easier and more accurate to measure). The angles are computed automatically
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johannes_otto | 3:5efd78be4662 | 1 | #include "mbed.h" |
johannes_otto | 3:5efd78be4662 | 2 | |
johannes_otto | 3:5efd78be4662 | 3 | static const float PI = 3.141592653589793238; |
johannes_otto | 3:5efd78be4662 | 4 | |
johannes_otto | 3:5efd78be4662 | 5 | class IgnMod |
johannes_otto | 3:5efd78be4662 | 6 | { |
johannes_otto | 3:5efd78be4662 | 7 | private: |
johannes_otto | 3:5efd78be4662 | 8 | Timeout ignition_timer; |
johannes_otto | 3:5efd78be4662 | 9 | Timer t; |
johannes_otto | 3:5efd78be4662 | 10 | |
johannes_otto | 3:5efd78be4662 | 11 | DigitalOut _pin; |
johannes_otto | 3:5efd78be4662 | 12 | |
johannes_otto | 3:5efd78be4662 | 13 | int ignition_length; |
johannes_otto | 3:5efd78be4662 | 14 | int round_time; |
johannes_otto | 3:5efd78be4662 | 15 | int ignition_delay; |
johannes_otto | 3:5efd78be4662 | 16 | |
johannes_otto | 3:5efd78be4662 | 17 | float offset; |
johannes_otto | 3:5efd78be4662 | 18 | float def_ignition; |
johannes_otto | 3:5efd78be4662 | 19 | |
johannes_otto | 3:5efd78be4662 | 20 | void on() { |
johannes_otto | 3:5efd78be4662 | 21 | _pin.write(1); |
johannes_otto | 3:5efd78be4662 | 22 | ignition_timer.attach_us(this,&IgnMod::off,ignition_length); |
johannes_otto | 3:5efd78be4662 | 23 | } |
johannes_otto | 3:5efd78be4662 | 24 | void off() { |
johannes_otto | 3:5efd78be4662 | 25 | _pin.write(0); |
johannes_otto | 3:5efd78be4662 | 26 | } |
johannes_otto | 3:5efd78be4662 | 27 | |
johannes_otto | 3:5efd78be4662 | 28 | int get_delay(int round_time) { |
johannes_otto | 3:5efd78be4662 | 29 | int delay=0; |
johannes_otto | 3:5efd78be4662 | 30 | int rpm=60e6/round_time; |
johannes_otto | 3:5efd78be4662 | 31 | |
johannes_otto | 3:5efd78be4662 | 32 | if (rpm>7000) delay=-4; |
johannes_otto | 3:5efd78be4662 | 33 | else if (rpm>6000) delay=-4; |
johannes_otto | 3:5efd78be4662 | 34 | else if (rpm>5000) delay=-4; |
johannes_otto | 3:5efd78be4662 | 35 | else if (rpm>4000) delay=-4; |
johannes_otto | 3:5efd78be4662 | 36 | else if (rpm>3000) delay=-2; |
johannes_otto | 3:5efd78be4662 | 37 | else if (rpm>2000) delay=-2; |
johannes_otto | 3:5efd78be4662 | 38 | else if (rpm>1000) delay=-2; |
johannes_otto | 3:5efd78be4662 | 39 | else delay=-2; |
johannes_otto | 3:5efd78be4662 | 40 | |
johannes_otto | 3:5efd78be4662 | 41 | return round_time*(offset-def_ignition+delay)/360; |
johannes_otto | 3:5efd78be4662 | 42 | } |
johannes_otto | 3:5efd78be4662 | 43 | public: |
johannes_otto | 3:5efd78be4662 | 44 | IgnMod(PinName pin,int il,float of_mm, float di_mm, float h) : _pin(pin) { |
johannes_otto | 3:5efd78be4662 | 45 | float r=h/2; |
johannes_otto | 3:5efd78be4662 | 46 | ignition_length=il; |
johannes_otto | 3:5efd78be4662 | 47 | offset=acos((r-of_mm)/r)*180/PI; |
johannes_otto | 3:5efd78be4662 | 48 | def_ignition=acos((r-di_mm)/r)*180/PI; |
johannes_otto | 3:5efd78be4662 | 49 | t.start(); |
johannes_otto | 3:5efd78be4662 | 50 | } |
johannes_otto | 3:5efd78be4662 | 51 | void ignite() { |
johannes_otto | 3:5efd78be4662 | 52 | round_time=t.read_us(); |
johannes_otto | 3:5efd78be4662 | 53 | t.reset(); |
johannes_otto | 3:5efd78be4662 | 54 | ignition_delay=get_delay(round_time); |
johannes_otto | 3:5efd78be4662 | 55 | ignition_timer.attach_us(this,&IgnMod::on,ignition_delay-t.read_us()); |
johannes_otto | 3:5efd78be4662 | 56 | } |
johannes_otto | 3:5efd78be4662 | 57 | }; |