bug fix static in motor und speed
This commit is contained in:
+7
-6
@@ -20,18 +20,19 @@ void Menu::addEntry(MenuAction* entry) {
|
||||
// TODO: Same shit here. Why I get the first element after an increment.
|
||||
static uint8_t inc = 0;
|
||||
inc++;
|
||||
if (inc == 2) {
|
||||
if (inc == 1) {
|
||||
this->it = this->entrys.begin();
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::printMenu() {
|
||||
Serial.println("\n\n");
|
||||
std::cout << std::endl;
|
||||
for (std::list<MenuAction*>::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) {
|
||||
MenuAction* selectedEntry = *(iter);
|
||||
if (this->it == iter)
|
||||
Serial.print(" ");
|
||||
Serial.println(selectedEntry->getName());
|
||||
std::cout << " " << selectedEntry->getName() << std::endl;
|
||||
else
|
||||
std::cout << selectedEntry->getName() << std::endl;
|
||||
}
|
||||
this->inSubmenu = false;
|
||||
}
|
||||
@@ -62,11 +63,9 @@ void Menu::up() {
|
||||
std::list<MenuAction*>::iterator iter = this->entrys.end();
|
||||
iter--;
|
||||
if (this->it != this->entrys.begin()) {
|
||||
Serial.println("--");
|
||||
this->it--;
|
||||
}
|
||||
else {
|
||||
Serial.println("begin");
|
||||
this->it = iter;
|
||||
}
|
||||
this->printMenu();
|
||||
@@ -86,6 +85,8 @@ void Menu::right() {
|
||||
selectedEntry->getMenu()->setParentMenu(this);
|
||||
}
|
||||
selectedEntry->runAction();
|
||||
if (selectedEntry->getIsMenu())
|
||||
selectedEntry->getMenu()->down();
|
||||
}
|
||||
|
||||
void Menu::left() {
|
||||
|
||||
@@ -13,10 +13,6 @@
|
||||
|
||||
#include <list>
|
||||
|
||||
// Only for print in consol
|
||||
// have to be deleted after display installation
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "menuAction.h"
|
||||
#include "menuControl.h"
|
||||
|
||||
|
||||
@@ -22,6 +22,14 @@ MenuAction::MenuAction(const char* name, void (*function) ()) {
|
||||
this->callback = nullptr;
|
||||
}
|
||||
|
||||
// MenuAction::MenuAction(const char* name, void (DriveManager:: *classFunction) ()) {
|
||||
// this->name = name;
|
||||
// this->classFunction = classFunction;
|
||||
// this->classFunc = true;
|
||||
// this->function = nullptr;
|
||||
// this->callback = nullptr;
|
||||
// }
|
||||
|
||||
MenuAction::MenuAction(const char* name, MenuControl* menu) {
|
||||
this->name = name;
|
||||
this->menu = menu;
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#ifndef MENU_CONTROLL_H
|
||||
#define MENU_CONTROLL_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class MenuControl {
|
||||
public:
|
||||
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
/**
|
||||
* @file pidSettings.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-01-19
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#include "menuPidSettings.h"
|
||||
|
||||
MenuPidSettings::MenuPidSettings(PID* pid) {
|
||||
this->pid = pid;
|
||||
this->values[0] = (uint8_t) pid->GetKp();
|
||||
this->values[1] = (uint8_t) pid->GetKi();
|
||||
this->values[2] = (uint8_t) pid->GetKd();
|
||||
}
|
||||
|
||||
void MenuPidSettings::down() {
|
||||
if (values[curPos] > 0)
|
||||
values[curPos]--;
|
||||
|
||||
this->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::up() {
|
||||
if (values[curPos] < UINT8_MAX)
|
||||
values[curPos]++;
|
||||
|
||||
this->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::right() {
|
||||
if (curPos < NUM_VAL - 1)
|
||||
curPos++;
|
||||
else
|
||||
curPos = 0;
|
||||
|
||||
this->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::left() {
|
||||
if (curPos > 0)
|
||||
curPos--;
|
||||
else
|
||||
curPos = NUM_VAL - 1;
|
||||
|
||||
this->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::no() {
|
||||
if (parentMenu)
|
||||
this->parentMenu->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::yes() {
|
||||
this->pid->SetTunings(this->values[0], this->values[1], this->values[2]);
|
||||
if (parentMenu)
|
||||
this->parentMenu->printMenu();
|
||||
}
|
||||
|
||||
void MenuPidSettings::printMenu() {
|
||||
Serial.println(" P I D ");
|
||||
char pos[16];
|
||||
switch (this->curPos) {
|
||||
case 0:
|
||||
sprintf(pos, "_%d.3_ %d.3 %d.3 ", this->values[0], this->values[1], this->values[2]);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
sprintf(pos, " %d.3 _%d.3_ %d.3 ", this->values[0], this->values[1], this->values[2]);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
sprintf(pos, " %d.3 %d.3 _%d.3_", this->values[0], this->values[1], this->values[2]);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Serial.println(pos);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/**
|
||||
* @file pidSettings.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-01-19
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PID_SETTINGS_H
|
||||
#define PID_SETTINGS_H
|
||||
|
||||
#include "menuControl.h"
|
||||
#include <PID_v1.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// TODO: delete after installtion of display
|
||||
#include <Arduino.h>
|
||||
|
||||
#define NUM_VAL 3
|
||||
|
||||
class MenuPidSettings : public MenuControl {
|
||||
public:
|
||||
MenuPidSettings(PID* pid);
|
||||
|
||||
void down();
|
||||
void up();
|
||||
void right();
|
||||
void left();
|
||||
void no();
|
||||
void yes();
|
||||
|
||||
void printMenu();
|
||||
|
||||
private:
|
||||
PID* pid;
|
||||
uint8_t values[NUM_VAL];
|
||||
uint8_t curPos = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // PID_SETTINGS_H
|
||||
@@ -34,23 +34,24 @@ void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uin
|
||||
ledcWrite(this->pwm_channel, 0);
|
||||
}
|
||||
|
||||
void MotorControl::loop() {
|
||||
static uint32_t last_millis = 0;
|
||||
uint16_t MotorControl::loop() {
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsed_time = time - this->last_millis;
|
||||
|
||||
//Cancel if delay is not reached
|
||||
if (time - last_millis < delay) {
|
||||
return;
|
||||
}
|
||||
if (elapsed_time < delay)
|
||||
return elapsed_time;
|
||||
|
||||
runMotorControl();
|
||||
last_millis = time;
|
||||
this->last_millis = time;
|
||||
return elapsed_time;
|
||||
}
|
||||
|
||||
void MotorControl::runMotorControl() {
|
||||
// Absolute difference between target_power and speed
|
||||
// Absolute difference between target_power and power
|
||||
uint8_t abs_difference = abs(this->target_power - this->power);
|
||||
|
||||
// Difference between target_power and speed
|
||||
// Difference between target_power and power
|
||||
int16_t difference = this->target_power - this->power;
|
||||
|
||||
// Check that the target speed is close to 0 and that the abs_difference is lower than powersteps
|
||||
|
||||
@@ -48,8 +48,9 @@ class MotorControl {
|
||||
* functions returns immediately.
|
||||
* @see runMotorControl()
|
||||
* @see DELAY
|
||||
* @return time since the last call in Milliseconds
|
||||
*/
|
||||
void loop();
|
||||
uint16_t loop();
|
||||
|
||||
/**
|
||||
* @brief Normaly called repeatedly by loop() to update the pwm signal.
|
||||
@@ -161,6 +162,8 @@ class MotorControl {
|
||||
uint8_t dir_2;
|
||||
uint8_t delay = DELAY;
|
||||
uint8_t powersteps = POWERSTEPS;
|
||||
|
||||
uint32_t last_millis = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -29,24 +29,24 @@ void Speedometer::init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t ste
|
||||
this->isInit = true;
|
||||
}
|
||||
|
||||
void Speedometer::loop() {
|
||||
static uint32_t last_millis = 0;
|
||||
uint16_t Speedometer::loop() {
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsed_time = time - this->last_millis_loop;
|
||||
|
||||
//Cancel if delay is not reached
|
||||
if (time - last_millis < delay) {
|
||||
return;
|
||||
if (elapsed_time < delay) {
|
||||
return elapsed_time;
|
||||
}
|
||||
runSpeedometer();
|
||||
last_millis = time;
|
||||
this->last_millis_loop = time;
|
||||
return elapsed_time;
|
||||
}
|
||||
|
||||
void Speedometer::runSpeedometer() {
|
||||
static uint32_t last_millis = 0;
|
||||
uint32_t time = millis();
|
||||
|
||||
uint16_t elapsed_time = time - last_millis;
|
||||
last_millis = time;
|
||||
uint16_t elapsed_time = time - last_millis_calc;
|
||||
last_millis_calc = time;
|
||||
|
||||
int16_t count = encoder.getCount();
|
||||
this->addValToBuf(count);
|
||||
|
||||
@@ -58,8 +58,9 @@ class Speedometer {
|
||||
* functions returns immediately.
|
||||
* @see runSpeedometer()
|
||||
* @see DELAY_SPEEDOMETER
|
||||
* @return time since the last call in Milliseconds
|
||||
*/
|
||||
void loop();
|
||||
uint16_t loop();
|
||||
|
||||
/**
|
||||
* @brief Noramly called repeatedly by loop() to calcluate new values.
|
||||
@@ -115,6 +116,9 @@ class Speedometer {
|
||||
uint8_t delay = DELAY_SPEEDOMETER;
|
||||
|
||||
int16_t *buf;
|
||||
|
||||
uint32_t last_millis_loop = 0;
|
||||
uint32_t last_millis_calc = 0;
|
||||
};
|
||||
|
||||
#endif // SPEEDOMETER_H
|
||||
Reference in New Issue
Block a user