send less mqtt msgs, improve performance

master
Eggert Jung 3 years ago
parent cf5cdfc79f
commit 1ad6f56a9d

@ -15,14 +15,14 @@ void timer3_init()
} }
void send_info(void); void send_settings(void);
void do_abzug(){ void do_abzug(){
if (read_Input(BTN_ABZUG_EIN, RISING)) { if (read_Input(BTN_ABZUG_EIN, RISING)) {
TCCR3B |= _BV(CS31); TCCR3B |= _BV(CS31);
set_Output(LED_ABZUG, ON); set_Output(LED_ABZUG, ON);
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }
@ -30,7 +30,7 @@ void do_abzug(){
TCCR3B &= ~(_BV(CS31)); TCCR3B &= ~(_BV(CS31));
set_Output(LED_ABZUG, OFF); set_Output(LED_ABZUG, OFF);
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }
@ -40,7 +40,7 @@ void do_abzug(){
else else
abzug_speed = 1000; abzug_speed = 1000;
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }
@ -50,7 +50,7 @@ void do_abzug(){
else else
abzug_speed = 10; abzug_speed = 10;
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }
@ -60,7 +60,7 @@ void do_abzug(){
else else
abzug_speed = 1000; abzug_speed = 1000;
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }
@ -70,7 +70,7 @@ void do_abzug(){
else else
abzug_speed = 10; abzug_speed = 10;
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
send_info(); send_settings();
#endif #endif
} }

@ -36,7 +36,7 @@ void do_kraftsensor(){
readReg(1,0,2); readReg(1,0,2);
if(wait_receive(2, m_data, 100)){ if(wait_receive(2, m_data, 100)){
kraftsensor_valid = 0; kraftsensor_valid = 0;
printf("modbus error\n\r"); //printf("modbus error\n\r");
} }
else{ else{
kraftsensor_valid = 1; kraftsensor_valid = 1;
@ -50,12 +50,12 @@ void do_kraftsensor(){
if(abs(kraftsensor_read - old_value) > 10000){ if(abs(kraftsensor_read - old_value) > 10000){
if(abs(last_read - kraftsensor_read) > 10000){ if(abs(last_read - kraftsensor_read) > 10000){
kraftsensor_value = old_value; kraftsensor_value = old_value;
printf("delta: %d\tvalue:%ld\n", kraftsensor_read - old_value, kraftsensor_read); //printf("delta: %ld\tvalue:%ld\n", kraftsensor_read - old_value, kraftsensor_read);
printf("spike\n"); //printf("spike\n");
} }
else{ else{
kraftsensor_value = kraftsensor_read; kraftsensor_value = kraftsensor_read;
printf("jump\n"); //printf("jump\n");
} }
} }
else{ else{
@ -117,5 +117,7 @@ void writeReg(uint8_t slaveid, uint16_t address, uint16_t value) {
} }
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
//PORTH &= ~(1<<5);
modbusTickTimer(); modbusTickTimer();
//PORTH |= (1<<5);
} }

@ -1,6 +1,7 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/wdt.h> #include <avr/wdt.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -71,19 +72,10 @@ static void avr_init()
return; return;
} }
void send_info(void){ // send processvalues that are constantly changing
void send_values(void){
char msg[10]; char msg[10];
/* Abzug */
sprintf(msg, "%d", abzug_speed);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/speed", msg, strlen(msg));
if(TCCR3B & (1<<CS31))
sprintf(msg, "True");
else
sprintf(msg, "False");
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/onoff", msg, strlen(msg));
/* Taenzer */ /* Taenzer */
if(kraftsensor_valid) if(kraftsensor_valid)
ltoa(kraftsensor_value, msg, 10); ltoa(kraftsensor_value, msg, 10);
@ -95,23 +87,41 @@ void send_info(void){
ltoa(temp, msg, 10); ltoa(temp, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/taenzer/pos", msg, strlen(msg)); mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/taenzer/pos", msg, strlen(msg));
itoa((250*60)/ICR5, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/spule/speed", msg, strlen(msg));
}
// send settings wich only change on buttion press
void send_settings(void){
//TODO only send on change or improve performance otherwise
char msg[10];
//PORTH &= ~(1<<5);
//PORTH |= (1<<5);
/* Abzug */
sprintf(msg, "%d", abzug_speed);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/speed", msg, strlen(msg));
if(TCCR3B & (1<<CS31))
sprintf(msg, "True");
else
sprintf(msg, "False");
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/onoff", msg, strlen(msg));
/* Taenzer */
itoa(taenzer_state.force_setpoint, msg, 10); itoa(taenzer_state.force_setpoint, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/taenzer/sp_kraft", msg, strlen(msg)); mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/taenzer/sp_kraft", msg, strlen(msg));
/* Spule */ /* Spule */
itoa((250*60)/ICR5, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/spule/speed", msg, strlen(msg));
if(TCCR5B & (1<<CS31)) if(TCCR5B & (1<<CS31))
sprintf(msg, "True"); sprintf(msg, "True");
else else
sprintf(msg, "False"); sprintf(msg, "False");
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/spule/onoff", msg, strlen(msg)); mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/spule/onoff", msg, strlen(msg));
/* Uptime */ ///* Uptime */
sprintf(msg, "%ld", millis()/1000); //sprintf(msg, "%ld", millis()/1000);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/uptime", msg, strlen(msg)); //mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/uptime", msg, strlen(msg));
} }
@ -186,13 +196,16 @@ int main()
set_Output(LED_FEHLER, OFF); set_Output(LED_FEHLER, OFF);
set_Output(BitPH5, ON);
while(1) while(1)
{ {
wdt_reset(); // WDT reset at least every sec wdt_reset(); // WDT reset at least every sec
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
if(millis() < 10) if(millis() < 10)
send_info(); send_settings();
#endif #endif
ioHelperReadPins(); ioHelperReadPins();
@ -206,6 +219,7 @@ int main()
outStates[2] ^= outStatesBlinking[2]; outStates[2] ^= outStatesBlinking[2];
outStates[3] ^= outStatesBlinking[3]; outStates[3] ^= outStatesBlinking[3];
timer_blink_outs = millis(); timer_blink_outs = millis();
//printf("icr5: %u\n", ICR5);
} }
if(millis() - timer_modbus_poll > 20){ if(millis() - timer_modbus_poll > 20){
@ -217,7 +231,8 @@ int main()
// send misc info // send misc info
if(millis() - timer_send_info > 200){ if(millis() - timer_send_info > 200){
timer_send_info += 200; timer_send_info += 200;
send_info(); send_values();
//send_info(); // 27ms every 200ms
} }
#endif #endif
@ -229,10 +244,10 @@ int main()
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
ioHelperSetBit(outStates, LED_BUS_OK, 1); ioHelperSetBit(outStates, LED_BUS_OK, 1);
ioHelperSetOuts(); ioHelperSetOuts();
MQTTYield(&mqtt_client, 10); //blocking call MQTTYield(&mqtt_client, 1); //blocking call
ioHelperSetBit(outStates, LED_BUS_OK, 0); ioHelperSetBit(outStates, LED_BUS_OK, 0);
#endif #endif
ioHelperSetOuts(); ioHelperSetOuts(); //40us
} }
return 0; return 0;
} }

@ -25,7 +25,7 @@ typedef struct {
} PID_vars; } PID_vars;
#define PID_VARS_INIT(x) PID_vars x = {.Kp=1.3,.Ki=0.00,.Kd=0.0,.output_max=20000.0, \ #define PID_VARS_INIT(x) PID_vars x = {.Kp=1.5,.Ki=0.00,.Kd=0.0,.output_max=20000.0, \
.output_min=-20000.0,._integral_sum=0.0,._prev_err=0.0,._dt=1.0} .output_min=-20000.0,._integral_sum=0.0,._prev_err=0.0,._dt=1.0}
/* Function Prototypes */ /* Function Prototypes */

@ -39,8 +39,19 @@ void timer5_init()
TIMSK5 |= 1<<TOIE5; TIMSK5 |= 1<<TOIE5;
} }
void do_spule(){ static void spule_onoff(uint8_t state){
if(state){
TCCR5B |= _BV(CS51); // ROTATION
TCCR1B |= _BV(CS11); // TRANSLATION
}
else{
TCCR5B &= ~(_BV(CS51));
TCCR1B &= ~(_BV(CS11));
}
}
void do_spule(){
//PORTH &= ~(1<<5);
if(read_Input(IN_SPULE_HOME, LEVEL) && spule_trans_homed == 0){ if(read_Input(IN_SPULE_HOME, LEVEL) && spule_trans_homed == 0){
spule_trans_homed = 1; spule_trans_homed = 1;
spule_trans_pos = 0; spule_trans_pos = 0;
@ -58,26 +69,32 @@ void do_spule(){
set_Output(MOTOR_TRANS_DIR, 1); // direction: front set_Output(MOTOR_TRANS_DIR, 1); // direction: front
TCCR1B |= _BV(CS11); //TURN ON TCCR1B |= _BV(CS11); //TURN ON
} }
else if(taenzer_state.pos <= 10){
spule_onoff(0);
}
else{ else{
/* speed regulation - keep taenzer at 10% */ /* speed regulation - keep taenzer at 10% */
int32_t tmp = (100 - (int32_t)taenzer_state.pos/1000)*10; int32_t tmp = 0;//(100 - (int32_t)taenzer_state.pos/1000);
if(tmp < -75000/abzug_speed/2) //printf("temp1: %d\n", tmp);
tmp = -75000/abzug_speed/2; //TODO fix bounds
//if(tmp < -7500/abzug_speed/2)
// tmp = -7500/abzug_speed/2;
//printf("temp2: %d\n", tmp);
if(tmp < -35)
tmp = -35;
ICR5=75000/abzug_speed + tmp; ICR5=7500/abzug_speed + tmp;
OCR5C = ICR5/2; OCR5C = ICR5/2;
ICR1 = ICR5/0.7; ICR1 = ICR5/TRANS_ROT_FACTOR;
OCR1A = ICR1/2; OCR1A = ICR1/2;
if (read_Input(BTN_WICKELN_EIN, RISING)) { if (read_Input(BTN_WICKELN_EIN, RISING)) {
TCCR5B |= _BV(CS51); //TURN ON spule_onoff(1);
TCCR1B |= _BV(CS11); //TURN ON
} }
if (read_Input(BTN_WICKELN_AUS, RISING)) { if (read_Input(BTN_WICKELN_AUS, RISING)) {
TCCR5B &= ~(_BV(CS51)); spule_onoff(0);
TCCR1B &= ~(_BV(CS11));
} }
if (read_Input(BTN_INIT, RISING)) { if (read_Input(BTN_INIT, RISING)) {
@ -88,9 +105,11 @@ void do_spule(){
windings_wakeup = 0; windings_wakeup = 0;
} }
} }
//PORTH |= (1<<5);
} }
ISR(TIMER1_OVF_vect) { ISR(TIMER1_OVF_vect) {
//PORTH &= ~(1<<5);
//if(ioHelperReadBit(outStates, MOTOR_TRANS_DIR)){ //if(ioHelperReadBit(outStates, MOTOR_TRANS_DIR)){
if(PORTB & (1<<6)){ if(PORTB & (1<<6)){
spule_trans_pos -= 1; spule_trans_pos -= 1;
@ -99,12 +118,14 @@ ISR(TIMER1_OVF_vect) {
spule_trans_pos += 1; spule_trans_pos += 1;
} }
//TODO keep track if position stays in bounds //TODO keep track if position stays in bounds
//PORTH |= (1<<5);
} }
ISR(TIMER5_OVF_vect) { ISR(TIMER5_OVF_vect) {
//PORTH &= ~(1<<5);
static uint16_t steps = 0; static uint16_t steps = 0;
steps++; steps++;
if(steps == 5000){ if(steps == 50000){
windings++; windings++;
steps=0; steps=0;
printf("windungen: %d\n", windings); printf("windungen: %d\n", windings);
@ -135,4 +156,5 @@ ISR(TIMER5_OVF_vect) {
OCR1A = ICR1/2; OCR1A = ICR1/2;
printf("nachlauf abbauen\n"); printf("nachlauf abbauen\n");
} }
//PORTH |= (1<<5);
} }

@ -89,7 +89,7 @@ void do_taenzer(){
OCR4A = ICR4/2; OCR4A = ICR4/2;
if(kraftsensor_valid && notaus_state == POWER_ON){ if(kraftsensor_valid && notaus_state == POWER_ON){
if(out < -TAENZER_KRAFT_HYST/2 && taenzer_state.pos < 300000UL){ if(out < -TAENZER_KRAFT_HYST/2 /* && taenzer_state.pos < 300000UL*/){
set_Output(MOTOR_TAENZER_DIR, 0); // direction: down set_Output(MOTOR_TAENZER_DIR, 0); // direction: down
TCCR4B |= _BV(CS41); //TURN ON TCCR4B |= _BV(CS41); //TURN ON
} }