User Tools

Site Tools


common_code_v1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
common_code_v1 [2016/08/26 12:02] – created hakcentercommon_code_v1 [2016/08/26 12:52] (current) – [Timer1] hakcenter
Line 9: Line 9:
 **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**
Line 18: Line 22:
 \\ ''<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**
Line 43: Line 47:
 \\ ''<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**
Line 59: Line 67:
 \\ ''<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**
Line 222: Line 245:
 ==== Timer1 ==== ==== Timer1 ====
 <code> <code>
-void set_turbo_position() { +void sweep() { 
-  // Keep vane position within constraints +  if (turbo_feedback > 110) { 
-  constrain(vane_position, min_position, max_position); +    Serial.print(F("POS: ")); 
-  final_vane_position = vane_position+    Serial.print(turbo_cmd_position)
-  // Vane smoothing between large values +    Serial.print(F(" | Feedback : ")); 
-  if (turbo_rpm < top_end_rpm && turbo_rpm >= curve_rpm[0] && !brake_mode{ +    Serial.print(turbo_feedback); 
-    if (vane_position >= last_vane_position + 20 && last_vane_position < max_position - 10+    Serial.println(F("%")); 
-      final_vane_position = last_vane_position + 10+  } 
-    else if (vane_position <= last_vane_position - 20 && last_vane_position > min_position + 10) { +  if (sweep_update) { 
-      final_vane_position last_vane_position - 10+    sweep_update false
-    } else if (vane_position - 10 >= last_vane_position || vane_position + 10 <= last_vane_position) { +    if (sweep_position && sweep_counter < 2) { 
-      if (vane_position > last_vane_position + 2 && last_vane_position max_position - 2) { +      if (sweep_counter 1) { 
-        final_vane_position = last_vane_position + 2; +        vane_position++;
-      } else if (vane_position last_vane_position - 2 && last_vane_position > min_position + 2) { +
-        final_vane_position = last_vane_position - 2;+
       }       }
 +      if (vane_position > 960) { sweep_counter++; }
 +      if (sweep_counter > 0) {
 +        vane_position--;
 +      }
 +      if (vane_position < 40) { sweep_counter++; }
 +    }
 +    if (sweep_counter == 2 || !sweep_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;
     }     }
-  else if (turbo_rpm top_end_rpm) { +    */ 
-    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}+    // 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);
Line 249: Line 318:
   // 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>
Line 257: Line 326:
 void keep_time() { void keep_time() {
   timer++;   timer++;
 +  // Startup Timer
 +  if (turbo_rpm < minimum_turbo_rpm) {
 +    if (startup > 249) { startup = 250; }
 +    startup++;
 +  }
   // Update vane_position every 10ms 100k+ / 25ms 60k - 100k / 100ms 0 - 60k   // Update vane_position every 10ms 100k+ / 25ms 60k - 100k / 100ms 0 - 60k
        if (timer % 10 == 0 && turbo_rpm > top_end_rpm) { update_vane_position = true; }        if (timer % 10 == 0 && turbo_rpm > top_end_rpm) { update_vane_position = true; }
Line 271: Line 345:
     if (turbo_rpm < top_end_rpm) { turbo_accel[2] = turbo_accel[1]; } else { turbo_accel[2] = turbo_accel[0]; }     if (turbo_rpm < top_end_rpm) { turbo_accel[2] = turbo_accel[1]; } else { turbo_accel[2] = turbo_accel[0]; }
   }   }
 +  // Read Serial Input
 +  if (timer % 10 == 0 && check_serial()) { read_serial(); }
 +  // Read CAN BUS
 +  if (timer % 25 == 0) { read_can_data(can_id, can_length); }
   // Modes   // Modes
   if (timer % 100 == 0 && turbo_rpm < top_end_rpm) { calculate_modes(); }   if (timer % 100 == 0 && turbo_rpm < top_end_rpm) { calculate_modes(); }
 +  // Sweep vane_position
 +  if (timer % 25 == 0 && sweep_position) { sweep(); }
   // Calculate vane_position   // Calculate vane_position
-  if (update_vane_position) {+  if (update_vane_position && !sweep_position) {
     update_vane_position = false;     update_vane_position = false;
     calculate_vane_position();     calculate_vane_position();
common_code_v1.txt · Last modified: 2016/08/26 12:52 by hakcenter