LBB Common Code

  • 7 Replies
  • 590 Views
*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
LBB Common Code
« on: August 26, 2016, 09:52:14 AM »
Latest Version 2.2.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.
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
Re: LBB Common Code
« Reply #1 on: August 26, 2016, 09:52:23 AM »
Main Loop
Code: [Select]
void loop() {
  // Freq Measure
  unsigned long sum[32];
  float final_sum = 0.0f;
  byte count = 0;
  byte i = 0;
 
  while(!turbo_online) {
    t2.update();
    if (turbo_flags[0] == 7) {
      set_turbo_position();
      delay(25);
    }
    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();
    // Freq Measure
    if (FreqMeasure.available()) {
      // average several reading together
      sum[count] = FreqMeasure.read();
      final_sum = 0.0f;
      for (i = 0 ; i < 32 ; i++) { final_sum += sum[i]; }
      // turbo_rpm = Freq(sum / count) * 60
      final_sum = FreqMeasure.countToFrequency(final_sum / 32.0f);
      turbo_rpm = final_sum * 60.0f;
      count++;
      if (count > 31) { count = 0; }
    }
  }
  if (!bt_enabled) { Serial.println(F("Turbo OFFLINE!")); }
}
  • Using float division for more accurate rpm conversion.
  • Only keeping the last 32 counts in the array.
  • Keep i in ram.
  • Keep count in ram
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
Re: LBB Common Code
« Reply #2 on: August 26, 2016, 09:52:35 AM »
Timers

Timer 1
--Sets turbo vane position
Code: [Select]
t1.every(2, set_turbo_position);Runs every 2ms, to keep gearbox from automatically resetting to max open (no input default)
Code: [Select]
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);
    final_vane_position = vane_position;
    // Vane smoothing between large values
         if (turbo_rpm < curve_rpm[0] && (vane_position <= last_vane_position - half_cm)) { final_vane_position = last_vane_position - quarter_cm; }
    else if (!brake_mode && !boost_mode && turbo_rpm >= curve_rpm[1] && turbo_rpm < top_end_rpm) {
    // if vane_position change is 20 or more, move by half the difference
         if (vane_position >= last_vane_position + 20) { final_vane_position = vane_position + ((last_vane_position - vane_position) >> 2); }
    else if (vane_position <= last_vane_position - 20) { final_vane_position = vane_position - ((last_vane_position - vane_position) >> 2); }
    // if vane_position change is 10 or less, move by 2
         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; }
    // 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; }
    }
    //---- 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 2ms, 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
Code: [Select]
t2.every(1, keep_time);Runs every 1ms and is used to keep things in time.
Code: [Select]
void keep_time() {
  timer++;
  //---- Startup Timer ----//
  if (turbo_rpm < minimum_turbo_rpm) {
    if (startup > 249) { startup = 250; }
    startup++;
  }
  //---- Boost Mode ----//
  if (boost_timer > 0) {
    boost_timer--;
  }
  if (timer % 250 == 0 && boost_mode && boost_timer < 1) {
    boost_enable = true;
    boost_mode = false;
  }
  //---- Spare Pin Interrupt ----//
  /*
  if (timer % 250 == 0 && spare_count > 0) {
    // Spare Counting Fast
    detachInterrupt(2);
    spare_rpm[spare_counter] = 15.0f * 1000.0f * (spare_count / 250.0f);
    spare_count = 0;
    spare_value = (spare_value + ((spare_rpm[0] + spare_rpm[1] + spare_rpm[2] + spare_rpm[3]) / 4.0f)) / 2.0f;
    attachInterrupt(0, spare_counting, HIGH);
    spare_counter++;
    if (spare_counter > 3) { spare_counter = 0; }
  }
  */
  // Update vane_position every 10ms 100k+ / 25ms 60k - 100k / 50ms 0 - 60k
       if (timer % 10 == 0 && turbo_rpm > top_end_rpm) { update_vane_position = true; }
  else if (timer % 25 == 0 && turbo_rpm > curve_rpm[4]) { update_vane_position = true; }
  else if (timer % 50 == 0) { update_vane_position = true; } 
  if (timer % 10 == 0) {
    //---- Read Serial Input ----//
    check_serial();
  }
  if (timer % 25 == 0) {
    //---- Read CAN BUS ----//
    read_can_data(can_id, can_length);
  }
  if (timer % 100 == 0) {
    //---- Calculate Modes ----//
    if (turbo_rpm < top_end_rpm) { calculate_modes(); }
    //---- Calculate turbo_accel ----//
    turbo_accel = (int) (turbo_rpm - last_turbo_rpm) / 100.0f;
    last_turbo_rpm = turbo_rpm;
    //---- Deceleration Mode ----//
    if (turbo_accel < -20 && turbo_rpm > 70000) {
      deceleration_mode = true;
    } else {
      deceleration_mode = false;
    }
  }
  // Sweep vane_position
  if (timer % 25 == 0 && sweep_position) { sweep(); }
  // 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 last_turbo_rpm every 16ms
  • Update vane_position every 50ms under 60krpm
  • Update vane_position every 25ms over 60k rpm but under 100k rpm
  • Update vane_position every 10ms over 100k rpm
  • Update turbo_accelevery 100ms
  • Read CANBUS every 25ms
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
Re: LBB Common Code
« Reply #3 on: August 26, 2016, 09:52:47 AM »
Variables Wiki > here.
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
Re: LBB Common Code
« Reply #4 on: November 17, 2016, 11:11:25 AM »
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
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 695
    • 1998 12v Ram
Re: LBB Common Code
« Reply #5 on: April 02, 2018, 12:43:01 PM »
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
    AMP 2 Corinthians 8:21
    For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


    *

    Offline hakcenter

    • developer
    • Hero Member
    • *****
    • 695
      • 1998 12v Ram
    Re: LBB Common Code
    « Reply #6 on: April 02, 2018, 02:48:17 PM »
    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
    AMP 2 Corinthians 8:21
    For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.


    *

    Offline hakcenter

    • developer
    • Hero Member
    • *****
    • 695
      • 1998 12v Ram
    Re: LBB Common Code
    « Reply #7 on: June 28, 2018, 12:07:42 PM »
    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.
    AMP 2 Corinthians 8:21
    For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.