Advertisement
RevDrMosesPLester

rx50_tpi_v1

Jan 17th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.64 KB | None | 0 0
  1. volatile unsigned long previous_tdc; // time of the time before the last time that the crank sensor window closed (indicating TDC happened) in microseconds
  2. volatile unsigned long last_tdc; // last time the crank sensor window closed in microseconds
  3. volatile int engine_running; // is engine already running (2) or is it in the process of being started for the first time since bootup (1) or has there been no crank sensor activity at all yet since bootup (0)
  4. int crank_sensor_pin; // arduino interrupt pin for +5v from crank sensor
  5. int coil_pin; // output pin to HT coil
  6. int dwell; // ignition coil charge time (time required for the coil to receive 12v before it can be discharged with a good spark) in microseconds
  7. byte advance_map[49]; // map of spark advance indexed by rpm at 250rpm resolution to the map, advance numbers in degrees BTDC
  8. boolean coil_charging; // whether the coil has power to it right now or not
  9. int redline; // max rpm, above this spark must be restricted to 0 advance
  10. int minimum_rpm; // min rpm, below this we consider the engine to have stalled and we will reset to "engine not running" mode
  11.  
  12. void setup()
  13. {
  14.   last_tdc = 0;
  15.   previous_tdc = 0;
  16.   engine_running = 0;
  17.   crank_sensor_pin = 0;
  18.   dwell = 2000;
  19.   coil_charging = false;
  20.   coil_pin = 13;
  21.   redline = 12000;
  22.   minimum_rpm = 900;
  23.   advance_map[0] = 8; // 0 rpm
  24.   advance_map[1] = 8; // 250 rpm
  25.   advance_map[2] = 8; // 500
  26.   advance_map[3] = 8; // 750
  27.   advance_map[4] = 8; // 1000
  28.   advance_map[5] = 8;
  29.   advance_map[6] = 8;
  30.   advance_map[7] = 8;
  31.   advance_map[8] = 8; // 2000
  32.   advance_map[9] = 8;
  33.   advance_map[10] = 8;
  34.   advance_map[11] = 8;
  35.   advance_map[12] = 10; // 3000
  36.   advance_map[13] = 12;
  37.   advance_map[14] = 14;
  38.   advance_map[15] = 16;
  39.   advance_map[16] = 18; // 4000
  40.   advance_map[17] = 20;
  41.   advance_map[18] = 22;
  42.   advance_map[19] = 22;
  43.   advance_map[20] = 22; // 5000
  44.   advance_map[21] = 22;
  45.   advance_map[22] = 22;
  46.   advance_map[23] = 22;
  47.   advance_map[24] = 22; // 6000
  48.   advance_map[25] = 22;
  49.   advance_map[26] = 22;
  50.   advance_map[27] = 22;
  51.   advance_map[28] = 22; // 7000
  52.   advance_map[29] = 22;
  53.   advance_map[30] = 22;
  54.   advance_map[31] = 22;
  55.   advance_map[32] = 22; // 8000
  56.   advance_map[33] = 22;
  57.   advance_map[34] = 22;
  58.   advance_map[35] = 22;
  59.   advance_map[36] = 22; // 9000
  60.   advance_map[37] = 22;
  61.   advance_map[38] = 22;
  62.   advance_map[39] = 22;
  63.   advance_map[40] = 22; // 10000
  64.   advance_map[41] = 22;
  65.   advance_map[42] = 22;
  66.   advance_map[43] = 22;
  67.   advance_map[44] = 22; // 11000
  68.   advance_map[45] = 22;
  69.   advance_map[46] = 22;
  70.   advance_map[47] = 22;
  71.   advance_map[48] = 22; // 12000
  72.  
  73.   pinMode(coil_pin, OUTPUT);
  74.   attachInterrupt(crank_sensor_pin, sensor_ring_window_closed, RISING);
  75. }
  76.  
  77. void loop()
  78. {
  79.   if (engine_running == 1) // first crank revolution is just now happening
  80.   {
  81.     if (micros() - last_tdc >= dwell)
  82.     {
  83.       discharge_coil();
  84.       engine_running = 2;
  85.     }
  86.   } else if (engine_running = 2) // engine spinning normally, perform normal spark advance and discharge procedures
  87.   {
  88.     if (coil_charging)
  89.     {
  90.       if (get_rpm() > redline)
  91.       {
  92.         if (get_microseconds_since_last_tdc() >= ((previous_tdc - last_tdc) - (get_microseconds_per_degree_at_rpm(get_rpm()) * 0))) // if we're at roughly TDC for 0 degrees advance, discharge coil at TDC since we're hitting the redline
  93.         {
  94.           discharge_coil();
  95.         }
  96.       }
  97.       else if (get_microseconds_since_last_tdc() >= ((previous_tdc - last_tdc) - (get_microseconds_per_degree_at_rpm(get_rpm()) * advance_map[get_250_rounded_rpm_index()]))) // calc the time before expected next TDC and compare to advance_map to see if we want to fire now
  98.       {
  99.         discharge_coil();
  100.       }
  101.     } else {
  102.       if (get_rpm() > redline)
  103.       {
  104.         if (get_microseconds_since_last_tdc() >= ((previous_tdc - last_tdc) - (get_microseconds_per_degree_at_rpm(get_rpm()) * 0))) // if we're at TDC at redline running speed, charge the coil and then discharge immediately
  105.         {
  106.           charge_coil();
  107.           delayMicroseconds(dwell);
  108.           discharge_coil();
  109.         }
  110.       }
  111.       else if (get_microseconds_since_last_tdc() >= ((previous_tdc - last_tdc) - (get_microseconds_per_degree_at_rpm(get_rpm()) * advance_map[get_250_rounded_rpm_index()]) - dwell)) // if we've reached dwell time before spark advance before TDC, power up the coil
  112.       {
  113.         charge_coil();
  114.       }
  115.     }
  116.  
  117.     if (get_rpm() < minimum_rpm) // engine stalled
  118.     {
  119.       engine_running = 0;
  120.       discharge_coil();
  121.     }
  122.   }
  123. }
  124.  
  125. void sensor_ring_window_closed() // crank sensor has detected the end of the sensor ring window in rotation
  126. {
  127.   if (engine_running == 0)
  128.   {
  129.     charge_coil();
  130.     engine_running = 1;
  131.   }
  132.  
  133.   previous_tdc = last_tdc;
  134.   last_tdc = micros();
  135. }
  136.  
  137. void charge_coil() // provide ground to ignition coil, allowing it to charge
  138. {
  139.   digitalWrite(coil_pin, HIGH);
  140.   coil_charging = true;
  141. }
  142.  
  143. void discharge_coil() // cut ground to coil, making spark
  144. {
  145.   digitalWrite(coil_pin, LOW);
  146.   coil_charging = false;
  147. }
  148.  
  149. unsigned int get_250_rounded_rpm_index() // returns the current rpm to the quarter of a thousand rpm in array index form
  150. {
  151.   unsigned long rev_time = last_tdc - previous_tdc;
  152.   return (round(int((60000000 + (rev_time / 2)) / rev_time) / 250));
  153. }
  154.  
  155. unsigned int get_rpm() // returns the current rpm
  156. {
  157.   unsigned long rev_time = last_tdc - previous_tdc;
  158.   return (int((60000000 + (rev_time / 2)) / rev_time));
  159. }
  160.  
  161. int get_microseconds_per_degree_at_rpm(int rpm)
  162. {
  163.   return (((1 / (rpm / 60)) / 360) * 1000000);
  164. }
  165.  
  166. unsigned long get_microseconds_since_last_tdc()
  167. {
  168.   return (micros() - last_tdc);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement