Aes encryption code
Dependencies: Crypto USBDevice mbed
Fork of larada by
main.cpp@0:aaf22a04f350, 2016-02-04 (annotated)
- Committer:
- stupid
- Date:
- Thu Feb 04 03:42:24 2016 +0000
- Revision:
- 0:aaf22a04f350
- Child:
- 1:ccb26dd9845a
cleaned up code, most everything mostly works, except the tip encryption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stupid | 0:aaf22a04f350 | 1 | #include "mbed.h" |
stupid | 0:aaf22a04f350 | 2 | #include "USBSerial.h" |
stupid | 0:aaf22a04f350 | 3 | #include "IAP.h" |
stupid | 0:aaf22a04f350 | 4 | |
stupid | 0:aaf22a04f350 | 5 | #define SERIAL //comment for USB operation, uncomment for serial |
stupid | 0:aaf22a04f350 | 6 | //#define DEBUG 1 |
stupid | 0:aaf22a04f350 | 7 | #define CALIBRATE 0 |
stupid | 0:aaf22a04f350 | 8 | #define FUNCTION_CHECK 0 |
stupid | 0:aaf22a04f350 | 9 | |
stupid | 0:aaf22a04f350 | 10 | // longest user command we will accept |
stupid | 0:aaf22a04f350 | 11 | #define MAX_COMMAND_LEN 64 |
stupid | 0:aaf22a04f350 | 12 | |
stupid | 0:aaf22a04f350 | 13 | // where to start reading stored values from the EEPROM |
stupid | 0:aaf22a04f350 | 14 | #define BASE_ADDRESS 1 |
stupid | 0:aaf22a04f350 | 15 | |
stupid | 0:aaf22a04f350 | 16 | // temperature control stuff |
stupid | 0:aaf22a04f350 | 17 | |
stupid | 0:aaf22a04f350 | 18 | // fixed values |
stupid | 0:aaf22a04f350 | 19 | #define CONTROL_INTERRUPT 1000 // every 1ms |
stupid | 0:aaf22a04f350 | 20 | #define MAX_DUTY 100 // This is both the number of times the heater_control interrupt will run per control period and the max amount of interrupts the heater triac can be on. |
stupid | 0:aaf22a04f350 | 21 | |
stupid | 0:aaf22a04f350 | 22 | // adjustable values |
stupid | 0:aaf22a04f350 | 23 | #define DEFAULT_FILTER 0.05 // first order digital filter parameter, should be [0.0 - 1]. less means filter more, a value of 1 should turn it off |
stupid | 0:aaf22a04f350 | 24 | //#define INITIAL_DUTY 25 // 25% duty generally results in a few degrees under 57C output for the ambient temperatures I've been testing at |
stupid | 0:aaf22a04f350 | 25 | #define DEFAULT_INITIAL_DUTY 45 |
stupid | 0:aaf22a04f350 | 26 | #define DEFAULT_NOMINAL_DUTY 7 // 11% duty is a good set point for Beta init (testing at 20C ambient) |
stupid | 0:aaf22a04f350 | 27 | //#define SETPOINT 435 // this results in roughly 57C at the tines on the Alphas (really 53) |
stupid | 0:aaf22a04f350 | 28 | #define DEFAULT_SETPOINT 463 //445 -> 53.1-54.1//475 -> 58-59.5// 500 -> 62.5 // 505 -> 59 //475 -> 53C // 445 // 505 seems right for second unit //535-> first unit //527 // this results in roughly 57C at the tines on the Betas |
stupid | 0:aaf22a04f350 | 29 | #define DEFAULT_TOLERANCE 2 // if we are within this many ADC counts of the setpoint don't make any changes to the duty cycle |
stupid | 0:aaf22a04f350 | 30 | //#define DUTY_TOO_LARGE 40 // If the duty gets larger than this we have problems (Alphas) |
stupid | 0:aaf22a04f350 | 31 | #define DEFAULT_DUTY_TOO_LARGE 70 // If the duty gets larger than this we have problems (Betas) -> 65 duty should result in roughly 62C tip temps |
stupid | 0:aaf22a04f350 | 32 | //#define DUTY_TOO_LARGE 85 // For testing |
stupid | 0:aaf22a04f350 | 33 | //#define DEFAULT_TEMP_LIMIT_LOWER 50 // If the temperature measures too low during operation we are either doing a bad job or have a faulty temperature sensor |
stupid | 0:aaf22a04f350 | 34 | #define DEFAULT_TEMP_LIMIT_LOWER 0 // For testing |
stupid | 0:aaf22a04f350 | 35 | //#define TEMP_LIMIT_UPPER 485 // No burning allowed (Alphas) |
stupid | 0:aaf22a04f350 | 36 | #define DEFAULT_TEMP_LIMIT_UPPER 625 // No burning allowed (Betas) |
stupid | 0:aaf22a04f350 | 37 | |
stupid | 0:aaf22a04f350 | 38 | float filter = DEFAULT_FILTER; |
stupid | 0:aaf22a04f350 | 39 | uint8_t initial_duty = DEFAULT_INITIAL_DUTY; |
stupid | 0:aaf22a04f350 | 40 | uint8_t nominal_duty = DEFAULT_NOMINAL_DUTY; |
stupid | 0:aaf22a04f350 | 41 | uint16_t setpoint = DEFAULT_SETPOINT; |
stupid | 0:aaf22a04f350 | 42 | uint8_t tolerance = DEFAULT_TOLERANCE; |
stupid | 0:aaf22a04f350 | 43 | uint8_t duty_too_large = DEFAULT_DUTY_TOO_LARGE; |
stupid | 0:aaf22a04f350 | 44 | uint16_t temp_limit_lower = DEFAULT_TEMP_LIMIT_LOWER; |
stupid | 0:aaf22a04f350 | 45 | uint16_t temp_limit_upper = DEFAULT_TEMP_LIMIT_UPPER; |
stupid | 0:aaf22a04f350 | 46 | |
stupid | 0:aaf22a04f350 | 47 | enum State { |
stupid | 0:aaf22a04f350 | 48 | IDLE, |
stupid | 0:aaf22a04f350 | 49 | WAIT_FOR_TIP, |
stupid | 0:aaf22a04f350 | 50 | INITIAL_RAMP, |
stupid | 0:aaf22a04f350 | 51 | ACTIVE, |
stupid | 0:aaf22a04f350 | 52 | ERROR, |
stupid | 0:aaf22a04f350 | 53 | }; |
stupid | 0:aaf22a04f350 | 54 | |
stupid | 0:aaf22a04f350 | 55 | enum Heater_State { |
stupid | 0:aaf22a04f350 | 56 | ON, |
stupid | 0:aaf22a04f350 | 57 | OFF, |
stupid | 0:aaf22a04f350 | 58 | }; |
stupid | 0:aaf22a04f350 | 59 | |
stupid | 0:aaf22a04f350 | 60 | // not used yet |
stupid | 0:aaf22a04f350 | 61 | enum Error { |
stupid | 0:aaf22a04f350 | 62 | NONE, |
stupid | 0:aaf22a04f350 | 63 | TEMP_TOO_LOW, |
stupid | 0:aaf22a04f350 | 64 | TEMP_TOO_HIGH, |
stupid | 0:aaf22a04f350 | 65 | DUTY_TOO_LARGE, |
stupid | 0:aaf22a04f350 | 66 | }; |
stupid | 0:aaf22a04f350 | 67 | |
stupid | 0:aaf22a04f350 | 68 | // Heater control pins |
stupid | 0:aaf22a04f350 | 69 | DigitalOut fan(P0_22); |
stupid | 0:aaf22a04f350 | 70 | DigitalOut heater_pin(P1_27); |
stupid | 0:aaf22a04f350 | 71 | AnalogIn temp_sense(P0_16); |
stupid | 0:aaf22a04f350 | 72 | |
stupid | 0:aaf22a04f350 | 73 | // LED pins |
stupid | 0:aaf22a04f350 | 74 | DigitalInOut empty_led(P0_10); |
stupid | 0:aaf22a04f350 | 75 | DigitalInOut fuel_gage_1(P0_9); |
stupid | 0:aaf22a04f350 | 76 | DigitalInOut fuel_gage_2(P0_8); |
stupid | 0:aaf22a04f350 | 77 | DigitalInOut fuel_gage_3(P1_21); |
stupid | 0:aaf22a04f350 | 78 | DigitalInOut fuel_gage_4(P1_31); |
stupid | 0:aaf22a04f350 | 79 | DigitalOut tip_light(P1_14); |
stupid | 0:aaf22a04f350 | 80 | |
stupid | 0:aaf22a04f350 | 81 | DigitalIn tip_sensor(P0_14); |
stupid | 0:aaf22a04f350 | 82 | |
stupid | 0:aaf22a04f350 | 83 | #define ON_TIME_S 2700L // The device is active for 45 minutes |
stupid | 0:aaf22a04f350 | 84 | //#define ON_TIME_S 150L |
stupid | 0:aaf22a04f350 | 85 | #define RED_LED_ON_TIME_S 300L // The red LED is active for the last 5 minutes |
stupid | 0:aaf22a04f350 | 86 | //#define RED_LED_ON_TIME_S 30L |
stupid | 0:aaf22a04f350 | 87 | #define TIP_FLASH_INTERVAL_S 20L // How often the tip will flash |
stupid | 0:aaf22a04f350 | 88 | //Serial pc(0x1f00, 0x2012, 0x0001, false); |
stupid | 0:aaf22a04f350 | 89 | |
stupid | 0:aaf22a04f350 | 90 | #ifdef SERIAL |
stupid | 0:aaf22a04f350 | 91 | Serial pc(P0_19, P0_18); // tx, rx |
stupid | 0:aaf22a04f350 | 92 | #else |
stupid | 0:aaf22a04f350 | 93 | USBSerial pc(0x1f00, 0x2012, 0x0001); |
stupid | 0:aaf22a04f350 | 94 | #endif |
stupid | 0:aaf22a04f350 | 95 | |
stupid | 0:aaf22a04f350 | 96 | Ticker control_interrupt; |
stupid | 0:aaf22a04f350 | 97 | Ticker check_limits_interrupt; |
stupid | 0:aaf22a04f350 | 98 | Ticker temperature_interrupt; |
stupid | 0:aaf22a04f350 | 99 | Ticker tick_interrupt; |
stupid | 0:aaf22a04f350 | 100 | |
stupid | 0:aaf22a04f350 | 101 | volatile Heater_State heater = OFF; |
stupid | 0:aaf22a04f350 | 102 | volatile State state = WAIT_FOR_TIP; |
stupid | 0:aaf22a04f350 | 103 | volatile Error error_state = NONE; |
stupid | 0:aaf22a04f350 | 104 | |
stupid | 0:aaf22a04f350 | 105 | volatile uint8_t duty = DEFAULT_INITIAL_DUTY; |
stupid | 0:aaf22a04f350 | 106 | volatile float temperature = 0.0; |
stupid | 0:aaf22a04f350 | 107 | unsigned long start_time = 0L; |
stupid | 0:aaf22a04f350 | 108 | unsigned long current_time = 0L; |
stupid | 0:aaf22a04f350 | 109 | bool connected = false; |
stupid | 0:aaf22a04f350 | 110 | volatile uint32_t millis = 0; |
stupid | 0:aaf22a04f350 | 111 | |
stupid | 0:aaf22a04f350 | 112 | void loop(void); |
stupid | 0:aaf22a04f350 | 113 | void calibrate(bool first); |
stupid | 0:aaf22a04f350 | 114 | void interpret(char parameter, int value); |
stupid | 0:aaf22a04f350 | 115 | void spin_lights(bool first); |
stupid | 0:aaf22a04f350 | 116 | |
stupid | 0:aaf22a04f350 | 117 | //INTERRUPT - increment this every ms since the normal mbed timer returns an int and real code needs an unsigned type |
stupid | 0:aaf22a04f350 | 118 | void tick(void) {millis++;} |
stupid | 0:aaf22a04f350 | 119 | |
stupid | 0:aaf22a04f350 | 120 | uint32_t get_time(void){ |
stupid | 0:aaf22a04f350 | 121 | uint32_t copy = 0; |
stupid | 0:aaf22a04f350 | 122 | __disable_irq(); |
stupid | 0:aaf22a04f350 | 123 | copy = millis; |
stupid | 0:aaf22a04f350 | 124 | __enable_irq(); |
stupid | 0:aaf22a04f350 | 125 | return copy; |
stupid | 0:aaf22a04f350 | 126 | } |
stupid | 0:aaf22a04f350 | 127 | |
stupid | 0:aaf22a04f350 | 128 | uint8_t get_duty(void){ |
stupid | 0:aaf22a04f350 | 129 | __disable_irq(); |
stupid | 0:aaf22a04f350 | 130 | uint8_t duty_copy = duty; |
stupid | 0:aaf22a04f350 | 131 | __enable_irq(); |
stupid | 0:aaf22a04f350 | 132 | return duty_copy; |
stupid | 0:aaf22a04f350 | 133 | } |
stupid | 0:aaf22a04f350 | 134 | |
stupid | 0:aaf22a04f350 | 135 | void set_duty(uint8_t new_duty){ |
stupid | 0:aaf22a04f350 | 136 | __disable_irq(); |
stupid | 0:aaf22a04f350 | 137 | duty = new_duty; |
stupid | 0:aaf22a04f350 | 138 | __enable_irq(); |
stupid | 0:aaf22a04f350 | 139 | } |
stupid | 0:aaf22a04f350 | 140 | |
stupid | 0:aaf22a04f350 | 141 | float get_temperature(void){ |
stupid | 0:aaf22a04f350 | 142 | __disable_irq(); |
stupid | 0:aaf22a04f350 | 143 | float temperature_copy = temperature; |
stupid | 0:aaf22a04f350 | 144 | __enable_irq(); |
stupid | 0:aaf22a04f350 | 145 | return temperature_copy; |
stupid | 0:aaf22a04f350 | 146 | } |
stupid | 0:aaf22a04f350 | 147 | |
stupid | 0:aaf22a04f350 | 148 | //pull the settings saved to flash into memory |
stupid | 0:aaf22a04f350 | 149 | void read_settings(void){ |
stupid | 0:aaf22a04f350 | 150 | uint8_t address = BASE_ADDRESS; |
stupid | 0:aaf22a04f350 | 151 | uint16_t filter_temp = 0; |
stupid | 0:aaf22a04f350 | 152 | |
stupid | 0:aaf22a04f350 | 153 | IAP iap; |
stupid | 0:aaf22a04f350 | 154 | |
stupid | 0:aaf22a04f350 | 155 | iap.read_eeprom((char*)address, (char*)&filter_temp, sizeof(filter_temp)); |
stupid | 0:aaf22a04f350 | 156 | address += sizeof(filter_temp); |
stupid | 0:aaf22a04f350 | 157 | filter = (float)filter_temp/100; |
stupid | 0:aaf22a04f350 | 158 | iap.read_eeprom((char*)address, (char*)&initial_duty, sizeof(initial_duty)); |
stupid | 0:aaf22a04f350 | 159 | address += sizeof(initial_duty); |
stupid | 0:aaf22a04f350 | 160 | iap.read_eeprom((char*)address, (char*)&setpoint, sizeof(setpoint)); |
stupid | 0:aaf22a04f350 | 161 | address += sizeof(setpoint); |
stupid | 0:aaf22a04f350 | 162 | iap.read_eeprom((char*)address, (char*)&tolerance, sizeof(tolerance)); |
stupid | 0:aaf22a04f350 | 163 | address += sizeof(tolerance); |
stupid | 0:aaf22a04f350 | 164 | iap.read_eeprom((char*)address, (char*)&duty_too_large, sizeof(duty_too_large)); |
stupid | 0:aaf22a04f350 | 165 | address += sizeof(duty_too_large); |
stupid | 0:aaf22a04f350 | 166 | iap.read_eeprom((char*)address, (char*)&temp_limit_lower, sizeof(temp_limit_lower)); |
stupid | 0:aaf22a04f350 | 167 | address += sizeof(temp_limit_lower); |
stupid | 0:aaf22a04f350 | 168 | iap.read_eeprom((char*)address, (char*)&temp_limit_upper, sizeof(temp_limit_upper)); |
stupid | 0:aaf22a04f350 | 169 | address += sizeof(temp_limit_upper); |
stupid | 0:aaf22a04f350 | 170 | iap.read_eeprom((char*)address, (char*)&nominal_duty, sizeof(nominal_duty)); |
stupid | 0:aaf22a04f350 | 171 | address += sizeof(nominal_duty); |
stupid | 0:aaf22a04f350 | 172 | } |
stupid | 0:aaf22a04f350 | 173 | |
stupid | 0:aaf22a04f350 | 174 | //write settings to persistent memory |
stupid | 0:aaf22a04f350 | 175 | void write_settings(void){ |
stupid | 0:aaf22a04f350 | 176 | IAP iap; |
stupid | 0:aaf22a04f350 | 177 | uint8_t address = 1; //BASE_ADDRESS; |
stupid | 0:aaf22a04f350 | 178 | uint16_t filter_temp = 0; |
stupid | 0:aaf22a04f350 | 179 | |
stupid | 0:aaf22a04f350 | 180 | filter_temp = (int)(filter*100); |
stupid | 0:aaf22a04f350 | 181 | |
stupid | 0:aaf22a04f350 | 182 | iap.write_eeprom((char*)&filter_temp, (char*)address, sizeof(filter_temp)); |
stupid | 0:aaf22a04f350 | 183 | address += sizeof(filter_temp); |
stupid | 0:aaf22a04f350 | 184 | iap.write_eeprom((char*)&initial_duty, (char*)address, sizeof(initial_duty)); |
stupid | 0:aaf22a04f350 | 185 | address += sizeof(initial_duty); |
stupid | 0:aaf22a04f350 | 186 | iap.write_eeprom((char*)&setpoint, (char*)address, sizeof(setpoint)); |
stupid | 0:aaf22a04f350 | 187 | address += sizeof(setpoint); |
stupid | 0:aaf22a04f350 | 188 | iap.write_eeprom((char*)&tolerance, (char*)address, sizeof(tolerance)); |
stupid | 0:aaf22a04f350 | 189 | address += sizeof(tolerance); |
stupid | 0:aaf22a04f350 | 190 | iap.write_eeprom((char*)&duty_too_large, (char*)address, sizeof(duty_too_large)); |
stupid | 0:aaf22a04f350 | 191 | address += sizeof(duty_too_large); |
stupid | 0:aaf22a04f350 | 192 | iap.write_eeprom((char*)&temp_limit_lower, (char*)address, sizeof(temp_limit_lower)); |
stupid | 0:aaf22a04f350 | 193 | address += sizeof(temp_limit_lower); |
stupid | 0:aaf22a04f350 | 194 | iap.write_eeprom((char*)&temp_limit_upper, (char*)address, sizeof(temp_limit_upper)); |
stupid | 0:aaf22a04f350 | 195 | address += sizeof(temp_limit_upper); |
stupid | 0:aaf22a04f350 | 196 | iap.write_eeprom((char*)&nominal_duty, (char*)address, sizeof(nominal_duty)); |
stupid | 0:aaf22a04f350 | 197 | address += sizeof(nominal_duty); |
stupid | 0:aaf22a04f350 | 198 | } |
stupid | 0:aaf22a04f350 | 199 | |
stupid | 0:aaf22a04f350 | 200 | //parse commands. commands take the form of a character followed by a number, delimited by "\r|\n|;" |
stupid | 0:aaf22a04f350 | 201 | void getInput(void){ |
stupid | 0:aaf22a04f350 | 202 | static int i = 0; |
stupid | 0:aaf22a04f350 | 203 | static char parameter = '_'; |
stupid | 0:aaf22a04f350 | 204 | static char buffer[MAX_COMMAND_LEN + 1]; |
stupid | 0:aaf22a04f350 | 205 | int value = 0; |
stupid | 0:aaf22a04f350 | 206 | //char *endp = NULL; |
stupid | 0:aaf22a04f350 | 207 | |
stupid | 0:aaf22a04f350 | 208 | if(!connected) return; |
stupid | 0:aaf22a04f350 | 209 | |
stupid | 0:aaf22a04f350 | 210 | // listen for commands coming in |
stupid | 0:aaf22a04f350 | 211 | #ifdef SERIAL |
stupid | 0:aaf22a04f350 | 212 | while (pc.readable()){ |
stupid | 0:aaf22a04f350 | 213 | char ch = pc.getc(); |
stupid | 0:aaf22a04f350 | 214 | #else |
stupid | 0:aaf22a04f350 | 215 | while (pc.available()){ |
stupid | 0:aaf22a04f350 | 216 | char ch = pc._getc(); |
stupid | 0:aaf22a04f350 | 217 | #endif |
stupid | 0:aaf22a04f350 | 218 | if((ch == '\r' || ch == ';' || ch == '\n') && parameter != '_'){ |
stupid | 0:aaf22a04f350 | 219 | if(i > 1){ |
stupid | 0:aaf22a04f350 | 220 | buffer[i-1] = 0; |
stupid | 0:aaf22a04f350 | 221 | value = atoi(buffer); |
stupid | 0:aaf22a04f350 | 222 | } |
stupid | 0:aaf22a04f350 | 223 | pc.printf("%c, %d", parameter, value); |
stupid | 0:aaf22a04f350 | 224 | //Serial.println("not _"); |
stupid | 0:aaf22a04f350 | 225 | interpret(parameter, value); |
stupid | 0:aaf22a04f350 | 226 | parameter = '_'; |
stupid | 0:aaf22a04f350 | 227 | buffer[0] = 0; |
stupid | 0:aaf22a04f350 | 228 | i=0; |
stupid | 0:aaf22a04f350 | 229 | break; |
stupid | 0:aaf22a04f350 | 230 | } |
stupid | 0:aaf22a04f350 | 231 | else{ |
stupid | 0:aaf22a04f350 | 232 | if(i==0) parameter = ch; |
stupid | 0:aaf22a04f350 | 233 | else buffer[i-1] = ch; |
stupid | 0:aaf22a04f350 | 234 | i++; |
stupid | 0:aaf22a04f350 | 235 | } |
stupid | 0:aaf22a04f350 | 236 | |
stupid | 0:aaf22a04f350 | 237 | if(ch == '_' || ch == '\r' || ch == ';' || ch == '\n'){ |
stupid | 0:aaf22a04f350 | 238 | parameter = '_'; |
stupid | 0:aaf22a04f350 | 239 | buffer[0] = 0; |
stupid | 0:aaf22a04f350 | 240 | i=0; |
stupid | 0:aaf22a04f350 | 241 | } |
stupid | 0:aaf22a04f350 | 242 | } |
stupid | 0:aaf22a04f350 | 243 | } |
stupid | 0:aaf22a04f350 | 244 | |
stupid | 0:aaf22a04f350 | 245 | //print usage info |
stupid | 0:aaf22a04f350 | 246 | void usage(void){ |
stupid | 0:aaf22a04f350 | 247 | if(!connected)return; |
stupid | 0:aaf22a04f350 | 248 | pc.printf("\r\nCommands are a character followed by a number.\r\n"); |
stupid | 0:aaf22a04f350 | 249 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 250 | pc.printf("Available commands are:\r\n"); |
stupid | 0:aaf22a04f350 | 251 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 252 | pc.printf("'c': duty cap [1-100]\r\n"); |
stupid | 0:aaf22a04f350 | 253 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 254 | pc.printf("'d': set the initial duty used on startup [1-100]\r\n"); |
stupid | 0:aaf22a04f350 | 255 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 256 | pc.printf("'f': set the smoothing filter for the temperature sensor."); |
stupid | 0:aaf22a04f350 | 257 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 258 | pc.printf(" smaller is more smoothing. [1-100]\r\n"); |
stupid | 0:aaf22a04f350 | 259 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 260 | pc.printf("'l': lower temperature limit. arbitrary units. [1-1000]\r\n"); |
stupid | 0:aaf22a04f350 | 261 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 262 | pc.printf("'r': reset all parameters to default values.\r\n"); |
stupid | 0:aaf22a04f350 | 263 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 264 | pc.printf("'n': set the nominal duty used on control cycle start [1-100]\r\n"); |
stupid | 0:aaf22a04f350 | 265 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 266 | pc.printf("'s': setpoint. same units as upper and lower temperature"); |
stupid | 0:aaf22a04f350 | 267 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 268 | pc.printf(" limit [1-1000]\r\n"); |
stupid | 0:aaf22a04f350 | 269 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 270 | pc.printf("'t': tolerance. no control action within this band. same "); |
stupid | 0:aaf22a04f350 | 271 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 272 | pc.printf("units as upper and lower temperature limit [1-1000]\r\n"); |
stupid | 0:aaf22a04f350 | 273 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 274 | pc.printf("'u': upper temperature limit. arbitrary units. [1-1000]\r\n"); |
stupid | 0:aaf22a04f350 | 275 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 276 | pc.printf("'w': write the current control parameters to permanent memory\r\n"); |
stupid | 0:aaf22a04f350 | 277 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 278 | pc.printf("\r\n\r\n"); |
stupid | 0:aaf22a04f350 | 279 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 280 | |
stupid | 0:aaf22a04f350 | 281 | pc.printf("Values can be modified by entering a "); |
stupid | 0:aaf22a04f350 | 282 | pc.printf("command followed by a number."); |
stupid | 0:aaf22a04f350 | 283 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 284 | pc.printf(" For example, entering 'c50' <enter> would limit the duty"); |
stupid | 0:aaf22a04f350 | 285 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 286 | pc.printf(" to 50%\r\n\r\n"); |
stupid | 0:aaf22a04f350 | 287 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 288 | |
stupid | 0:aaf22a04f350 | 289 | pc.printf("Status of individual variables can be queried by entering "); |
stupid | 0:aaf22a04f350 | 290 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 291 | pc.printf("that command name (no number afterwards), or typing any "); |
stupid | 0:aaf22a04f350 | 292 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 293 | pc.printf("unrecognised command (which prints this message).\r\n\r\n"); |
stupid | 0:aaf22a04f350 | 294 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 295 | } |
stupid | 0:aaf22a04f350 | 296 | |
stupid | 0:aaf22a04f350 | 297 | void print_active_settings(void){ |
stupid | 0:aaf22a04f350 | 298 | if(!connected)return; |
stupid | 0:aaf22a04f350 | 299 | pc.printf("Duty capped at: %u%\r\n", duty_too_large); |
stupid | 0:aaf22a04f350 | 300 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 301 | pc.printf("Initial duty is: %u\r\n", initial_duty); |
stupid | 0:aaf22a04f350 | 302 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 303 | pc.printf("Nominal duty is: %u\r\n", nominal_duty); |
stupid | 0:aaf22a04f350 | 304 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 305 | pc.printf("Temperature filter is: %d\r\n", (int)(filter * 100)); |
stupid | 0:aaf22a04f350 | 306 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 307 | pc.printf("Lower temperature limit is: %u\r\n", temp_limit_lower); |
stupid | 0:aaf22a04f350 | 308 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 309 | pc.printf("Upper temperature limit is: %u\r\n", temp_limit_upper); |
stupid | 0:aaf22a04f350 | 310 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 311 | pc.printf("Setpoint is: %u\r\n", setpoint); |
stupid | 0:aaf22a04f350 | 312 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 313 | pc.printf("Tolerance is %u\r\n\r\n", tolerance); |
stupid | 0:aaf22a04f350 | 314 | wait_ms(1); |
stupid | 0:aaf22a04f350 | 315 | } |
stupid | 0:aaf22a04f350 | 316 | |
stupid | 0:aaf22a04f350 | 317 | //interpret a user command |
stupid | 0:aaf22a04f350 | 318 | void interpret(char parameter, int value){ |
stupid | 0:aaf22a04f350 | 319 | switch(parameter){ |
stupid | 0:aaf22a04f350 | 320 | case 'c': |
stupid | 0:aaf22a04f350 | 321 | if(value != 0) duty_too_large = value; |
stupid | 0:aaf22a04f350 | 322 | pc.printf("Duty cap is %u\r\n", duty_too_large); |
stupid | 0:aaf22a04f350 | 323 | break; |
stupid | 0:aaf22a04f350 | 324 | case 'd': |
stupid | 0:aaf22a04f350 | 325 | if(value != 0) initial_duty = value; |
stupid | 0:aaf22a04f350 | 326 | pc.printf("Initial duty is %u\r\n", initial_duty); |
stupid | 0:aaf22a04f350 | 327 | break; |
stupid | 0:aaf22a04f350 | 328 | case 'f': |
stupid | 0:aaf22a04f350 | 329 | if(value != 0) filter = ((float)value) / 100.0; |
stupid | 0:aaf22a04f350 | 330 | pc.printf("Filter is %d\r\n", (int)(filter * 100)); |
stupid | 0:aaf22a04f350 | 331 | break; |
stupid | 0:aaf22a04f350 | 332 | case 'r': |
stupid | 0:aaf22a04f350 | 333 | filter = DEFAULT_FILTER; |
stupid | 0:aaf22a04f350 | 334 | initial_duty = DEFAULT_INITIAL_DUTY; |
stupid | 0:aaf22a04f350 | 335 | nominal_duty = DEFAULT_NOMINAL_DUTY; |
stupid | 0:aaf22a04f350 | 336 | setpoint = DEFAULT_SETPOINT; |
stupid | 0:aaf22a04f350 | 337 | tolerance = DEFAULT_TOLERANCE; |
stupid | 0:aaf22a04f350 | 338 | duty_too_large = DEFAULT_DUTY_TOO_LARGE; |
stupid | 0:aaf22a04f350 | 339 | temp_limit_lower = DEFAULT_TEMP_LIMIT_LOWER; |
stupid | 0:aaf22a04f350 | 340 | temp_limit_upper = DEFAULT_TEMP_LIMIT_UPPER; |
stupid | 0:aaf22a04f350 | 341 | write_settings(); |
stupid | 0:aaf22a04f350 | 342 | pc.printf("All parameters reset to default values:\r\n"); |
stupid | 0:aaf22a04f350 | 343 | print_active_settings(); |
stupid | 0:aaf22a04f350 | 344 | break; |
stupid | 0:aaf22a04f350 | 345 | case 'l': |
stupid | 0:aaf22a04f350 | 346 | if(value != 0) temp_limit_lower = value; |
stupid | 0:aaf22a04f350 | 347 | pc.printf("Lower temperature limit is %u\r\n", temp_limit_lower); |
stupid | 0:aaf22a04f350 | 348 | break; |
stupid | 0:aaf22a04f350 | 349 | case 'n': |
stupid | 0:aaf22a04f350 | 350 | if(value != 0) nominal_duty = value; |
stupid | 0:aaf22a04f350 | 351 | pc.printf("Nominal duty is %u\r\n", nominal_duty); |
stupid | 0:aaf22a04f350 | 352 | break; |
stupid | 0:aaf22a04f350 | 353 | case 's': |
stupid | 0:aaf22a04f350 | 354 | if(value != 0) setpoint = value; |
stupid | 0:aaf22a04f350 | 355 | pc.printf("Setpoint is %u\r\n", setpoint); |
stupid | 0:aaf22a04f350 | 356 | break; |
stupid | 0:aaf22a04f350 | 357 | case 't': |
stupid | 0:aaf22a04f350 | 358 | if(value != 0) tolerance = value; |
stupid | 0:aaf22a04f350 | 359 | pc.printf("Tolerance is %u\r\n", tolerance); |
stupid | 0:aaf22a04f350 | 360 | break; |
stupid | 0:aaf22a04f350 | 361 | case 'u': |
stupid | 0:aaf22a04f350 | 362 | if(value != 0) temp_limit_upper = value; |
stupid | 0:aaf22a04f350 | 363 | pc.printf("Upper temperature limit is %u\r\n", temp_limit_upper); |
stupid | 0:aaf22a04f350 | 364 | break; |
stupid | 0:aaf22a04f350 | 365 | case 'w': |
stupid | 0:aaf22a04f350 | 366 | write_settings(); |
stupid | 0:aaf22a04f350 | 367 | pc.printf("Wrote the current control parameters to memory.\r\n"); |
stupid | 0:aaf22a04f350 | 368 | break; |
stupid | 0:aaf22a04f350 | 369 | |
stupid | 0:aaf22a04f350 | 370 | |
stupid | 0:aaf22a04f350 | 371 | default: |
stupid | 0:aaf22a04f350 | 372 | usage(); |
stupid | 0:aaf22a04f350 | 373 | print_active_settings(); |
stupid | 0:aaf22a04f350 | 374 | break; |
stupid | 0:aaf22a04f350 | 375 | } |
stupid | 0:aaf22a04f350 | 376 | } |
stupid | 0:aaf22a04f350 | 377 | |
stupid | 0:aaf22a04f350 | 378 | //put everything into a low power/off state. control loop will be unaffected by this command, so that will also need to be disabled before the heater will stay off |
stupid | 0:aaf22a04f350 | 379 | void all_off(){ |
stupid | 0:aaf22a04f350 | 380 | heater_pin = 0; |
stupid | 0:aaf22a04f350 | 381 | heater = OFF; //need to treat this as volatile |
stupid | 0:aaf22a04f350 | 382 | fan = 0; |
stupid | 0:aaf22a04f350 | 383 | empty_led.input(); // |
stupid | 0:aaf22a04f350 | 384 | fuel_gage_1.input(); // = 1; |
stupid | 0:aaf22a04f350 | 385 | fuel_gage_2.input(); // = 1; |
stupid | 0:aaf22a04f350 | 386 | fuel_gage_3.input(); // = 1; |
stupid | 0:aaf22a04f350 | 387 | fuel_gage_4.input(); // = 1; |
stupid | 0:aaf22a04f350 | 388 | tip_light = 0; |
stupid | 0:aaf22a04f350 | 389 | } |
stupid | 0:aaf22a04f350 | 390 | |
stupid | 0:aaf22a04f350 | 391 | // run this to manually check the hardware works |
stupid | 0:aaf22a04f350 | 392 | // probably best to disable the heater on the first try |
stupid | 0:aaf22a04f350 | 393 | void functional_check(void){ |
stupid | 0:aaf22a04f350 | 394 | all_off(); |
stupid | 0:aaf22a04f350 | 395 | if(connected)pc.printf("Tip light\r\n"); |
stupid | 0:aaf22a04f350 | 396 | tip_light = 1; |
stupid | 0:aaf22a04f350 | 397 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 398 | all_off(); |
stupid | 0:aaf22a04f350 | 399 | if(connected)pc.printf("Empty\r\n"); |
stupid | 0:aaf22a04f350 | 400 | empty_led.output(); |
stupid | 0:aaf22a04f350 | 401 | empty_led = 0; |
stupid | 0:aaf22a04f350 | 402 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 403 | all_off(); |
stupid | 0:aaf22a04f350 | 404 | if(connected)pc.printf("Fuel Gage 1\r\n"); |
stupid | 0:aaf22a04f350 | 405 | fuel_gage_1.output(); |
stupid | 0:aaf22a04f350 | 406 | fuel_gage_1 = 0; |
stupid | 0:aaf22a04f350 | 407 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 408 | all_off(); |
stupid | 0:aaf22a04f350 | 409 | if(connected)pc.printf("Fuel Gage 2\r\n"); |
stupid | 0:aaf22a04f350 | 410 | fuel_gage_2.output(); |
stupid | 0:aaf22a04f350 | 411 | fuel_gage_2 = 0; |
stupid | 0:aaf22a04f350 | 412 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 413 | all_off(); |
stupid | 0:aaf22a04f350 | 414 | if(connected)pc.printf("Fuel Gage 3\r\n"); |
stupid | 0:aaf22a04f350 | 415 | fuel_gage_3.output(); |
stupid | 0:aaf22a04f350 | 416 | fuel_gage_3 = 0; |
stupid | 0:aaf22a04f350 | 417 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 418 | all_off(); |
stupid | 0:aaf22a04f350 | 419 | if(connected)pc.printf("Fuel Gage 4\r\n"); |
stupid | 0:aaf22a04f350 | 420 | fuel_gage_4.output(); |
stupid | 0:aaf22a04f350 | 421 | fuel_gage_4 = 0; |
stupid | 0:aaf22a04f350 | 422 | wait_ms(1000); |
stupid | 0:aaf22a04f350 | 423 | all_off(); |
stupid | 0:aaf22a04f350 | 424 | if(connected)pc.printf("Fan\r\n"); |
stupid | 0:aaf22a04f350 | 425 | fan = 1; |
stupid | 0:aaf22a04f350 | 426 | wait_ms(5000); |
stupid | 0:aaf22a04f350 | 427 | if(connected)pc.printf("Heater\r\n"); |
stupid | 0:aaf22a04f350 | 428 | heater_pin = 1; |
stupid | 0:aaf22a04f350 | 429 | |
stupid | 0:aaf22a04f350 | 430 | while(1){ |
stupid | 0:aaf22a04f350 | 431 | if(connected)pc.printf("Temp: %u\r\n", temp_sense.read_u16()>>6); |
stupid | 0:aaf22a04f350 | 432 | wait_ms(50); |
stupid | 0:aaf22a04f350 | 433 | tip_light = 1; |
stupid | 0:aaf22a04f350 | 434 | wait_ms(50); |
stupid | 0:aaf22a04f350 | 435 | tip_light = 0; |
stupid | 0:aaf22a04f350 | 436 | } |
stupid | 0:aaf22a04f350 | 437 | } |
stupid | 0:aaf22a04f350 | 438 | |
stupid | 0:aaf22a04f350 | 439 | // INTERRUPT - called every 4ms, samples the ADC and filters the value with a low pass first order digital filter |
stupid | 0:aaf22a04f350 | 440 | void get_temp(void){ |
stupid | 0:aaf22a04f350 | 441 | // not bothering with units, this means we need to keep the update constant at once every 4ms or we will need to set the filter again |
stupid | 0:aaf22a04f350 | 442 | //temperature = filter * (temp_sense.read_u16()>>6) + (1 - filter) * temperature; |
stupid | 0:aaf22a04f350 | 443 | // temperature is now in degrees C (value is at sensor, not at tips) |
stupid | 0:aaf22a04f350 | 444 | temperature = filter * (51.282 * (temp_sense.read()*3.3 - 0.4)) + (1 - filter) * temperature; |
stupid | 0:aaf22a04f350 | 445 | } |
stupid | 0:aaf22a04f350 | 446 | |
stupid | 0:aaf22a04f350 | 447 | // INTERRUPT - called every millisecond to handle maintaining the on/off state of the triac |
stupid | 0:aaf22a04f350 | 448 | void heater_control(void){ |
stupid | 0:aaf22a04f350 | 449 | static uint8_t count = 0; |
stupid | 0:aaf22a04f350 | 450 | |
stupid | 0:aaf22a04f350 | 451 | // count up once per interrupt. |
stupid | 0:aaf22a04f350 | 452 | // safety: check to make sure we haven't exceeded MAX_DUTY safety limit |
stupid | 0:aaf22a04f350 | 453 | // after duty is reached for this control period, turn the heat off |
stupid | 0:aaf22a04f350 | 454 | count++; |
stupid | 0:aaf22a04f350 | 455 | |
stupid | 0:aaf22a04f350 | 456 | if(heater == ON && fan.read()){ |
stupid | 0:aaf22a04f350 | 457 | if(count > MAX_DUTY){ |
stupid | 0:aaf22a04f350 | 458 | count = 0; |
stupid | 0:aaf22a04f350 | 459 | heater_pin = 1; |
stupid | 0:aaf22a04f350 | 460 | } |
stupid | 0:aaf22a04f350 | 461 | if(count > duty)heater_pin = 0; |
stupid | 0:aaf22a04f350 | 462 | } |
stupid | 0:aaf22a04f350 | 463 | else heater_pin = 0; |
stupid | 0:aaf22a04f350 | 464 | } |
stupid | 0:aaf22a04f350 | 465 | |
stupid | 0:aaf22a04f350 | 466 | // bail, something is wrong |
stupid | 0:aaf22a04f350 | 467 | void abort(bool error){ |
stupid | 0:aaf22a04f350 | 468 | uint16_t period = 1000; |
stupid | 0:aaf22a04f350 | 469 | if(error) period = 250; |
stupid | 0:aaf22a04f350 | 470 | // turn everything off, leave the fan on for a few seconds and flash the red LED |
stupid | 0:aaf22a04f350 | 471 | control_interrupt.detach(); |
stupid | 0:aaf22a04f350 | 472 | all_off(); |
stupid | 0:aaf22a04f350 | 473 | empty_led.input(); |
stupid | 0:aaf22a04f350 | 474 | fan = 1; |
stupid | 0:aaf22a04f350 | 475 | wait_ms(3000); |
stupid | 0:aaf22a04f350 | 476 | fan = 0; |
stupid | 0:aaf22a04f350 | 477 | while(1){ |
stupid | 0:aaf22a04f350 | 478 | empty_led.output(); |
stupid | 0:aaf22a04f350 | 479 | empty_led = 0; |
stupid | 0:aaf22a04f350 | 480 | heater_pin = 0; |
stupid | 0:aaf22a04f350 | 481 | wait_ms(period); |
stupid | 0:aaf22a04f350 | 482 | heater_pin = 0; |
stupid | 0:aaf22a04f350 | 483 | empty_led.input(); |
stupid | 0:aaf22a04f350 | 484 | heater_pin = 0; |
stupid | 0:aaf22a04f350 | 485 | wait_ms(period); |
stupid | 0:aaf22a04f350 | 486 | } |
stupid | 0:aaf22a04f350 | 487 | } |
stupid | 0:aaf22a04f350 | 488 | |
stupid | 0:aaf22a04f350 | 489 | // INTERRUPT - monitor stuff that might indicate an error condition |
stupid | 0:aaf22a04f350 | 490 | void check_limits(void){ |
stupid | 0:aaf22a04f350 | 491 | // need to move printing to the main loop |
stupid | 0:aaf22a04f350 | 492 | if(duty >= duty_too_large){ |
stupid | 0:aaf22a04f350 | 493 | if(connected){ |
stupid | 0:aaf22a04f350 | 494 | pc.printf("Error!!! Duty cycle has become unreasonably "); |
stupid | 0:aaf22a04f350 | 495 | pc.printf("large, Aborting.\r\n"); |
stupid | 0:aaf22a04f350 | 496 | } |
stupid | 0:aaf22a04f350 | 497 | abort(true); |
stupid | 0:aaf22a04f350 | 498 | } |
stupid | 0:aaf22a04f350 | 499 | |
stupid | 0:aaf22a04f350 | 500 | if(get_temperature() > temp_limit_upper){ |
stupid | 0:aaf22a04f350 | 501 | if(connected)pc.printf("Error!!! Temperature is too high, Aborting.\r\n"); |
stupid | 0:aaf22a04f350 | 502 | abort(true); |
stupid | 0:aaf22a04f350 | 503 | } |
stupid | 0:aaf22a04f350 | 504 | |
stupid | 0:aaf22a04f350 | 505 | if((state == ACTIVE || state == INITIAL_RAMP) && (get_temperature() < temp_limit_lower)){ |
stupid | 0:aaf22a04f350 | 506 | if(connected){ |
stupid | 0:aaf22a04f350 | 507 | pc.printf("%f\r\n", get_temperature()); |
stupid | 0:aaf22a04f350 | 508 | pc.printf("Error!!! Abnormally low temperature detected. "); |
stupid | 0:aaf22a04f350 | 509 | pc.printf("Please check the sensor, Aborting.\r\n"); |
stupid | 0:aaf22a04f350 | 510 | } |
stupid | 0:aaf22a04f350 | 511 | abort(true); |
stupid | 0:aaf22a04f350 | 512 | } |
stupid | 0:aaf22a04f350 | 513 | } |
stupid | 0:aaf22a04f350 | 514 | |
stupid | 0:aaf22a04f350 | 515 | // values pulled from the MCP9701T-E/LT datasheet |
stupid | 0:aaf22a04f350 | 516 | // 19.5mV/deg-C, 400mV @ 0C |
stupid | 0:aaf22a04f350 | 517 | float adc_to_temp(float adc_value){ |
stupid | 0:aaf22a04f350 | 518 | //return 0.195 * adc_value - |
stupid | 0:aaf22a04f350 | 519 | return 0.196 * adc_value - 31.322; |
stupid | 0:aaf22a04f350 | 520 | } |
stupid | 0:aaf22a04f350 | 521 | |
stupid | 0:aaf22a04f350 | 522 | void init(void){ |
stupid | 0:aaf22a04f350 | 523 | #ifdef SERIAL |
stupid | 0:aaf22a04f350 | 524 | pc.baud(115200); |
stupid | 0:aaf22a04f350 | 525 | connected = true; |
stupid | 0:aaf22a04f350 | 526 | #else |
stupid | 0:aaf22a04f350 | 527 | pc.connect(); |
stupid | 0:aaf22a04f350 | 528 | connected = pc.vbusDetected(); |
stupid | 0:aaf22a04f350 | 529 | #endif |
stupid | 0:aaf22a04f350 | 530 | |
stupid | 0:aaf22a04f350 | 531 | if(connected)pc.printf("hello\r\n"); |
stupid | 0:aaf22a04f350 | 532 | |
stupid | 0:aaf22a04f350 | 533 | tick_interrupt.attach(&tick, 0.001); |
stupid | 0:aaf22a04f350 | 534 | control_interrupt.attach(&heater_control, 0.001); |
stupid | 0:aaf22a04f350 | 535 | temperature_interrupt.attach(&get_temp, 0.004); |
stupid | 0:aaf22a04f350 | 536 | if(!CALIBRATE)check_limits_interrupt.attach(&check_limits, 0.5); |
stupid | 0:aaf22a04f350 | 537 | |
stupid | 0:aaf22a04f350 | 538 | read_settings(); |
stupid | 0:aaf22a04f350 | 539 | set_duty(initial_duty); |
stupid | 0:aaf22a04f350 | 540 | all_off(); |
stupid | 0:aaf22a04f350 | 541 | } |
stupid | 0:aaf22a04f350 | 542 | |
stupid | 0:aaf22a04f350 | 543 | // tip neeeds to be present before we can start the cycle |
stupid | 0:aaf22a04f350 | 544 | // later we will also abort the cycle if the tip is removed |
stupid | 0:aaf22a04f350 | 545 | void do_wait_for_tip(bool first){ |
stupid | 0:aaf22a04f350 | 546 | unsigned long time = get_time(); |
stupid | 0:aaf22a04f350 | 547 | unsigned long time_increment = time % 1800; |
stupid | 0:aaf22a04f350 | 548 | |
stupid | 0:aaf22a04f350 | 549 | if(first){ |
stupid | 0:aaf22a04f350 | 550 | if(connected)pc.printf("Looking for tip\r\n"); |
stupid | 0:aaf22a04f350 | 551 | } |
stupid | 0:aaf22a04f350 | 552 | if(!tip_sensor){ //tip present is low == present |
stupid | 0:aaf22a04f350 | 553 | tip_light = 1; |
stupid | 0:aaf22a04f350 | 554 | fuel_gage_1.output(); |
stupid | 0:aaf22a04f350 | 555 | fuel_gage_1 = 0; |
stupid | 0:aaf22a04f350 | 556 | fuel_gage_2.output(); |
stupid | 0:aaf22a04f350 | 557 | fuel_gage_2 = 0; |
stupid | 0:aaf22a04f350 | 558 | fuel_gage_3.output(); |
stupid | 0:aaf22a04f350 | 559 | fuel_gage_3 = 0; |
stupid | 0:aaf22a04f350 | 560 | fuel_gage_4.output(); |
stupid | 0:aaf22a04f350 | 561 | fuel_gage_4 = 0; |
stupid | 0:aaf22a04f350 | 562 | empty_led.input(); |
stupid | 0:aaf22a04f350 | 563 | state = INITIAL_RAMP; |
stupid | 0:aaf22a04f350 | 564 | if(connected)pc.printf("Found the tip\r\n"); |
stupid | 0:aaf22a04f350 | 565 | return; |
stupid | 0:aaf22a04f350 | 566 | } |
stupid | 0:aaf22a04f350 | 567 | |
stupid | 0:aaf22a04f350 | 568 | if(time_increment < 300){ |
stupid | 0:aaf22a04f350 | 569 | fuel_gage_1.input(); // = 1; |
stupid | 0:aaf22a04f350 | 570 | fuel_gage_2.input(); // = 1; |
stupid | 0:aaf22a04f350 | 571 | fuel_gage_3.input(); // = 1; |
stupid | 0:aaf22a04f350 | 572 | fuel_gage_4.input(); // = 1; |
stupid | 0:aaf22a04f350 | 573 | } |
stupid | 0:aaf22a04f350 | 574 | if(time_increment > 600){ |
stupid | 0:aaf22a04f350 | 575 | fuel_gage_1.output(); |
stupid | 0:aaf22a04f350 | 576 | fuel_gage_1 = 0; |
stupid | 0:aaf22a04f350 | 577 | } |
stupid | 0:aaf22a04f350 | 578 | if(time_increment > 900){ |
stupid | 0:aaf22a04f350 | 579 | fuel_gage_2.output(); |
stupid | 0:aaf22a04f350 | 580 | fuel_gage_2 = 0; |
stupid | 0:aaf22a04f350 | 581 | } |
stupid | 0:aaf22a04f350 | 582 | if(time_increment > 1200){ |
stupid | 0:aaf22a04f350 | 583 | fuel_gage_3.output(); |
stupid | 0:aaf22a04f350 | 584 | fuel_gage_3 = 0; |
stupid | 0:aaf22a04f350 | 585 | } |
stupid | 0:aaf22a04f350 | 586 | if(time_increment > 1500){ |
stupid | 0:aaf22a04f350 | 587 | fuel_gage_4.output(); |
stupid | 0:aaf22a04f350 | 588 | fuel_gage_4 = 0; |
stupid | 0:aaf22a04f350 | 589 | } |
stupid | 0:aaf22a04f350 | 590 | } |
stupid | 0:aaf22a04f350 | 591 | |
stupid | 0:aaf22a04f350 | 592 | //This should quickly take us up from ambient to the setpoint. |
stupid | 0:aaf22a04f350 | 593 | void do_initial_ramp(bool first){ |
stupid | 0:aaf22a04f350 | 594 | // set duty to initial_duty and wait to reach the setpoint to break out |
stupid | 0:aaf22a04f350 | 595 | static uint32_t start_time = 0; |
stupid | 0:aaf22a04f350 | 596 | static uint32_t last_print = get_time(); |
stupid | 0:aaf22a04f350 | 597 | |
stupid | 0:aaf22a04f350 | 598 | if(first){ |
stupid | 0:aaf22a04f350 | 599 | start_time = get_time(); |
stupid | 0:aaf22a04f350 | 600 | fan = 1; |
stupid | 0:aaf22a04f350 | 601 | set_duty(initial_duty); |
stupid | 0:aaf22a04f350 | 602 | heater = ON; |
stupid | 0:aaf22a04f350 | 603 | if(connected)pc.printf("Initial ramp up. Duty will be held constant until setpoint is reached.\r\n"); |
stupid | 0:aaf22a04f350 | 604 | } |
stupid | 0:aaf22a04f350 | 605 | |
stupid | 0:aaf22a04f350 | 606 | if(get_time() - start_time > 60000){ |
stupid | 0:aaf22a04f350 | 607 | if(connected)pc.printf("Took too long to reach setpoint, aborting.\r\n"); |
stupid | 0:aaf22a04f350 | 608 | abort(true); |
stupid | 0:aaf22a04f350 | 609 | } |
stupid | 0:aaf22a04f350 | 610 | |
stupid | 0:aaf22a04f350 | 611 | if(get_time() - last_print > 5000){ |
stupid | 0:aaf22a04f350 | 612 | if(connected)pc.printf("Duty: %u, Temp: %f, Time: %.2fs\r\n", get_duty(), get_temperature(), (float)get_time()/1000); |
stupid | 0:aaf22a04f350 | 613 | last_print = get_time(); |
stupid | 0:aaf22a04f350 | 614 | } |
stupid | 0:aaf22a04f350 | 615 | |
stupid | 0:aaf22a04f350 | 616 | if(get_temperature() > setpoint - tolerance){ |
stupid | 0:aaf22a04f350 | 617 | //now we are roughly up to temperature |
stupid | 0:aaf22a04f350 | 618 | set_duty(nominal_duty); |
stupid | 0:aaf22a04f350 | 619 | state = ACTIVE; |
stupid | 0:aaf22a04f350 | 620 | return; |
stupid | 0:aaf22a04f350 | 621 | } |
stupid | 0:aaf22a04f350 | 622 | } |
stupid | 0:aaf22a04f350 | 623 | |
stupid | 0:aaf22a04f350 | 624 | void do_treatment_cycle(bool first){ |
stupid | 0:aaf22a04f350 | 625 | static uint32_t start_time = 0; |
stupid | 0:aaf22a04f350 | 626 | static uint32_t control_timer = 0; |
stupid | 0:aaf22a04f350 | 627 | uint8_t duty_copy; |
stupid | 0:aaf22a04f350 | 628 | float temperature_copy; |
stupid | 0:aaf22a04f350 | 629 | |
stupid | 0:aaf22a04f350 | 630 | if(first){ |
stupid | 0:aaf22a04f350 | 631 | start_time = get_time(); |
stupid | 0:aaf22a04f350 | 632 | control_timer = start_time; |
stupid | 0:aaf22a04f350 | 633 | } |
stupid | 0:aaf22a04f350 | 634 | |
stupid | 0:aaf22a04f350 | 635 | uint32_t current_time = get_time() - start_time; |
stupid | 0:aaf22a04f350 | 636 | |
stupid | 0:aaf22a04f350 | 637 | // check if we're done |
stupid | 0:aaf22a04f350 | 638 | if(current_time >= ON_TIME_S * 1000L){ |
stupid | 0:aaf22a04f350 | 639 | if(connected)pc.printf("Done!\r\n"); |
stupid | 0:aaf22a04f350 | 640 | abort(false); |
stupid | 0:aaf22a04f350 | 641 | state = IDLE; |
stupid | 0:aaf22a04f350 | 642 | } |
stupid | 0:aaf22a04f350 | 643 | |
stupid | 0:aaf22a04f350 | 644 | //if(!tip->in_place())abort(false); |
stupid | 0:aaf22a04f350 | 645 | |
stupid | 0:aaf22a04f350 | 646 | if(current_time - control_timer > 5000){ // run the control loop every 5 seconds |
stupid | 0:aaf22a04f350 | 647 | control_timer = current_time; |
stupid | 0:aaf22a04f350 | 648 | duty_copy = get_duty(); |
stupid | 0:aaf22a04f350 | 649 | temperature_copy = get_temperature(); |
stupid | 0:aaf22a04f350 | 650 | if(temperature_copy > setpoint + tolerance) duty_copy--; |
stupid | 0:aaf22a04f350 | 651 | if(temperature_copy < setpoint - tolerance) duty_copy++; |
stupid | 0:aaf22a04f350 | 652 | set_duty(duty_copy); |
stupid | 0:aaf22a04f350 | 653 | |
stupid | 0:aaf22a04f350 | 654 | if(connected)pc.printf("Duty: %u, Temp: %f, Time: %.2fs\r\n", get_duty(), get_temperature(), ((float)get_time() - (float)start_time)/1000); |
stupid | 0:aaf22a04f350 | 655 | } |
stupid | 0:aaf22a04f350 | 656 | } |
stupid | 0:aaf22a04f350 | 657 | |
stupid | 0:aaf22a04f350 | 658 | void print_state(void){ |
stupid | 0:aaf22a04f350 | 659 | if(!connected)return; |
stupid | 0:aaf22a04f350 | 660 | switch(state){ |
stupid | 0:aaf22a04f350 | 661 | case IDLE: |
stupid | 0:aaf22a04f350 | 662 | printf("IDLE\r\n"); |
stupid | 0:aaf22a04f350 | 663 | break; |
stupid | 0:aaf22a04f350 | 664 | case WAIT_FOR_TIP: |
stupid | 0:aaf22a04f350 | 665 | printf("WAIT_FOR_TIP\r\n"); |
stupid | 0:aaf22a04f350 | 666 | break; |
stupid | 0:aaf22a04f350 | 667 | case INITIAL_RAMP: |
stupid | 0:aaf22a04f350 | 668 | printf("INITIAL_RAMP\r\n"); |
stupid | 0:aaf22a04f350 | 669 | break; |
stupid | 0:aaf22a04f350 | 670 | case ACTIVE: |
stupid | 0:aaf22a04f350 | 671 | printf("ACTIVE\r\n"); |
stupid | 0:aaf22a04f350 | 672 | break; |
stupid | 0:aaf22a04f350 | 673 | case ERROR: |
stupid | 0:aaf22a04f350 | 674 | printf("ERROR\r\n"); |
stupid | 0:aaf22a04f350 | 675 | break; |
stupid | 0:aaf22a04f350 | 676 | default: break; |
stupid | 0:aaf22a04f350 | 677 | } |
stupid | 0:aaf22a04f350 | 678 | } |
stupid | 0:aaf22a04f350 | 679 | |
stupid | 0:aaf22a04f350 | 680 | int main(){ |
stupid | 0:aaf22a04f350 | 681 | static State last_state = IDLE; |
stupid | 0:aaf22a04f350 | 682 | init(); |
stupid | 0:aaf22a04f350 | 683 | //if(FUNCTION_CHECK) functional_check(); |
stupid | 0:aaf22a04f350 | 684 | if(CALIBRATE){ |
stupid | 0:aaf22a04f350 | 685 | calibrate(true); |
stupid | 0:aaf22a04f350 | 686 | while(1)calibrate(false); |
stupid | 0:aaf22a04f350 | 687 | } |
stupid | 0:aaf22a04f350 | 688 | |
stupid | 0:aaf22a04f350 | 689 | while(1){ |
stupid | 0:aaf22a04f350 | 690 | getInput(); |
stupid | 0:aaf22a04f350 | 691 | |
stupid | 0:aaf22a04f350 | 692 | bool state_change = false; |
stupid | 0:aaf22a04f350 | 693 | if(state != last_state){ |
stupid | 0:aaf22a04f350 | 694 | state_change = true; |
stupid | 0:aaf22a04f350 | 695 | last_state = state; |
stupid | 0:aaf22a04f350 | 696 | print_state(); |
stupid | 0:aaf22a04f350 | 697 | } |
stupid | 0:aaf22a04f350 | 698 | |
stupid | 0:aaf22a04f350 | 699 | switch(state){ |
stupid | 0:aaf22a04f350 | 700 | case IDLE: |
stupid | 0:aaf22a04f350 | 701 | // later put a check to exit state per on/off switch |
stupid | 0:aaf22a04f350 | 702 | break; |
stupid | 0:aaf22a04f350 | 703 | case WAIT_FOR_TIP: |
stupid | 0:aaf22a04f350 | 704 | do_wait_for_tip(state_change); |
stupid | 0:aaf22a04f350 | 705 | break; |
stupid | 0:aaf22a04f350 | 706 | case INITIAL_RAMP: |
stupid | 0:aaf22a04f350 | 707 | do_initial_ramp(state_change); |
stupid | 0:aaf22a04f350 | 708 | spin_lights(state_change); |
stupid | 0:aaf22a04f350 | 709 | break; |
stupid | 0:aaf22a04f350 | 710 | case ACTIVE: |
stupid | 0:aaf22a04f350 | 711 | do_treatment_cycle(state_change); |
stupid | 0:aaf22a04f350 | 712 | spin_lights(false); |
stupid | 0:aaf22a04f350 | 713 | break; |
stupid | 0:aaf22a04f350 | 714 | case ERROR: |
stupid | 0:aaf22a04f350 | 715 | abort(true); |
stupid | 0:aaf22a04f350 | 716 | break; |
stupid | 0:aaf22a04f350 | 717 | default: break; |
stupid | 0:aaf22a04f350 | 718 | } |
stupid | 0:aaf22a04f350 | 719 | } |
stupid | 0:aaf22a04f350 | 720 | } |
stupid | 0:aaf22a04f350 | 721 | |
stupid | 0:aaf22a04f350 | 722 | void spin_lights(bool first){ |
stupid | 0:aaf22a04f350 | 723 | static uint32_t start_time = 0; |
stupid | 0:aaf22a04f350 | 724 | |
stupid | 0:aaf22a04f350 | 725 | if(first) start_time = get_time(); |
stupid | 0:aaf22a04f350 | 726 | uint32_t current_time = get_time() - start_time; |
stupid | 0:aaf22a04f350 | 727 | |
stupid | 0:aaf22a04f350 | 728 | uint32_t tip_time = current_time % 23000; |
stupid | 0:aaf22a04f350 | 729 | if(tip_time < 20000) tip_light = 1; |
stupid | 0:aaf22a04f350 | 730 | else tip_light = (tip_time % 100 < 50)?1:0; |
stupid | 0:aaf22a04f350 | 731 | //if(current_time > 4600) current_time = 0; |
stupid | 0:aaf22a04f350 | 732 | |
stupid | 0:aaf22a04f350 | 733 | // handle fule gage LEDs |
stupid | 0:aaf22a04f350 | 734 | // this should be more directly linked to the treatment stop condition |
stupid | 0:aaf22a04f350 | 735 | uint32_t step = (ON_TIME_S - RED_LED_ON_TIME_S)/5; |
stupid | 0:aaf22a04f350 | 736 | step *= 1000; |
stupid | 0:aaf22a04f350 | 737 | |
stupid | 0:aaf22a04f350 | 738 | if(current_time > step) fuel_gage_4.input(); |
stupid | 0:aaf22a04f350 | 739 | else{ |
stupid | 0:aaf22a04f350 | 740 | fuel_gage_4.output(); |
stupid | 0:aaf22a04f350 | 741 | fuel_gage_4 = 0; |
stupid | 0:aaf22a04f350 | 742 | } |
stupid | 0:aaf22a04f350 | 743 | if(current_time > 2*step) fuel_gage_3.input(); |
stupid | 0:aaf22a04f350 | 744 | else{ |
stupid | 0:aaf22a04f350 | 745 | fuel_gage_3.output(); |
stupid | 0:aaf22a04f350 | 746 | fuel_gage_3 = 0; |
stupid | 0:aaf22a04f350 | 747 | } |
stupid | 0:aaf22a04f350 | 748 | if(current_time > 3*step) fuel_gage_2.input(); |
stupid | 0:aaf22a04f350 | 749 | else{ |
stupid | 0:aaf22a04f350 | 750 | fuel_gage_2.output(); |
stupid | 0:aaf22a04f350 | 751 | fuel_gage_2 = 0; |
stupid | 0:aaf22a04f350 | 752 | } |
stupid | 0:aaf22a04f350 | 753 | if(current_time > 4*step) fuel_gage_1.input(); |
stupid | 0:aaf22a04f350 | 754 | else{ |
stupid | 0:aaf22a04f350 | 755 | fuel_gage_1.output(); |
stupid | 0:aaf22a04f350 | 756 | fuel_gage_1 = 0; |
stupid | 0:aaf22a04f350 | 757 | } |
stupid | 0:aaf22a04f350 | 758 | if(current_time > 5*step){ |
stupid | 0:aaf22a04f350 | 759 | empty_led.output(); |
stupid | 0:aaf22a04f350 | 760 | empty_led = 0; |
stupid | 0:aaf22a04f350 | 761 | } |
stupid | 0:aaf22a04f350 | 762 | else empty_led.input(); |
stupid | 0:aaf22a04f350 | 763 | } |
stupid | 0:aaf22a04f350 | 764 | |
stupid | 0:aaf22a04f350 | 765 | void calibrate(bool first){ |
stupid | 0:aaf22a04f350 | 766 | static uint32_t start_time = 0; |
stupid | 0:aaf22a04f350 | 767 | static uint32_t control_timer = 0; |
stupid | 0:aaf22a04f350 | 768 | uint8_t duty_copy; |
stupid | 0:aaf22a04f350 | 769 | |
stupid | 0:aaf22a04f350 | 770 | if(first){ |
stupid | 0:aaf22a04f350 | 771 | start_time = get_time(); |
stupid | 0:aaf22a04f350 | 772 | control_timer = start_time; |
stupid | 0:aaf22a04f350 | 773 | fan = 1; |
stupid | 0:aaf22a04f350 | 774 | set_duty(5); |
stupid | 0:aaf22a04f350 | 775 | heater = ON; |
stupid | 0:aaf22a04f350 | 776 | } |
stupid | 0:aaf22a04f350 | 777 | |
stupid | 0:aaf22a04f350 | 778 | uint32_t current_time = get_time() - start_time; |
stupid | 0:aaf22a04f350 | 779 | |
stupid | 0:aaf22a04f350 | 780 | if(current_time - control_timer > 15000){ // increase the duty by 5% every 15 seconds |
stupid | 0:aaf22a04f350 | 781 | if(connected)pc.printf("Duty: %u, Temp: %f, Time: %.2fs\r\n", get_duty(), get_temperature(), ((float)get_time() - (float)start_time)/1000); |
stupid | 0:aaf22a04f350 | 782 | control_timer = current_time; |
stupid | 0:aaf22a04f350 | 783 | duty_copy = get_duty(); |
stupid | 0:aaf22a04f350 | 784 | duty_copy += 5; |
stupid | 0:aaf22a04f350 | 785 | // check if we're done |
stupid | 0:aaf22a04f350 | 786 | if(duty > 75 && connected){ |
stupid | 0:aaf22a04f350 | 787 | set_duty(1); |
stupid | 0:aaf22a04f350 | 788 | pc.printf("Done!\r\n"); |
stupid | 0:aaf22a04f350 | 789 | abort(false); |
stupid | 0:aaf22a04f350 | 790 | } |
stupid | 0:aaf22a04f350 | 791 | set_duty(duty_copy); |
stupid | 0:aaf22a04f350 | 792 | } |
stupid | 0:aaf22a04f350 | 793 | } |
stupid | 0:aaf22a04f350 | 794 | |
stupid | 0:aaf22a04f350 | 795 | /* |
stupid | 0:aaf22a04f350 | 796 | void calibrate(void){ |
stupid | 0:aaf22a04f350 | 797 | // this is really not necessary because all we care about is the temperature we need to hold the sensor at, |
stupid | 0:aaf22a04f350 | 798 | // which we are assuming directly correlates to output air temp. |
stupid | 0:aaf22a04f350 | 799 | // in reality it will probably be affected by ambient temperature, |
stupid | 0:aaf22a04f350 | 800 | // humidity, air flow rate, elevation (air density), and other factors |
stupid | 0:aaf22a04f350 | 801 | static uint16_t count = 0; |
stupid | 0:aaf22a04f350 | 802 | static uint8_t duty_copy = 5; //inital duty |
stupid | 0:aaf22a04f350 | 803 | float temperature_copy; |
stupid | 0:aaf22a04f350 | 804 | static int tester_count = 0; |
stupid | 0:aaf22a04f350 | 805 | |
stupid | 0:aaf22a04f350 | 806 | current_time = get_time(); |
stupid | 0:aaf22a04f350 | 807 | count++; |
stupid | 0:aaf22a04f350 | 808 | |
stupid | 0:aaf22a04f350 | 809 | check_limits(); |
stupid | 0:aaf22a04f350 | 810 | |
stupid | 0:aaf22a04f350 | 811 | if(count > 200){ // run the control loop every second |
stupid | 0:aaf22a04f350 | 812 | count = 0; |
stupid | 0:aaf22a04f350 | 813 | tester_count++; |
stupid | 0:aaf22a04f350 | 814 | if(tester_count % 30 == 0){ //30 seconds each duty cycle |
stupid | 0:aaf22a04f350 | 815 | duty_copy += 5; |
stupid | 0:aaf22a04f350 | 816 | if(duty > 85)abort(false); |
stupid | 0:aaf22a04f350 | 817 | if(connected)pc.printf("Duty: %u\r\nTemps:\r\n", duty_copy); |
stupid | 0:aaf22a04f350 | 818 | } |
stupid | 0:aaf22a04f350 | 819 | __disable_irq(); |
stupid | 0:aaf22a04f350 | 820 | duty = duty_copy; |
stupid | 0:aaf22a04f350 | 821 | temperature_copy = temperature; |
stupid | 0:aaf22a04f350 | 822 | __enable_irq(); |
stupid | 0:aaf22a04f350 | 823 | |
stupid | 0:aaf22a04f350 | 824 | if(DEBUG && (tester_count % 30) > 24)pc.printf("\t%f\t%u\t%f\r\n", temperature_copy, temp_sense.read_u16(), temp_sense.read()); |
stupid | 0:aaf22a04f350 | 825 | } |
stupid | 0:aaf22a04f350 | 826 | }*/ |