Rev 48 | Rev 50 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 48 | Rev 49 | ||
---|---|---|---|
Line 20... | Line 20... | ||
20 | /* Includes ------------------------------------------------------------------*/ |
20 | /* Includes ------------------------------------------------------------------*/ |
21 | #include "main.h" |
21 | #include "main.h" |
22 | 22 | ||
23 | /* Private includes ----------------------------------------------------------*/ |
23 | /* Private includes ----------------------------------------------------------*/ |
24 | /* USER CODE BEGIN Includes */ |
24 | /* USER CODE BEGIN Includes */ |
- | 25 | #include <string.h> |
|
25 | #include "libSerial/serial.h" |
26 | #include "libSerial/serial.h" |
26 | #include "libPLX/plx.h" |
27 | #include "libPLX/plx.h" |
27 | #include "misc.h" |
28 | #include "misc.h" |
28 | 29 | ||
29 | #include "libIgnTiming/rpm.h" |
30 | #include "libIgnTiming/rpm.h" |
Line 76... | Line 77... | ||
76 | volatile char TimerFlag = 0; |
77 | volatile char TimerFlag = 0; |
77 | 78 | ||
78 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
79 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
79 | volatile char NoSerialIn = 0; |
80 | volatile char NoSerialIn = 0; |
80 | 81 | ||
81 | - | ||
- | 82 | // an array containing values being one more than incoming index for all observations or zero |
|
- | 83 | // if no incoming index |
|
- | 84 | char obsIndex[PLX_MAX_OBS]; |
|
82 | 85 | ||
83 | // storage for ADC |
86 | // storage for ADC |
84 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
87 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
85 | 88 | ||
86 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
89 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
Line 150... | Line 153... | ||
150 | float adc_vref = STM32REF * (4096.0 * Scale) / adc_val; // the estimate for checking |
153 | float adc_vref = STM32REF * (4096.0 * Scale) / adc_val; // the estimate for checking |
151 | 154 | ||
152 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
155 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
153 | } |
156 | } |
154 | 157 | ||
155 | void ProcessRPM(int instance) |
158 | void ProcessRPM(void) |
156 | { |
159 | { |
157 | static unsigned int Coded_RPM = 0; |
160 | static unsigned int Coded_RPM = 0; |
158 | int32_t rpm = CalculateRPM(); |
161 | int32_t rpm = CalculateRPM(); |
159 | if (rpm >= 0) |
162 | if (rpm >= 0) |
160 | Coded_RPM = rpm / 19.55; |
163 | Coded_RPM = rpm / 19.55; |
161 | 164 | ||
162 | // send the current RPM *calculation |
165 | // send the current RPM *calculation |
163 | plx_sendword(PLX_RPM); |
166 | plx_sendword(PLX_RPM); |
164 | PutCharSerial(&uc1, instance); |
167 | PutCharSerial(&uc1, obsIndex[PLX_RPM]++); |
165 | plx_sendword(Coded_RPM / Scale); |
168 | plx_sendword(Coded_RPM / Scale); |
166 | } |
169 | } |
167 | 170 | ||
168 | // this uses a MAX6675 which is a simple 16 bit read |
171 | // this uses a MAX6675 which is a simple 16 bit read |
169 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
172 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
Line 175... | Line 178... | ||
175 | #define CORR 3 |
178 | #define CORR 3 |
176 | 179 | ||
177 | uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = {[0 ... NUM_SPI_TEMP_SENS - 1] = 0}; |
180 | uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = {[0 ... NUM_SPI_TEMP_SENS - 1] = 0}; |
178 | 181 | ||
179 | /// \param item The array index to send |
182 | /// \param item The array index to send |
180 | /// \param instance The instance to send over the bus |
- | |
181 | /// \param type the code to use for this observation |
183 | /// \param type the code to use for this observation |
182 | void ProcessTemp(char item, int instance, enum PLX_Observations type) |
184 | void ProcessTemp(char item, enum PLX_Observations type) |
183 | { |
185 | { |
184 | if (item > NUM_SPI_TEMP_SENS) |
186 | if (item > NUM_SPI_TEMP_SENS) |
185 | return; |
187 | return; |
186 | plx_sendword(type); |
188 | plx_sendword(type); |
187 | PutCharSerial(&uc1, instance); |
189 | PutCharSerial(&uc1, obsIndex[type]++); |
188 | plx_sendword(Temp_Observations[(int)item]); |
190 | plx_sendword(Temp_Observations[(int)item]); |
189 | } |
191 | } |
190 | 192 | ||
191 | /// \brief Reset the temperature chip select system |
193 | /// \brief Reset the temperature chip select system |
192 | void resetTempCS(void) |
194 | void resetTempCS(void) |
Line 254... | Line 256... | ||
254 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
256 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
255 | } |
257 | } |
256 | } |
258 | } |
257 | 259 | ||
258 | // 1023 is 20.00 volts. |
260 | // 1023 is 20.00 volts. |
- | 261 | /// \param item - used to lookup the index of the local reading |
|
259 | void ProcessBatteryVoltage(int instance) |
262 | void ProcessBatteryVoltage(int item) |
260 | { |
263 | { |
261 | float reading = FILT_Samples[instance] * ADC_Scale; |
264 | float reading = FILT_Samples[item] * ADC_Scale; |
262 | reading = reading * 7.8125; // real voltage |
265 | reading = reading * 7.8125; // real voltage |
263 | reading = reading * 51.15; // PLC scaling = 1023/20 |
266 | reading = reading * 51.15; // PLC scaling = 1023/20 |
264 | 267 | ||
265 | plx_sendword(PLX_Volts); |
268 | plx_sendword(PLX_Volts); |
266 | PutCharSerial(&uc1, instance); |
269 | PutCharSerial(&uc1, obsIndex[PLX_Volts]++); |
267 | plx_sendword((uint16_t)reading); |
270 | plx_sendword((uint16_t)reading); |
268 | } |
271 | } |
269 | 272 | ||
270 | void ProcessCPUTemperature(int instance) |
273 | void ProcessCPUTemperature(void) |
271 | { |
274 | { |
272 | // this is defined in the STM32F103 reference manual . # |
275 | // this is defined in the STM32F103 reference manual . # |
273 | // V25 = 1.43 volts |
276 | // V25 = 1.43 volts |
274 | // Avg_slope = 4.3mV /degree C |
277 | // Avg_slope = 4.3mV /degree C |
275 | // temperature = {(V25 - VSENSE) / Avg_Slope} + 25 |
278 | // temperature = {(V25 - VSENSE) / Avg_Slope} + 25 |
Line 284... | Line 287... | ||
284 | 287 | ||
285 | // int32_t result = 800 * ((int32_t) temp_val - TS_CAL30); |
288 | // int32_t result = 800 * ((int32_t) temp_val - TS_CAL30); |
286 | // result = result / (TS_CAL110 - TS_CAL30) + 300; |
289 | // result = result / (TS_CAL110 - TS_CAL30) + 300; |
287 | 290 | ||
288 | plx_sendword(PLX_FluidTemp); |
291 | plx_sendword(PLX_FluidTemp); |
289 | PutCharSerial(&uc1, instance); |
292 | PutCharSerial(&uc1, obsIndex[PLX_FluidTemp]++); |
290 | plx_sendword(result); |
293 | plx_sendword(result); |
291 | } |
294 | } |
292 | 295 | ||
293 | // the MAP sensor is giving us a reading of |
296 | // the MAP sensor is giving us a reading of |
294 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
297 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
Line 297... | Line 300... | ||
297 | // Real Displayed |
300 | // Real Displayed |
298 | // 989 968 |
301 | // 989 968 |
299 | // 994.1 986 |
302 | // 994.1 986 |
300 | // 992.3 984 |
303 | // 992.3 984 |
301 | 304 | ||
302 | void ProcessMAP(int instance) |
305 | void ProcessMAP(void) |
303 | { |
306 | { |
304 | // Using ADC_Samples[3] as the MAP input |
307 | // Using ADC_Samples[3] as the MAP input |
305 | float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale; |
308 | float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale; |
306 | reading = reading * 2.016; // real voltage |
309 | reading = reading * 2.016; // real voltage |
307 | // values computed from slope / intercept of map.ods |
310 | // values computed from slope / intercept of map.ods |
308 | // reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5 |
311 | // reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5 |
309 | // using a pressure gauge. |
312 | // using a pressure gauge. |
310 | reading = (reading)*150 + 326; |
313 | reading = (reading)*150 + 326; |
311 | 314 | ||
312 | plx_sendword(PLX_MAP); |
315 | plx_sendword(PLX_MAP); |
313 | PutCharSerial(&uc1, instance); |
316 | PutCharSerial(&uc1, obsIndex[PLX_MAP]++); |
314 | plx_sendword((uint16_t)reading); |
317 | plx_sendword((uint16_t)reading); |
315 | } |
318 | } |
316 | 319 | ||
317 | // the Oil pressi sensor is giving us a reading of |
320 | // the Oil pressi sensor is giving us a reading of |
318 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
321 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
319 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
322 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
320 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
323 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
321 | 324 | ||
322 | void ProcessOilPress(int instance) |
325 | void ProcessOilPress(void) |
323 | { |
326 | { |
324 | // Using ADC_Samples[2] as the MAP input |
327 | // Using ADC_Samples[2] as the MAP input |
325 | float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale; |
328 | float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale; |
326 | reading = reading * 2.00; // real voltage |
329 | reading = reading * 2.00; // real voltage |
327 | reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200 |
330 | reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200 |
328 | 331 | ||
329 | plx_sendword(PLX_FluidPressure); |
332 | plx_sendword(PLX_FluidPressure); |
330 | PutCharSerial(&uc1, instance); |
333 | PutCharSerial(&uc1, obsIndex[PLX_FluidPressure]++); |
331 | plx_sendword((uint16_t)reading); |
334 | plx_sendword((uint16_t)reading); |
332 | } |
335 | } |
333 | 336 | ||
334 | void ProcessTiming(int instance) |
337 | void ProcessTiming(void) |
335 | { |
338 | { |
336 | plx_sendword(PLX_Timing); |
339 | plx_sendword(PLX_Timing); |
337 | PutCharSerial(&uc1, instance); |
340 | PutCharSerial(&uc1, obsIndex[PLX_Timing]++); |
338 | plx_sendword(64 - 15); // make it negative |
341 | plx_sendword(64 - 15); // make it negative |
339 | } |
342 | } |
340 | 343 | ||
341 | /* USER CODE END 0 */ |
344 | /* USER CODE END 0 */ |
342 | 345 | ||
Line 412... | Line 415... | ||
412 | int CalCounter = 0; |
415 | int CalCounter = 0; |
413 | 416 | ||
414 | PowerTempTimer = HAL_GetTick() + 1000; /* wait 10 seconds before powering up the CHT sensor */ |
417 | PowerTempTimer = HAL_GetTick() + 1000; /* wait 10 seconds before powering up the CHT sensor */ |
415 | 418 | ||
416 | ResetRxBuffer(&uc1); |
419 | ResetRxBuffer(&uc1); |
- | 420 | ||
- | 421 | PLX_SensorInfo info; ///< sensor info structure for getting idents |
|
- | 422 | ||
- | 423 | memset(obsIndex, 0, PLX_MAX_OBS); // zero incoming obsevation index |
|
- | 424 | int infoCount = -1; ///< counting bytes when we are copying them across |
|
417 | /* USER CODE END 2 */ |
425 | /* USER CODE END 2 */ |
418 | 426 | ||
419 | /* Infinite loop */ |
427 | /* Infinite loop */ |
420 | /* USER CODE BEGIN WHILE */ |
428 | /* USER CODE BEGIN WHILE */ |
421 | while (1) |
429 | while (1) |
Line 470... | Line 478... | ||
470 | PowerTempTimer = 0; |
478 | PowerTempTimer = 0; |
471 | } |
479 | } |
472 | } |
480 | } |
473 | 481 | ||
474 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
482 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
475 | int c; |
- | |
476 | char send = 0; |
483 | char send = 0; |
477 | 484 | ||
478 | // poll the input for a stop bit or timeout |
485 | // poll the input for a stop bit or timeout |
479 | if (PollSerial(&uc1)) |
486 | if (PollSerial(&uc1)) |
480 | { |
487 | { |
481 | resetSerialTimeout(); |
488 | resetSerialTimeout(); |
482 | c = GetCharSerial(&uc1); |
489 | uint8_t c = GetCharSerial(&uc1); |
- | 490 | // code added to read in all data, extract the observation index maximum for each type of data |
|
- | 491 | if (c == PLX_Start) |
|
- | 492 | { |
|
- | 493 | infoCount = 0; |
|
- | 494 | } |
|
- | 495 | else |
|
- | 496 | { |
|
- | 497 | info.bytes[infoCount++] = c; |
|
- | 498 | // process the sensor info field : discover maximum observation index |
|
- | 499 | if (infoCount == sizeof(PLX_SensorInfo)) |
|
- | 500 | { |
|
- | 501 | infoCount = 0; |
|
- | 502 | int addr = ConvPLXAddr(&info); |
|
- | 503 | if (addr < PLX_MAX_OBS && (obsIndex[addr] <= info.Instance)) |
|
- | 504 | obsIndex[addr] = info.Instance + 1; |
|
- | 505 | } |
|
- | 506 | } |
|
- | 507 | ||
483 | if (c != PLX_Stop) |
508 | if (c != PLX_Stop) |
484 | { |
509 | { |
485 | PutCharSerial(&uc1, c); // echo all but the stop bit |
510 | PutCharSerial(&uc1, c); // echo all but the stop bit |
486 | } |
511 | } |
487 | else |
512 | else |
Line 503... | Line 528... | ||
503 | if (send) |
528 | if (send) |
504 | { |
529 | { |
505 | send = 0; |
530 | send = 0; |
506 | 531 | ||
507 | // send the observations |
532 | // send the observations |
508 | ProcessRPM(0); |
533 | ProcessRPM(); |
509 | ProcessTemp(0, 0, PLX_X_CHT); |
534 | ProcessTemp(0, PLX_X_CHT); |
510 | ProcessTemp(1, 1, PLX_X_CHT); |
535 | ProcessTemp(1, PLX_X_CHT); |
511 | ProcessTemp(2, 0, PLX_AIT); |
536 | ProcessTemp(2, PLX_AIT); |
512 | ProcessTemp(3, 1, PLX_AIT); |
537 | ProcessTemp(3, PLX_AIT); |
513 | ProcessBatteryVoltage(0); // Batt 1 |
538 | ProcessBatteryVoltage(0); // Batt 1 |
514 | ProcessBatteryVoltage(1); // Batt 2 |
539 | ProcessBatteryVoltage(1); // Batt 2 |
515 | ProcessCPUTemperature(0); // built in temperature sensor |
540 | ProcessCPUTemperature(); // built in temperature sensor |
516 | 541 | ||
517 | ProcessMAP(0); |
542 | ProcessMAP(); |
518 | ProcessOilPress(0); |
543 | ProcessOilPress(); |
- | 544 | memset(obsIndex, 0, PLX_MAX_OBS); // zero incoming obsevation index |
|
519 | 545 | ||
520 | PutCharSerial(&uc1, PLX_Stop); |
546 | PutCharSerial(&uc1, PLX_Stop); |
521 | } |
547 | } |
522 | } |
548 | } |
523 | 549 |