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.
Fork of foc-ed_in_the_bot_compact by
Revision 7:caebf421f288, committed 2016-03-30
- Comitter:
- bwang
- Date:
- Wed Mar 30 06:50:11 2016 +0000
- Parent:
- 4:a6669248ce4d
- Child:
- 8:70122bad5f90
- Commit message:
- voltage mode emrax
Changed in this revision
--- a/PositionSensor/PositionSensor.cpp Fri Mar 18 12:07:14 2016 +0000
+++ b/PositionSensor/PositionSensor.cpp Wed Mar 30 06:50:11 2016 +0000
@@ -73,7 +73,7 @@
int raw = TIM2->CNT;
if (raw < 0) raw += _CPR;
if (raw >= _CPR) raw -= _CPR;
- float signed_elec = fmod((2.0f * (6.28318530718f * (raw) / (float)_CPR + _offset)), 6.28318530718f);
+ float signed_elec = fmod((10.0f * (6.28318530718f * (raw) / (float)_CPR + _offset)), 6.28318530718f);
if (signed_elec < 0) {
return signed_elec + 6.28318530718f;
} else {
@@ -84,7 +84,9 @@
void PositionSensorEncoder::ZeroEncoderCount(void){
if (ZSense->read() == 1){
if (ZSense->read() == 1){
+ ZTest->write(state);
TIM2->CNT=0;
+ state = !state;
}
}
}
\ No newline at end of file
--- a/config.h Fri Mar 18 12:07:14 2016 +0000 +++ b/config.h Wed Mar 30 06:50:11 2016 +0000 @@ -14,7 +14,7 @@ #define PI 3.141593f #define CPR 4096 -#define POS_OFFSET 5.3f +#define POS_OFFSET 4.8f #define I_SCALE_RAW 25.0f //mv/A #define R_UP 12000.0f //ohms @@ -25,8 +25,8 @@ #define I_OFFSET (AVDD * R_DOWN * R_UP / (R_DOWN * R_UP + R_BIAS * (R_DOWN + R_UP))) #define I_SCALE (R_BIAS * R_DOWN * I_SCALE_RAW / (R_DOWN * R_UP + R_BIAS * (R_DOWN + R_UP))) -#define K_LOOP 1.0f -#define KI_BASE 0.01f +#define K_LOOP 0.02f +#define KI_BASE 0.008f #define BUS_VOLTAGE 20.0f #define KP (K_LOOP / BUS_VOLTAGE)
--- a/main.cpp Fri Mar 18 12:07:14 2016 +0000
+++ b/main.cpp Wed Mar 30 06:50:11 2016 +0000
@@ -9,7 +9,7 @@
FastPWM *b;
FastPWM *c;
DigitalOut en(EN);
-DigitalOut toggle(PC_10);
+//DigitalOut toggle(PC_10);
PositionSensorEncoder pos(CPR, 0);
@@ -17,13 +17,13 @@
int state = 0;
int adval1, adval2;
-float ia, ib, ic, alpha, beta, d, q, vd, vq, p;
+float ia, ib, ic, alpha, beta, d, q, vd, vq, p = 0.0f;
float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV)
float d_integral = 0.0f, q_integral = 0.0f;
float last_d = 0.0f, last_q = 0.0f;
-float d_ref = -0.0f, q_ref = -50.0f;
+float d_ref = -0.0f, q_ref = 30.0f;
void commutate();
void zero_current();
@@ -32,11 +32,11 @@
extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
if (TIM1->SR & TIM_SR_UIF ) {
- toggle = 1;
+ //toggle = 1;
ADC1->CR2 |= 0x40000000;
volatile int delay;
for (delay = 0; delay < 35; delay++);
- toggle = 0;
+ //toggle = 0;
adval1 = ADC1->DR;
adval2 = ADC2->DR;
commutate();
@@ -80,7 +80,7 @@
TIM1->EGR |= TIM_EGR_UG;
TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock
- TIM1->ARR = 0x4650; //5 Khz
+ TIM1->ARR = 0x1770; //15 Khz
TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
TIM1->CR1 |= TIM_CR1_CEN;
@@ -143,12 +143,14 @@
void commutate() {
p = pos.GetElecPosition() - POS_OFFSET;
+
if (p < 0) p += 2 * PI;
+ if (p > 2 * PI) p -= 2 * PI;
float sin_p = sinf(p);
float cos_p = cosf(p);
- //float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
+ //float pos_dac = 0.8f * p / (2 * PI) + 0.1f;
//DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
@@ -183,11 +185,11 @@
if (vq < -1.0f) vq = -1.0f;
if (vq > 1.0f) vq = 1.0f;
- DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048);
+ //DAC->DHR12R2 = (unsigned int) (q * 20 + 2048);
//DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048);
//vd = 0.0f;
- //vq = -1.0f;
+ //vq = 1.0f;
float valpha = vd * cos_p - vq * sin_p;
float vbeta = vd * sin_p + vq * cos_p;
@@ -196,6 +198,8 @@
float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta;
float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta;
+ //DAC->DHR12R2 = (unsigned int) (-va * 1500 + 2048);
+
set_dtc(a, 0.5f + 0.5f * va);
set_dtc(b, 0.5f + 0.5f * vb);
set_dtc(c, 0.5f + 0.5f * vc);
@@ -206,6 +210,8 @@
startup_msg();
for (;;) {
+ //pc.printf("%f\n\r", p);
+ //wait_ms(100);
/*
q_ref = 0.0f;
wait(3);
