Main Menu

LBB v2.0 Common Code

Started by hakcenter, August 26, 2016, 09:52:14 AM

hakcenter

Latest Version 3.0.0
You will need to be a registered forum user to see the attachment, but there is a Arduino Sketch attached to this post.

Detailed list of code will follow...

Timer's must be used if you don't want to micromanage the gearbox. And using timers will allow RPM to fluctuate enough that you don't continue to update vane position when it absolutely is not necessary. Secondly waiting to adjust position makes the turbo more aggressive in acceleration, which is good.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

hakcenter

#1
Main Loop

void loop() {
  // Freq Measure
  double sum;
  byte count = 0;
  byte i = 0;
 
  while(!turbo_online) {
    t2.update();
    t3.update();
    if (turbo_flags[0] == 1) {
      turbo_online = true;
    }
  }
  if (!bt_enabled) { Serial.println(F("Turbo ONLINE!")); }
 
  // main loop
  while(turbo_online) {
    t1.update();
    t2.update();
    t3.update();
    // Freq Measure
    if (FreqMeasure.available() && !emulate) {
      emulate = false;
      // average several reading together
      sum += FreqMeasure.read();
      count++;
      if (count > 7) {
        // turbo_rpm = Freq(sum / count) * 60
        turbo_rpm = (FreqMeasure.countToFrequency(sum / 8.0f)) * 60;
        sum = 0;
        count = 0;
      }
    }
    if (emulate) {
      if (turbo_rpm == 0) { turbo_rpm = minimum_turbo_rpm; }
      turbo_rpm++;
      if (turbo_rpm > 140000) { turbo_rpm = minimum_turbo_rpm; }
    }
  }
  if (!bt_enabled) { Serial.println(F("Turbo OFFLINE!")); }
}


  • Using float division for more accurate rpm conversion.
  • Average the last 8 Frequencies together.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

hakcenter

#2
Timers

Timer 1
--Sets turbo vane position
t1.every(5, set_turbo_position);
Runs every 5ms, to keep gearbox from automatically resetting to max open (no input default)

void set_turbo_position() {
  // if not sweeping smoothe vane changes
  if (!sweep_position) {
    // Keep vane position within constraints
    constrain(vane_position, min_position, max_position);
    // Vane smoothing between large values
         if (!cruise_mode && !brake_mode && !boost_mode && turbo_rpm < top_end_rpm) {
    // if vane_position change is 20 or more, move by half the difference
         if (vane_position - 20 >= last_vane_position) { final_vane_position = vane_position - ((vane_position - last_vane_position) >> 1); }
    else if (vane_position + 20 <= last_vane_position) { final_vane_position = vane_position + ((last_vane_position - vane_position) >> 1); }
    // if vane_position change is 10 or more, move by 2
    else if (vane_position - 10 >= last_vane_position) { final_vane_position = last_vane_position + 2; }
    else if (vane_position + 10 <= last_vane_position) { final_vane_position = last_vane_position - 2; }
    else { final_vane_position = vane_position; }
    // End Smoothing
    } else if (turbo_rpm > top_end_rpm) {
      // remove jitter during top end turbo speeds
      if (vane_position - 5 >= last_vane_position || vane_position + 5 <= last_vane_position) { final_vane_position = vane_position; } else { final_vane_position = last_vane_position; }
    } else { final_vane_position = vane_position; }
    //---- Decel Mode ----//
    if (deceleration_mode) {
      vane_position = deceleration_position;
      final_vane_position = deceleration_position;
    }
    // Constrain and update last_vane_position
    constrain(final_vane_position, min_position, max_position);
    //last_vane_position = final_vane_position;
  }
  // Ignore vane changes until startup is complete
  if (startup > 1 && !sweep_position) {
    startup--;
    vane_position = min_position;
    last_vane_position = min_position;
    final_vane_position = min_position;
  }
  byte lo_byte = lowByte(final_vane_position);
  byte hi_byte = highByte(final_vane_position);
  byte do_calibrate = 0x01;
  if (calibrate) {
    do_calibrate = 0x02;
  } else {
    do_calibrate = 0x01;
  }
  byte data[] = { lo_byte, hi_byte, do_calibrate, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // data message with an added counter
  // data[2] = 0x02 for recalibrating gearbox
  // Load message and send
  send_turbo_position(data);
}



  • Vane smoothing under top_end_rpm.
  • Section executes at 5ms, move vane by intervals of 2. Making full transition from full closed to full open, roughly ~1s
  • Remove jitter at top, forcing adjustments by 5.
  • Check for deceleration mode and force decel position


Timer 2
--Keeps everything in time
t2.every(1, keep_time);
Runs every 1ms and is used to keep things in time.

void keep_time() {
  timer++;
  // ----
  // Startup Timer
  if (turbo_rpm < minimum_turbo_rpm) {
    if (startup > 249) { startup = 250; }
    startup++;
  } 
  if (timer % 10 == 0) {
    // ----
    // Read Serial Input
    check_serial();
    // ----
    // Update vane_position every 10ms 100k+ / 50ms 0 - 100k
         if (turbo_rpm > top_end_rpm) { update_vane_position = true; }
    else if (timer % 50 == 0) { update_vane_position = true; } 
  }
  if (timer % 25 == 0) {
    // ----
    // Sweep vane_position
    if (sweep_position) { sweep(); }
  }
  if (timer % 128 == 0) {
    // ----
    // Calculate Modes
    if (turbo_rpm < top_end_rpm) { calculate_modes(); }
    // ----
    // Calculate turbo_accel
    turbo_accel = (int) ((turbo_rpm - last_turbo_rpm) >> 7);
    if (turbo_accel > 10) { boost_position = half_cm; }
    if (turbo_accel > 20) { boost_position = one_cm; }
    if (turbo_accel > 30) { boost_position = two_cm; }
    last_turbo_rpm = turbo_rpm;
  }
  if (timer % 200 == 0) {
    /*
    // ----
    // Spare Pin Interrupt
    spare_value = (spare_value + (spare_count * 150)) >> 1;
    spare_count = 0;
    */
  }
  // ----
  // Calculate vane_position
  if (update_vane_position && !sweep_position) {
    update_vane_position = false;
    calculate_vane_position();
    if (serial_out) { serial_output(); }
  }
  if (timer == 1000) { timer = 0; }
}


  • Update vane_position each 50ms under 100krpm
  • Update vane_position each 10ms over 100k rpm
  • Update last_turbo_rpm each 128ms
  • Update turbo_accel each 128ms
  • Read CANBUS each 25ms
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

hakcenter

#3
Variables Wiki > here.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

hakcenter

Initial release v2.0.0 has been added.

For the android app, you can opt into the semi-beta but 100% stable app @ https://play.google.com/apps/testing/com.devnull.lbb on your Android smartphone
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

hakcenter

v2.0.2 update

Major Changes:

    • CAN BUS fixes applied
      • CAN chips RX/TX buffers and Filters zero'd on startup
      • Startup CAN timeout working properly
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    #6
    v2.1.2 update

    Major Changes:

    • Boost Timer function added
      • You will see Normal++ when output is toggled and while boost is engaged
      • Basically it's a slight vane close one initial acceleration
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    #7
    v2.2.0 update

    Major Changes:

    • Emulate function added

      • This will bypass startup procedures and get let the controller think the Turbo is online.
      • It also bypasses some CANBUS communication, so it is essential to turn off once you're done debugging.
    • Calibrate function added
      • Enabling calibrate will switch the output, to send the calibrate command until de-selected.
    • Bulk Data serial communication
      • This is for communicating faster.

    Minor Changes:

    • Fixed some communication
      • There were times that communication got blocked up or combined either via CANBUS or Serial.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v2.2.1 update

    Minor Changes:

    • Adjusted serial commands curve_rpmX= & turbo_curveX=
      • Just more specifics on making sure the command is read properly.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v3.0.0 update

    Major Changes:

    • Bluetooth serial communication is extremely specific now.

      • All responses are byte length specific.
      • Ask commands are written with length of data to be sent first, then the LBB receive will know how many bytes to expect before timing out
      • So do not try to communicate through USB while connected via Bluetooth. It'll break both.


    byte[] bytes = "bt_data".getBytes();
    bt_out.write(bytes.length);
    bt_out.write(bytes);
    bt_out.flush();


    Translates as, send 7, send "bt_data". LBB knows to expect 7 bytes, waits up to 0.5s before timing out waiting for said bytes to come in.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v3.0.2 update

    Major Changes:

    • Loop section averaging.

      • Turbo rpm should be a lot more sensitive.
      • Dividing with a float 8.0f might be swapped for a faster bitshift calculation, just really depends.


    // main loop
      while(turbo_online) {
        t1.update();
        t2.update();
        t3.update();
        // Freq Measure
        if (FreqMeasure.available() && !emulate) {
          emulate = false;
          // average several reading together
          sum += FreqMeasure.read();
          count++;
          if (count > 7) {
            // turbo_rpm = Freq(sum / count) * 60
            turbo_rpm = (FreqMeasure.countToFrequency(sum / 8.0f)) * 60;
            sum = 0;
            count = 0;
          }
        }
        if (emulate) {
          if (turbo_rpm == 0) { turbo_rpm = minimum_turbo_rpm; }
          turbo_rpm++;
          if (turbo_rpm > 140000) { turbo_rpm = minimum_turbo_rpm; }
        }
      }


    The loop is a lot smaller now, saved a lot of ram by not using so many longs in an overly complex array.

    Minor Changes:

    • Vane changes are set to 50ms now for speeds under 100k.
    • Moved quite a few things around just to tidy up the code.

      • Boost / Deceleration modes are in the calculate modes (100ms).
      • CANBUS reading has moved into Timer3, which will become a watchdog timer.
    • Vane calculation has a little more under it for speeds in excess of 125k.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v3.0.3 update

    Minor Changes:

    • Adjusted Bluetooth setup for a more stable startup
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v3.0.4 update

    Minor Changes:

    • Adjusted Idle boolean positions for more accuracy.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

    hakcenter

    v3.0.5 update

    Minor Changes:

    • Fixed Boost Timer code.
    TS2009 Deḇarim 8:2
    "And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.