Main Menu

LBB v1.0 Common Code

Started by hakcenter, April 13, 2015, 12:03:29 PM

hakcenter

Latest Version 1.3.1
You may 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
  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
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(2, set_turbo_position);
Runs every 2ms, 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);
    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 && 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;
    spare_count_1 = 0;
    spare_count_2 = 0;
  }
  byte lo_byte = lowByte(final_vane_position);
  byte hi_byte = highByte(final_vane_position);
  byte data[] = { lo_byte, hi_byte, 0x01, 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
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++;
  }
  //---- Spare Pin Interrupt ----//
  /*
  if (timer % 250 == 0 && startup < 2 && spare_count_1 > 0) {
    // Spare Counting Fast
    spare_rpm_1[spare_counter_1] = spare_count_1 * 125;
    spare_count_1 = 0;
    spare_value_fast = (spare_rpm_1[0] + spare_rpm_1[1] + spare_rpm_1[2] + spare_rpm_1[3]) / 4.0f;
    spare_counter_1++;
    if (spare_counter_1 == 5) { spare_counter_1 = 0; }
    if (spare_value < 1500) {
      spare_value = (spare_value_fast + spare_value_slow) / 2.0f;
    } else {
      spare_value = spare_value_fast;
    }
  }
  if (timer % 1000 == 0 && startup < 2 && spare_count_2 > 0) {
    // Spare Counting Slow
    spare_rpm_2[spare_counter_2] = spare_count_2 * 25;
    spare_count_2 = 0;
    spare_value_slow = (spare_rpm_2[0] + spare_rpm_2[1]) / 2.0f;
    spare_counter_2++;
    if (spare_counter_2 == 3) { spare_counter_2 = 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 ----//
    if (check_serial()) { read_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
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

#4
v1.0.1 update

Turbo idle (turbo_rpm < 20000), and millis output boolean (default false)
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

#5
v1.0.2 update

Adjusted curves slightly
Adjusted mid range mapping
Added vane_position smoothing between large value changes under 100k rpm
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

v1.0.3 update

Adjusted curves up 25, so curve 2 is now 1, etc. (adjust jumpers)
Adjusted curve code to be mapped and dynamically changing by variable factor array based on rpm. So the turbo isn't choking your motor after its starting to spin up.
Added staging_enabled boolean, default is false. for a more linear mapping for top end control, bypass mapping and go back to staging by changing false to true
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

v1.0.4 update

Added in cruise and exhaust brake functions and tied into external switches. Ground appropriate pin to activate.
Total curve code has been adjusted RPM wise, more turbo singing.
Minimum RPM has dropped slightly.
Top end curve adjusted to be slightly more aggressive.


Performance mode will be the next thing I'm working on, anyone with back pressure sensors.. check the exhaust braking to verify it is within safe bounds. Seemed fine on my unloaded truck.
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

#8
v1.0.5 update
Added idle_position variable - position for < 11000 rpm
Added one_cm & half_cm variable

Adjusted idle transition using idle_position variable
Adjusted curve code and curves
Adjusted smoothing to remove 1/4 cm jitter

Serial output changes include:
APOS is actual vane position
CPOS is calculated position
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

v1.1.0 update

Curves drastically altered. Be aware.
Serial output with more verbosity of curves, and units.

Performance mode! yaa. Performance mode uses vane position 800 if current curve + performance addition isn't at least position 800, then follows top end curve out.

Brake mode works pretty great. Following Cummins position 940 warm-up / braking mode, No constraining so rpm's past the map command are even lower, so its safer.

Lots of typos fixed, more variable changes to even list, will update variable page with more information on how to tweak variables.

BTW, tabbed code now!. I'll probably make a new thread, and post changes to the individual ino's inside it, as things develop further.
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

#10
v1.1.1 update

Accel variable added.
Lots of code cleanup including:

  • removal of the millis variables
  • removal of the staging code
  • turbo vane smoothing does not apply during certain criteria, brake_mode, off idle snap, etc
Minor speed ups in coding over various sections

  • calculate_mode now uses direct port access for that 12uS speed up
  • main_loop uses a multiplier over bitshifting for that unknown amount of extra speedz
  • serial_output no longer does addition but String manipulation for 200-300uS savings
  • quite a few variables moved from const to standard ram since they compute faster for whatever reason
Idle walkdown should be working as intended now. It was falling out of a few boolean checks


I haven't included the accel variable into any calculations as I'm still reviewing its functionality.
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

v1.1.2 update

Accel variable tuning
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

#12
v1.3.0e update!

e is for Experimental!

Major Changes:

  • Tons of strings converted to use flash, saves a lot of memory
  • Free memory means a lot of values were switched to longs, so now you can choose rpm values in the millions (or higher than 65,535)
  • You can talk to the controller now
  • CAN BUS rx/tx integration, now you can find out values from the turbo

Minor changes:

  • Tabs renamed
  • Some tabs combined

Talking commands:

  • output - starts the normal wall of info you like
  • sweep - sweeps the entire range of the controller DO NOT USE WHILE DRIVING
  • position - current commanded position
  • accel - current acceleration in rpm/s
  • temp - current controller temp (onboard ? who knows it does work though)
  • turbo_rpm - current turbo rpm
  • turbo_curve - outputs curve
  • turbo_curve#= substitute # for the array selection (0 for first number, 1 for second etc), and a value
  • curve_rpm - outputs set curve rpm points
  • curve_rpm#= substitute # for array selection as above, and rpm value for changing the points.. keep them incremental

All commands are case sensitive right now, maybe will change it to not be, but meh I'm still trying to figure out an anti surge code that'll work.

Very exciting to communicate. It runs in the background takes up no real computing time, and runs on the timer @ 10ms. CAN BUS is read every 25ms, which is about as fast as you can talk to the controller (sometimes faster sometimes slower, 25 was the better round number).

If you want to bypass typing output, to start logging, just change the serial_out = false back to true. output will just toggle the boolean.
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

#13
v1.3.0 update!

1.3.0 is stable!

Major Changes:

  • Deceleration mode added
  • EEPROM code added
  • Spare counting added
  • Cruise mode set to hover 65,000rpm
  • Brake mode walks into max_position

Minor changes:

  • Controller vane updates 10ms, 25ms, 50ms
  • Vane smoothing on first vane position transistion
  • turbo_accel is only updating at 100ms

REMINDER: If you want to bypass typing output, to start logging, just change the serial_out = false back to true. output will just toggle the boolean, otherwise remember to type output into the serial logger to get data


Big changes really is to make this code semi consistent with the 2.0 standalone box. Bluetooth stuff has been removed. You can use the eeprom code at your leisure . If you want to use the spare pin for counting, you will need to jump pin 9 to pin 2

For those that just want the spare as input/output, remember to change #define SPARE_PIN 2 to #define SPARE_PIN 9

This will most likely be the very last update
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

#14
v1.3.1 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.