integrate pid
2nd and 3rd channel can be coupled via percentage
This commit is contained in:
135
main.c
135
main.c
@@ -14,18 +14,34 @@
|
||||
#include "modbus.h"
|
||||
#include "menu.h"
|
||||
#include "adc.h"
|
||||
#include "pid.h"
|
||||
|
||||
#define COIL1 1<<1
|
||||
#define COIL2 1<<0
|
||||
#define COIL3 1<<2
|
||||
#define COIL4 1<<3
|
||||
|
||||
volatile uint16_t holdingRegisters[10];
|
||||
|
||||
volatile uint16_t temp_values[4];
|
||||
volatile uint16_t temp_setpoints[4];
|
||||
|
||||
volatile uint16_t output;
|
||||
volatile struct pid controller;
|
||||
|
||||
volatile uint32_t duty[3];
|
||||
|
||||
void modbusGet(void) {
|
||||
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||
{
|
||||
switch(rxbuffer[1]) {
|
||||
case fcReadHoldingRegisters:
|
||||
holdingRegisters[0] = menu_state;
|
||||
holdingRegisters[0] = OCR1A;
|
||||
holdingRegisters[1] = duty[0];
|
||||
holdingRegisters[2] = duty[1];
|
||||
holdingRegisters[3] = duty[2];
|
||||
modbusExchangeRegisters(holdingRegisters,0,10);
|
||||
break;
|
||||
case fcPresetSingleRegister:
|
||||
case fcPresetMultipleRegisters:
|
||||
modbusExchangeRegisters(holdingRegisters,0,4);
|
||||
@@ -38,7 +54,7 @@ void modbusGet(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void do_buttons(){
|
||||
void read_buttons(){
|
||||
i2c_start(0x71);
|
||||
uint8_t buttons = i2c_readNak();
|
||||
i2c_stop();
|
||||
@@ -58,29 +74,39 @@ void do_buttons(){
|
||||
|
||||
int main(void)
|
||||
{
|
||||
DDRB |= 0x0F; // output coils
|
||||
DDRD |= (1 << 4); // board LED
|
||||
|
||||
i2c_init();
|
||||
lcd_init();
|
||||
adc_init();
|
||||
enc_init();
|
||||
|
||||
lcd_clear();
|
||||
init_pid(&controller, 3, 0.13, 0);
|
||||
|
||||
sei();
|
||||
modbusSetAddress(1);
|
||||
modbusInit();
|
||||
|
||||
TCCR0B|=(1<<CS01); //prescaler 8
|
||||
//modbus timer
|
||||
TCCR0B|=(1<<CS01);
|
||||
TIMSK0|=(1<<TOIE0);
|
||||
|
||||
PORTD |= (1<<6) | (1 << 7);
|
||||
PCICR |= (1<<PCIE2);
|
||||
PCMSK2 |= (1<<PCINT23);
|
||||
// timer for regulator
|
||||
TCCR1B|=(1<<CS12) | (1<<CS10);
|
||||
TIMSK1|=(1<<TOIE1)|(1<<OCIE1A);
|
||||
|
||||
sei();
|
||||
|
||||
lcd_clear();
|
||||
draw_menu();
|
||||
|
||||
|
||||
uint16_t x = 0;
|
||||
while(1){
|
||||
modbusGet();
|
||||
do_buttons();
|
||||
read_buttons();
|
||||
|
||||
//update lcd every once in a while
|
||||
if(x==0){
|
||||
write_temps();
|
||||
write_setpoints();
|
||||
@@ -92,24 +118,87 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
ISR(PCINT2_vect){
|
||||
encoder_isr();
|
||||
}
|
||||
|
||||
ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
|
||||
modbusTickTimer();
|
||||
}
|
||||
|
||||
ISR(PCINT2_vect){
|
||||
//TODO good quadrature reading code
|
||||
if((PIND & (1<<7)) && (menu_state >= 1) && (menu_state <= 3)){
|
||||
if(PIND & (1<<6))
|
||||
temp_setpoints[menu_state-1]--;
|
||||
else
|
||||
temp_setpoints[menu_state-1]++;
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
/* turn off outputs */
|
||||
|
||||
if(OCR1A >= duty[0]){
|
||||
PORTB &= ~(COIL1);
|
||||
PORTD &= ~(1 << 4);
|
||||
}
|
||||
if(OCR1A >= duty[1])
|
||||
PORTB &= ~(COIL2);
|
||||
if(OCR1A >= duty[2])
|
||||
PORTB &= ~(COIL3);
|
||||
|
||||
uint16_t new_ocr = 0xFFFE;
|
||||
for(uint8_t i = 0; i < 3; i++){
|
||||
if((duty[i] > OCR1A) && (duty[i] < new_ocr))
|
||||
new_ocr = duty[i];
|
||||
}
|
||||
OCR1A = new_ocr;
|
||||
|
||||
//uint16_t tmp1 = duty[0];
|
||||
//uint16_t tmp2;
|
||||
//for(uint8_t i = 0; i < 3; i++){
|
||||
// if(duty[i] < tmp1 && duty[i] > OCR1A)
|
||||
// tmp2 = duty[i];
|
||||
//}
|
||||
//OCR1A = tmp2;
|
||||
}
|
||||
|
||||
ISR(TIMER1_OVF_vect) {
|
||||
int16_t tmp_pid = pid_step(&controller, 1, temp_setpoints[0] - temp_values[0]);
|
||||
|
||||
if(tmp_pid>=0)
|
||||
output = tmp_pid;
|
||||
else
|
||||
output = 0;
|
||||
|
||||
|
||||
if(output >= 128){
|
||||
output = 128;
|
||||
duty[0]=0xFFFE;
|
||||
}
|
||||
else if(output >= 1){
|
||||
uint32_t tmp = output * 512;
|
||||
duty[0] = tmp;
|
||||
}
|
||||
else
|
||||
duty[0]=0;
|
||||
|
||||
|
||||
duty[1] = duty[0] / 100 * temp_setpoints[1];
|
||||
duty[2] = duty[0] / 100 * temp_setpoints[2];
|
||||
|
||||
for(uint8_t i = 0; i < 3; i++){
|
||||
if(duty[i] >= 0xFFFE)
|
||||
duty[i] = 0xFFFE;
|
||||
}
|
||||
|
||||
//if(PIND & (1<<7))
|
||||
// if(PIND & (1<<6)){
|
||||
// if(temp_setpoints[menu_state] > 0)
|
||||
// }
|
||||
// else{
|
||||
// if(temp_setpoints[menu_state] < 300)
|
||||
// }
|
||||
OCR1A = 0xFFFE;
|
||||
for(uint8_t i = 0; i < 3; i++){
|
||||
if(duty[i] < OCR1A && duty[i] >= (uint16_t)1000)
|
||||
OCR1A = duty[i];
|
||||
}
|
||||
|
||||
if(duty[0] >= 1000){
|
||||
PORTB |= COIL1;
|
||||
PORTD |= 1<<4;
|
||||
}
|
||||
if(duty[1] >= 1000)
|
||||
PORTB |= COIL2;
|
||||
|
||||
if(duty[2] >= 1000)
|
||||
PORTB |= COIL3;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
2
makefile
2
makefile
@@ -1,5 +1,5 @@
|
||||
TARGET = main
|
||||
FILES = main i2c lcd modbus menu adc
|
||||
FILES = main i2c lcd modbus menu adc pid
|
||||
MCU = atmega328p
|
||||
PROGC = m328p
|
||||
CC = avr-gcc
|
||||
|
||||
41
menu.c
41
menu.c
@@ -19,15 +19,15 @@ void write_heater_set_temp(uint8_t n, uint16_t temp){
|
||||
void update_cursor(){
|
||||
switch(menu_state){
|
||||
case 1:
|
||||
lcd_set_position(2,6);
|
||||
lcd_set_position(2,8);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
case 2:
|
||||
lcd_set_position(2,11);
|
||||
lcd_set_position(2,13);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
case 3:
|
||||
lcd_set_position(2,16);
|
||||
lcd_set_position(2,18);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
default:
|
||||
@@ -53,14 +53,17 @@ void write_temps(){
|
||||
|
||||
lcd_set_position(3, 6);
|
||||
sprintf(str, "%3i", temp_values[0]);
|
||||
str[3] = 0xDF;
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(3, 11);
|
||||
sprintf(str, "%3i", temp_values[1]);
|
||||
str[3] = 0xDF;
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(3, 16);
|
||||
sprintf(str, "%3d", temp_values[2]);
|
||||
str[3] = 0xDF;
|
||||
lcd_print_str(str);
|
||||
}
|
||||
|
||||
@@ -69,14 +72,15 @@ void write_setpoints(){
|
||||
|
||||
lcd_set_position(2, 6);
|
||||
sprintf(str, "%3i", temp_setpoints[0]);
|
||||
str[3] = 0xDF;
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(2, 11);
|
||||
sprintf(str, "%3i", temp_setpoints[1]);
|
||||
sprintf(str, "%3i%%", temp_setpoints[1]);
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(2, 16);
|
||||
sprintf(str, "%3i", temp_setpoints[2]);
|
||||
sprintf(str, "%3i%%", temp_setpoints[2]);
|
||||
lcd_print_str(str);
|
||||
}
|
||||
|
||||
@@ -84,6 +88,31 @@ void set_item(uint8_t page_num){
|
||||
//if(menu_state=page_num)
|
||||
// menu_state=0;
|
||||
//else
|
||||
menu_state = page_num;
|
||||
menu_state = page_num;
|
||||
}
|
||||
|
||||
void enc_init(){
|
||||
PORTD |= (1<<6) | (1 << 7);
|
||||
PCICR |= (1<<PCIE2);
|
||||
PCMSK2 |= (1<<PCINT23);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void encoder_isr(){
|
||||
//TODO good quadrature reading code
|
||||
if((PIND & (1<<7)) && (menu_state >= 1) && (menu_state <= 3)){
|
||||
if(PIND & (1<<6))
|
||||
temp_setpoints[menu_state-1]--;
|
||||
else
|
||||
temp_setpoints[menu_state-1]++;
|
||||
}
|
||||
|
||||
//if(PIND & (1<<7))
|
||||
// if(PIND & (1<<6)){
|
||||
// if(temp_setpoints[menu_state] > 0)
|
||||
// }
|
||||
// else{
|
||||
// if(temp_setpoints[menu_state] < 300)
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user