Prius IPM controller

Dependencies:   mbed

Fork of analoghalls5_5 by N K

Committer:
nki
Date:
Sat Mar 14 18:51:36 2015 +0000
Revision:
27:846c08fb3697
Parent:
26:d00561c7bf43
Child:
29:cb03760ba9ea
NONWORKING: debugging native ADC reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang 0:54cf32d35f4d 1 #include "includes.h"
bwang 11:dccbaa9274c5 2 #include "transforms.h"
bwang 11:dccbaa9274c5 3 #include "filters.h"
bwang 11:dccbaa9274c5 4 #include "context.h"
bwang 1:1f58bdcf2956 5 #include "core.h"
bwang 1:1f58bdcf2956 6 #include "meta.h"
bwang 11:dccbaa9274c5 7 #include "sensors.h"
bwang 11:dccbaa9274c5 8 #include "callbacks.h"
nki 4:fdadf4a3577a 9
nki 27:846c08fb3697 10 //NVIC_SetVector(ADC_IRQn, (uint32_t)ADC_IRQHandler);
nki 27:846c08fb3697 11 //NVIC->IntSelect &= ~(1 << USB_IRQn);
nki 27:846c08fb3697 12 //NVIC_EnableIRQ(TIM2_CC_IRQn);
nki 27:846c08fb3697 13
nki 27:846c08fb3697 14
nki 27:846c08fb3697 15
nki 27:846c08fb3697 16 //NVIC_SetPriority(0); //(uint32_t)SPI_IRQn
nki 27:846c08fb3697 17 //NVIC_EnableIRQ((uint32_t)SPI_IRQn);
nki 27:846c08fb3697 18
nki 27:846c08fb3697 19 int current1, current2;
nki 27:846c08fb3697 20
nki 27:846c08fb3697 21 extern "C" void TIM2_Derpy_Handler()
nki 27:846c08fb3697 22 {
nki 27:846c08fb3697 23 toggler = !toggler;
nki 27:846c08fb3697 24
nki 27:846c08fb3697 25 ADC1->SQR3 = 0; //clear the conversion sequence register
nki 27:846c08fb3697 26 ADC1->SQR3 |= 1; //set the first bit high (read PA_1 a.k.a ADC1_1 a.k.a. A1
nki 27:846c08fb3697 27 ADC1->CR2 |= (1 << 30); //(SWSTART = 1) start the conversion
nki 27:846c08fb3697 28 while ((ADC1->SR & 2) == 0){} // while bit 1 of ADC_SR is 0 (not done converting yet), do nothing.
nki 27:846c08fb3697 29 current1 = ADC1->DR;
nki 27:846c08fb3697 30
nki 27:846c08fb3697 31 ADC1->SQR3 |= 4; //set the first bit high (read PA_4 a.k.a. ADC1_4 a.k.a A2
nki 27:846c08fb3697 32 ADC1->CR2 |= (1 << 30); // (SWSTART = 1) start the conversion
nki 27:846c08fb3697 33 while ((ADC1->SR & 2) == 0){} // while bit 1 of ADC_SR is 0 (not done converting yet), do nothing.
nki 27:846c08fb3697 34 current2 = ADC1->DR;
nki 27:846c08fb3697 35
nki 27:846c08fb3697 36
nki 27:846c08fb3697 37
nki 27:846c08fb3697 38 TIM2->SR &= ~1; //clear the LSB, that is the UIF - update interrupt flag. The flag is set high each time tim_2 overflows and underflows. we reset it here.
nki 27:846c08fb3697 39 toggler = !toggler;
nki 27:846c08fb3697 40 }
nki 27:846c08fb3697 41
nki 27:846c08fb3697 42
nki 27:846c08fb3697 43
nki 27:846c08fb3697 44
nki 27:846c08fb3697 45 DigitalOut toggler(D7); //to check loop timing
nki 27:846c08fb3697 46
bwang 11:dccbaa9274c5 47 int main() {
nki 27:846c08fb3697 48
nki 27:846c08fb3697 49 /*
nki 27:846c08fb3697 50 ADC_SQR1 |= (1 << 20); //we're doing 2 conversions. (p.231)
nki 27:846c08fb3697 51 ADC_CR2 |= (1 << 30); //start conversion mode (p.227)
nki 27:846c08fb3697 52 */
nki 27:846c08fb3697 53 NVIC_EnableIRQ(TIM2_IRQn);
nki 27:846c08fb3697 54 NVIC_SetVector(TIM2_IRQn, (uint32_t)TIM2_Derpy_Handler); //just calling it this so it doesn't blend in with the other cryptic register-looking names.
nki 27:846c08fb3697 55
nki 27:846c08fb3697 56
bwang 11:dccbaa9274c5 57 Context *context = new Context();
bwang 11:dccbaa9274c5 58 context->ConfigureOutputs(D6, D13, D3, D8);
nki 27:846c08fb3697 59 context->ConfigureCurrentSensors(A1, A2, 0.01f, 0.7f); // I guess this configures the ADCs or something
nki 27:846c08fb3697 60 context->ConfigureIdPidController(0.0000f, 0.0f, 0.0f, 5.0f, -5.0f);
nki 27:846c08fb3697 61 context->ConfigureIqPidController(0.0001f, 0.1f, 0.0f, 5.0f, -5.0f);
bwang 11:dccbaa9274c5 62 context->ConfigureThrottle(A0, 0.8f, 3.0f);
nki 27:846c08fb3697 63 context->ConfigurePositionSensor(A4, A5, 0.249f, 0.497f, 0.231f, 0.499f, 205.0f);
bwang 24:f1ff9c7256b5 64 context->ConfigureReference(5.0f);
bwang 24:f1ff9c7256b5 65 context->ConfigureDebugger(2, 1000);
bwang 11:dccbaa9274c5 66 context->AttachCallBack(&fast, 5000);
bwang 11:dccbaa9274c5 67 context->AttachCallBack(&slow, 10);
bwang 11:dccbaa9274c5 68 context->AttachCallBack(&debug, 10);
bwang 24:f1ff9c7256b5 69 context->AttachCallBack(&log, 500);
nki 27:846c08fb3697 70
bwang 11:dccbaa9274c5 71 context->Start();
bwang 0:54cf32d35f4d 72 }