Next revision | Previous revisionNext revisionBoth sides next revision |
common_code_v1 [2016/08/26 12:02] – created hakcenter | common_code_v1 [2016/08/26 12:51] – [Timer1] hakcenter |
---|
**Debug Serial Messages** | **Debug Serial Messages** |
\\ ''<fc #4682b4>const boolean</fc> serial_out = **true;**'' **-- enables serial output** | \\ ''<fc #4682b4>const boolean</fc> serial_out = **true;**'' **-- enables serial output** |
| |
| **Startup** |
| \\ ''<fc #4682b4>byte</fc> startup = <fc #ff0000>**200**</fc>;'' **-- startup timer to wait until turbo is ready** |
| \\ ''<fc #4682b4>boolean</fc> turbo_online = <fc #ff0000>**false**</fc>;'' -- true CANBUS from turbo is working |
| |
**Timers** | **Timers** |
\\ ''<fc #4682b4>byte</fc> current_mode = 2;'' -- run mode byte | \\ ''<fc #4682b4>byte</fc> current_mode = 2;'' -- run mode byte |
\\ ''<fc #4682b4>const String</fc> current_mode_array[7] = { "Idle", "Idle Walk Down", "Normal", "Spare", "Brake", "Cruise", "Performance" };'' | \\ ''<fc #4682b4>const String</fc> current_mode_array[7] = { "Idle", "Idle Walk Down", "Normal", "Spare", "Brake", "Cruise", "Performance" };'' |
\\ ''<fc #4682b4>boolean</fc> idle_mode = false;'' -- | \\ ''<fc #4682b4>boolean</fc> idle_mode = false;'' -- toggles when turbo_rpm < idle_rpm |
\\ ''<fc #4682b4>boolean</fc> idle_walkdown_mode = false;'' -- toggles when code is walking turbo down to idle | \\ ''<fc #4682b4>boolean</fc> idle_walkdown_mode = false;'' -- toggles when code is walking turbo down to idle |
\\ ''<fc #4682b4>boolean</fc> spare_mode = false;'' **-- spare digital i/o toggle** | \\ ''<fc #4682b4>boolean</fc> spare_mode = false;'' **-- spare digital i/o toggle** |
\\ ''<fc #4682b4>const byte</fc> perf_pos_2 = <fc #ffa500>**80**</fc>;'' **-- position 2 for performance toggle** | \\ ''<fc #4682b4>const byte</fc> perf_pos_2 = <fc #ffa500>**80**</fc>;'' **-- position 2 for performance toggle** |
| |
| **Turbo Idle** |
| \\ ''<fc #4682b4>long</fc> idle_rpm = <fc #ffa500>**11000**</fc>;'' -- under this value turbo is idling |
| \\ ''<fc #4682b4>long</fc> idle_walkdown_rpm = <fc #ffa500>**11000**</fc>;'' -- once turbo_rpm reaches this, start walkdown process |
| |
**Turbo Curves & Factors** | **Turbo Factors** |
\\ ''<fc #4682b4>const byte</fc> two_cm = <fc #ff0000>**80**</fc>;'' -- vane value for 2 cm² | \\ ''<fc #4682b4>const byte</fc> two_cm = <fc #ff0000>**80**</fc>;'' -- vane value for 2 cm² |
\\ ''<fc #4682b4>const byte</fc> one_cm = <fc #ff0000>**40**</fc>;'' -- vane value for 1 cm² | \\ ''<fc #4682b4>const byte</fc> one_cm = <fc #ff0000>**40**</fc>;'' -- vane value for 1 cm² |
\\ ''<fc #4682b4>const byte</fc> half_cm = <fc #ff0000>**20**</fc>;'' -- vane value for 1/2 cm² | \\ ''<fc #4682b4>const byte</fc> half_cm = <fc #ff0000>**20**</fc>;'' -- vane value for 1/2 cm² |
\\ ''<fc #4682b4>const byte</fc> quarter_cm = <fc #ff0000>**10**</fc>;'' -- vane value for 1/4 cm² | \\ ''<fc #4682b4>const byte</fc> quarter_cm = <fc #ff0000>**10**</fc>;'' -- vane value for 1/4 cm² |
\\ ''<fc #4682b4>const unsigned int</fc> idle_rpm = 12000;'' -- rpm when turbo is considered idling | |
\\ ''<fc #4682b4>const unsigned int</fc> idle_walkdown_rpm = 21000;'' | **Turbo Curves** |
| \\ ''<fc #4682b4>byte</fc> current_curve = 0;'' -- current curve |
\\ ''<fc #4682b4>const unsigned long</fc> top_end_rpm = 105000;'' -- first point in top end curve | \\ ''<fc #4682b4>const unsigned long</fc> top_end_rpm = 105000;'' -- first point in top end curve |
\\ ''<fc #4682b4>const unsigned int</fc> curve_rpm[5] = { <fc #ff0000>**17000, 22000, 30000, 45000, 65000**</fc> };'' **-- curve rpm points** | \\ ''<fc #4682b4>const unsigned int</fc> curve_rpm[5] = { <fc #ff0000>**17000, 22000, 30000, 45000, 65000**</fc> };'' **-- curve rpm points** |
\\ ''<fc #4682b4>const unsigned int</fc> turbo_curve_4[5] = { <fc #ffa500>**820,780,740,700,660**</fc> };'' | \\ ''<fc #4682b4>const unsigned int</fc> turbo_curve_4[5] = { <fc #ffa500>**820,780,740,700,660**</fc> };'' |
| |
**Turbo** | **Turbo Variables** |
\\ ''<fc #4682b4>unsigned int</fc> minimum_turbo_rpm = <fc #ffa500>**1000**</fc>;'' **-- minimum required turbo rpm for pretty much everything** | \\ ''<fc #4682b4>unsigned int</fc> minimum_turbo_rpm = <fc #ffa500>**1000**</fc>;'' **-- minimum required turbo rpm for pretty much everything** |
\\ ''<fc #4682b4>long</fc> turbo_rpm = 0;'' -- current turbo rpm | \\ ''<fc #4682b4>long</fc> turbo_rpm = 0;'' -- current turbo rpm |
\\ ''<fc #4682b4>long</fc> last_turbo_rpm[2] = { 0,0 };'' -- turbo rpm 10ms & 100ms ago | \\ ''<fc #4682b4>long</fc> last_turbo_rpm[2] = { 0,0 };'' -- turbo rpm 10ms & 100ms ago |
\\ ''<fc #4682b4>int</fc> turbo_accel[3] = { 0,0,0 };'' **-- turbo acceleration variable** | \\ ''<fc #4682b4>int</fc> turbo_accel[3] = { 0,0,0 };'' **-- turbo acceleration variable** |
| |
| **CANBUS Variables** |
| \\ ''<fc #4682b4>long</fc> can_id = <fc #ff0000>**0**</fc>;'' |
| \\ ''<fc #4682b4>byte</fc> can_length;'' |
| \\ ''<fc #4682b4>byte</fc> can_data[8] = <fc #ff0000>**{ 0, 0, 0, 0, 0, 0, 0, 0 }**</fc>;'' |
| \\ ''<fc #4682b4>byte</fc> turbo_flags[8] = <fc #ff0000>**{ 0, 0, 0, 0, 0, 0, 0, 0 }**</fc>;'' |
| \\ ''<fc #4682b4>int</fc> turbo_position = <fc #ff0000>**0**</fc>;'' |
| \\ ''<fc #4682b4>byte</fc> turbo_temp = <fc #ff0000>**0**</fc>;'' |
| \\ ''<fc #4682b4>int</fc> turbo_cmd_position = <fc #ff0000>**0**</fc>;'' |
| \\ ''<fc #4682b4>byte</fc> turbo_feedback = <fc #ff0000>**0**</fc>;'' |
| |
| **Sweep Variables** |
| \\ ''<fc #4682b4>boolean</fc> sweep_position = <fc #ff0000>**false**</fc>;'' **-- toggles on sweep command** |
| \\ ''<fc #4682b4>boolean</fc> sweep_update = <fc #ff0000>**false**</fc>;'' **-- toggles every sweep** |
| \\ ''<fc #4682b4></fc>int sweep_counter = <fc #ff0000>**0**</fc>;'' **-- counter chooses direction of sweep** |
| |
**Timer2 Variables** | **Timer2 Variables** |
==== Timer1 ==== | ==== Timer1 ==== |
<code> | <code> |
void set_turbo_position() { | /* |
// Keep vane position within constraints | * Copyright (c) 2014-2016, Curtis R Hacker |
constrain(vane_position, min_position, max_position); | * All rights reserved. |
final_vane_position = vane_position; | * |
// Vane smoothing between large values | * This work is licensed under the |
if (turbo_rpm < top_end_rpm && turbo_rpm >= curve_rpm[0] && !brake_mode) { | * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. |
if (vane_position >= last_vane_position + 20 && last_vane_position < max_position - 10) { | * To view a copy of this license, |
final_vane_position = last_vane_position + 10; | * visit http://creativecommons.org/licenses/by-nc-sa/4.0/. |
} else if (vane_position <= last_vane_position - 20 && last_vane_position > min_position + 10) { | */ |
final_vane_position = last_vane_position - 10; | |
} else if (vane_position - 10 >= last_vane_position || vane_position + 10 <= last_vane_position) { | void sweep() { |
if (vane_position > last_vane_position + 2 && last_vane_position < max_position - 2) { | if (turbo_feedback > 110) { |
final_vane_position = last_vane_position + 2; | Serial.print(F("POS: ")); |
} else if (vane_position < last_vane_position - 2 && last_vane_position > min_position + 2) { | Serial.print(turbo_cmd_position); |
final_vane_position = last_vane_position - 2; | Serial.print(F(" | Feedback : ")); |
| Serial.print(turbo_feedback); |
| Serial.println(F("%")); |
| } |
| if (sweep_update) { |
| sweep_update = false; |
| if (sweep_position && sweep_counter < 2) { |
| if (sweep_counter < 1) { |
| vane_position++; |
} | } |
| if (vane_position > 960) { sweep_counter++; } |
| if (sweep_counter > 0) { |
| vane_position--; |
| } |
| if (vane_position < 40) { sweep_counter++; } |
} | } |
} else if (turbo_rpm > top_end_rpm) { | if (sweep_counter == 2 || !sweep_position) { |
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; } | sweep_position = false; |
| sweep_update = false; |
| sweep_counter = 0; |
| Serial.println(F("Sweep done..")); |
| } |
| } else { sweep_update = true; } |
| } |
| |
| 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 - 20 && last_vane_position > min_position + 10)) { final_vane_position = last_vane_position - 10; } |
| 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[0] && 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 position on decel |
| if (turbo_accel[2] < -20 && turbo_rpm > 30000) { |
| 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; |
} | } |
// Update last_vane_position | |
last_vane_position = final_vane_position; | |
byte lo_byte = lowByte(final_vane_position); | byte lo_byte = lowByte(final_vane_position); |
byte hi_byte = highByte(final_vane_position); | byte hi_byte = highByte(final_vane_position); |
// data[2] = 0x02 for recalibrating gearbox | // data[2] = 0x02 for recalibrating gearbox |
// Load message and send | // Load message and send |
CAN1.send (0x0CFFC600, extID, 8, data); | send_turbo_position(data); |
} | } |
</code> | </code> |