Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-dev-f303 FastPWM3
Revision 1:b8bceb4daed5, committed 2016-02-05
- Comitter:
- benkatz
- Date:
- Fri Feb 05 01:04:57 2016 +0000
- Parent:
- 0:4e1c4df6aabd
- Child:
- 2:8724412ad628
- Commit message:
- Added some comments, cleaned things up a bit
Changed in this revision
--- a/CurrentRegulator/CurrentRegulator.cpp Fri Feb 05 00:52:53 2016 +0000
+++ b/CurrentRegulator/CurrentRegulator.cpp Fri Feb 05 01:04:57 2016 +0000
@@ -24,7 +24,7 @@
I_C = 0;
I_Alpha = 0;
I_Beta = 0;
- count = 0;
+ //count = 0;
_Kp = Kp;
_Ki = Ki;
Q_Integral = 0;
@@ -44,12 +44,12 @@
_Inverter->GetCurrent(&I_A, &I_B, &I_C);
Clarke(I_A, I_B, &I_Alpha, &I_Beta);
Park(I_Alpha, I_Beta, theta_elec, &I_D, &I_Q);
- count += 1;
- if(count > 10000) {
- count=0;
+ //count += 1;
+ //if(count > 10000) {
+ // count=0;
// printf("I_A: %f I_C: %f I_C: %f\n\r", I_A, I_B, I_C);
- IQ_Ref = -IQ_Ref;
- }
+ //IQ_Ref = -IQ_Ref;
+ // }
DAC->DHR12R1 = (int) (I_Q*490.648f) + 2048;
//DAC->DHR12R1 = (int) (I_Alpha*4096.0f) + 2048;
@@ -79,8 +79,7 @@
void CurrentRegulator::Commutate(){
theta_elec = _PositionSensor->GetElecPosition();
- SampleCurrent();
- //Run control loop
- Update();
- SetVoltage();
+ SampleCurrent(); //Grab most recent current sample
+ Update(); //Run control loop
+ SetVoltage(); //Set inverter duty cycles
}
\ No newline at end of file
--- a/FOC/FOC.cpp Fri Feb 05 00:52:53 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -#include "FOC.h" -
--- a/Inverter/Inverter.cpp Fri Feb 05 00:52:53 2016 +0000
+++ b/Inverter/Inverter.cpp Fri Feb 05 01:04:57 2016 +0000
@@ -8,8 +8,6 @@
Enable = new DigitalOut(PinEnable);
- //Current_B = new AnalogIn(BSense);
- //Current_C = new AnalogIn(CSense);
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // enable the clock to GPIOA
RCC->APB1ENR |= 0x00000001; // enable TIM2 clock
@@ -53,7 +51,7 @@
// DAC set-up
RCC->APB1ENR |= 0x20000000; // Enable clock for DAC
DAC->CR |= 0x00000001; // DAC control reg, both channels ON
- GPIOA->MODER |= 0x00000300; // PA04 as analog outputs
+ GPIOA->MODER |= 0x00000300; // PA04 as analog output
EnableInverter();
SetDTC(0.0f, 0.0f, 0.0f);
@@ -96,17 +94,13 @@
void Inverter::SampleCurrent(void){
// Dbg->write(1);
- GPIOC->ODR ^= (1 << 4);
+ GPIOC->ODR ^= (1 << 4); //Toggle pin for debugging
I_B = _I_Scale*((float) (ADC1->DR) - I_B_Offset);
I_C = _I_Scale*((float) (ADC2->DR)- I_C_Offset);
I_A = -I_B - I_C;
//DAC->DHR12R1 = ADC2->DR;
//DAC->DHR12R1 = TIM3->CNT>>2;//ADC2->DR; // pass ADC -> DAC, also clears EOC flag
ADC1->CR2 |= 0x40000000;
-
- //I_B = Current_B->read()*_I_Scale;
- //I_C = Current_C->read()*_I_Scale;
- GPIOC->ODR ^= (1 << 4);
- // Dbg->write(0);
+ GPIOC->ODR ^= (1 << 4); //toggle pin for debugging
}
--- a/PositionSensor/PositionSensor.cpp Fri Feb 05 00:52:53 2016 +0000
+++ b/PositionSensor/PositionSensor.cpp Fri Feb 05 01:04:57 2016 +0000
@@ -25,7 +25,7 @@
TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1
TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register
- TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1
+ TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1, maximum digital filtering
TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2
TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register
TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler
@@ -38,12 +38,10 @@
ZPulse->enable_irq();
ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount);
ZPulse->mode(PullDown);
- //TIM5->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1
- ZTest = new DigitalOut(PC_2);
- ZTest->write(1);
- //int state = 0;
+ //ZTest = new DigitalOut(PC_2);
+ //ZTest->write(1);
}
@@ -73,8 +71,8 @@
if (ZSense->read() == 1){
if (ZSense->read() == 1){
TIM3->CNT=0x8000;
- state = !state;
- ZTest->write(state);
+ //state = !state;
+ //ZTest->write(state);
}
}
}
\ No newline at end of file
--- a/PositionSensor/PositionSensor.h Fri Feb 05 00:52:53 2016 +0000
+++ b/PositionSensor/PositionSensor.h Fri Feb 05 01:04:57 2016 +0000
@@ -16,10 +16,10 @@
private:
InterruptIn *ZPulse;
DigitalIn *ZSense;
- DigitalOut *ZTest;
+ //DigitalOut *ZTest;
virtual void ZeroEncoderCount(void);
int _CPR;
- int state;
+ //int state;
float _offset;
};
--- a/Transforms/Transforms.cpp Fri Feb 05 00:52:53 2016 +0000
+++ b/Transforms/Transforms.cpp Fri Feb 05 01:04:57 2016 +0000
@@ -7,8 +7,6 @@
void Transforms::Park(float alpha, float beta, float theta, float *d, float *q){
float cosine = cos(theta);
float sine = sin(theta);
- //float a = sine;
- //float b = cosine;
*d = alpha*cosine - beta*sine;
*q = -beta*cosine - alpha*sine;
//DAC->DHR12R1 = (int) (*q*49.648f) + 2048;
--- a/main.cpp Fri Feb 05 00:52:53 2016 +0000
+++ b/main.cpp Fri Feb 05 01:04:57 2016 +0000
@@ -8,29 +8,16 @@
PositionSensorEncoder encoder(8192,0);
Inverter inverter(PA_5, PB_10, PB_3, PB_7, 0.02014160156, 0.00005);
-
CurrentRegulator foc(&inverter, &encoder, .005, .5);
Ticker testing;
-//SPWM spwm(&inverter, 2.0);
-
-//DigitalOut Dbg_pin(PA_15);
using namespace FastMath;
using namespace Transforms;
-float offset = 0;//-0.24;
-/*
-float v_alpha = 0;
-float v_beta = 0;
+//float offset = 0;//-0.24;
-float i_d = 0;
-float i_q = 1;
-float v_d = 0;
-float v_q = -.2;
-float f;
-float theta;
- */
+// Current Sampling IRQ
extern "C" void TIM2_IRQHandler(void) {
// flash on update event
if (TIM2->SR & TIM_SR_UIF & TIM2->CNT>0x465) {
@@ -39,14 +26,6 @@
TIM2->SR = 0x0; // reset the status register
}
-/*
-void PrintEncoder(void){
- printf("%f\n\r", encoder.GetElecPosition());
- //printf("%f\n\r", encoder.GetMechPosition());
-
- //printf("%d\n\r", TIM3->CNT-0x8000);
- }
-*/
void Loop(void){
foc.Commutate();
@@ -59,20 +38,12 @@
//output.write(theta/6.28318530718f);
}
*/
-/*
-void open_loop(void){
- f = t.read();
- v_alpha = 0.1f*sin(10.0f*f);
- v_beta = 0.1f*cos(10.0f*f);
- spwm.Update_DTC(v_alpha, v_beta);
- }
- */
+
int main() {
wait(1);
testing.attach(&Loop, .0001);
-
//inverter.EnableInverter();
while(1) {