This commit is contained in:
hg 2015-02-15 18:27:54 +01:00
parent c757788aee
commit 8820ac6782
5 changed files with 120 additions and 22 deletions

View File

@ -1,11 +1,11 @@
#include "TeensyPwm.h"
#include <stdint.h>
#include <LiquidCrystal.h>
#include "hardware.h"
#include "rotary.h"
#include "pwm.h"
#include "display.h"
@ -13,11 +13,11 @@ void setup() {
Serial.begin(115200);
Serial.println("Teensy SMPS");
pwmInit();
pwmInit();
rotaryInit();
displayInit();
displayInit();
}
void loop() {
displayLoop();
displayExec();
}

View File

@ -16,6 +16,12 @@ class Control {
public:
Control(float p_rMin, float p_rMax, float p_kP, float p_kI, float p_kD);
float cycle(float vDes, float vCur) volatile;
float getP() volatile { return m_kP; };
void setP(float p) volatile { m_kP = p; };
float getI() volatile { return m_kI; };
void setI(float i) volatile { m_kI = i; };
float getD() volatile { return m_kD; };
void setD(float d) volatile { m_kD = d; };
private:
float m_rOld;
float m_eOld;

View File

@ -1,5 +1,7 @@
#include <Arduino.h>
#include <stdint.h>
#include <LiquidCrystal.h>
#include "hardware.h"
#include "display.h"
@ -12,29 +14,88 @@ const uint32_t DISPLAY_UPDATE_TIME = 5e5; // microseconds
void displayInit() {
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("Teensy SMPS");
}
void displayLoop() {
static uint32_t lastDisplayCycle = 0;
void displayExec() {
static uint8_t state = 0;
static uint32_t lastDisplayCycle = 0;
uint32_t currentTime = micros();
if ((lastDisplayCycle + DISPLAY_UPDATE_TIME <= currentTime) || (lastDisplayCycle > currentTime)) {
lastDisplayCycle = currentTime;
int rotaryCount = getAndResetRotaryCount();
if (rotaryCount != 0) {
setUDes(getUDes() + rotaryCount);
}
bool switchState = getAndResetSwitchState();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(getUdes());
lcd.setCursor(8, 0);
lcd.print(getUCur());
lcd.setCursor(0, 1);
lcd.print(geDutyCycle());
lcd.home();
lcd.print(getUDes(), 0);
lcd.print("V ");
lcd.print(getUCur(), 0);
lcd.print("V ");
lcd.print(getDutyCycle(), 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(getP(), 1);
lcd.print(" ");
lcd.print(getI(), 1);
lcd.print(" ");
lcd.print(getD(), 1);
lcd.print(" ");
switch (state) {
case 0:
lcd.setCursor(15, 0);
lcd.print(" ");
if (switchState) {
state = 1;
}
break;
case 1:
if (switchState) {
state = 2;
}
lcd.setCursor(15, 0);
lcd.print("U");
if (rotaryCount != 0) {
setUDes(getUDes() + rotaryCount);
}
break;
case 2:
if (switchState) {
state = 3;
}
lcd.setCursor(15, 0);
lcd.print("P");
if (rotaryCount != 0) {
setP(getP() + 0.1 * rotaryCount);
}
break;
case 3:
if (switchState) {
state = 4;
}
lcd.setCursor(15, 0);
lcd.print("I");
if (rotaryCount != 0) {
setI(getI() + 0.1 * rotaryCount);
}
break;
case 4:
if (switchState) {
state = 5;
}
lcd.setCursor(15, 0);
lcd.print("D");
if (rotaryCount != 0) {
setD(getD() + 0.1 * rotaryCount);
}
break;
default:
state = 0;
break;
}
}
}
}

32
pwm.cpp
View File

@ -17,8 +17,32 @@ volatile float u_des = 0;
volatile float u_curr = 0;
volatile uint16_t newPwm = 0;
float getP() {
return ctrl.getP();
}
void setP(float p) {
ctrl.setP(p);
}
float getI() {
return ctrl.getI();
}
void setI(float i) {
ctrl.setI(i);
}
float getD() {
return ctrl.getD();
}
void setD(float d) {
ctrl.setD(d);
}
float getDutyCycle() {
nointerrupts();
noInterrupts();
uint16_t my_newPwm = newPwm;
interrupts();
float dutyCycle = ((float)my_newPwm) / ((float)PWM_MAX) * 100.0;
@ -26,14 +50,14 @@ float getDutyCycle() {
}
float getUCur() {
nointerrupts();
noInterrupts();
float my_u_curr = u_curr;
interrupts();
return my_u_curr;
}
float getUDes() {
nointerrupts();
noInterrupts();
float my_u_des = u_des;
interrupts();
return my_u_des;
@ -43,7 +67,7 @@ void setUDes(float uDes) {
if (uDes < 0) {
uDes= 0;
}
nointerrupts();
noInterrupts();
u_des = uDes;
interrupts();
}

9
pwm.h
View File

@ -1,11 +1,18 @@
#ifndef PWM_H_
#define PWM_H_
#include "control.h"
void pwmInit();
float getDutyCycle();
float getUCur();
float getUDes();
void setUDes(float uDes);
float getP();
void setP(float p);
float getI();
void setI(float i);
float getD();
void setD(float d);
#endif /* PWM_H_ */