Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbb9bbdbbb |
Vendored
+4
-4
@@ -8,8 +8,8 @@
|
|||||||
{
|
{
|
||||||
"name": "PlatformIO",
|
"name": "PlatformIO",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include",
|
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/include",
|
||||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src",
|
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/src",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
||||||
@@ -22,8 +22,8 @@
|
|||||||
"browse": {
|
"browse": {
|
||||||
"limitSymbolsToIncludedHeaders": true,
|
"limitSymbolsToIncludedHeaders": true,
|
||||||
"path": [
|
"path": [
|
||||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include",
|
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/include",
|
||||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src",
|
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/src",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
||||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
||||||
|
|||||||
Vendored
+3
-3
@@ -12,7 +12,7 @@
|
|||||||
"type": "platformio-debug",
|
"type": "platformio-debug",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "PIO Debug",
|
"name": "PIO Debug",
|
||||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||||
"projectEnvName": "nanoatmega328new",
|
"projectEnvName": "nanoatmega328new",
|
||||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||||
"internalConsoleOptions": "openOnSessionStart",
|
"internalConsoleOptions": "openOnSessionStart",
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
"type": "platformio-debug",
|
"type": "platformio-debug",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "PIO Debug (skip Pre-Debug)",
|
"name": "PIO Debug (skip Pre-Debug)",
|
||||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||||
"projectEnvName": "nanoatmega328new",
|
"projectEnvName": "nanoatmega328new",
|
||||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
"type": "platformio-debug",
|
"type": "platformio-debug",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "PIO Debug (without uploading)",
|
"name": "PIO Debug (without uploading)",
|
||||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||||
"projectEnvName": "nanoatmega328new",
|
"projectEnvName": "nanoatmega328new",
|
||||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||||
"internalConsoleOptions": "openOnSessionStart",
|
"internalConsoleOptions": "openOnSessionStart",
|
||||||
|
|||||||
+135
-56
@@ -1,87 +1,166 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
#include "rotary_encoder_driver.h"
|
#include "rotary_encoder_driver.h"
|
||||||
#include "motor_driver.h"
|
#include "motor_driver.h"
|
||||||
#include "p_controller.h"
|
#include "p_controller.h"
|
||||||
|
|
||||||
RotaryEncoderDriver encoder;
|
// RotaryEncoderDriver encoder;
|
||||||
MotorDriver motor;
|
// MotorDriver motor;
|
||||||
|
|
||||||
P_controller controller(12.5);
|
// P_controller controller(12.5);
|
||||||
|
|
||||||
const double REF_RPM1 = 30.0;
|
// const double REF_RPM1 = 30.0;
|
||||||
const double REF_RPM2 = 50.0;
|
// const double REF_RPM2 = 50.0;
|
||||||
|
|
||||||
const uint16_t STEP1_AT_SAMPLE = 50;
|
// const uint16_t STEP1_AT_SAMPLE = 50;
|
||||||
const uint16_t STEP2_AT_SAMPLE = 500;
|
// const uint16_t STEP2_AT_SAMPLE = 500;
|
||||||
|
|
||||||
const uint16_t STOP_AT_SAMPLE = 1000;
|
// const uint16_t STOP_AT_SAMPLE = 1000;
|
||||||
|
|
||||||
|
unsigned long state_entry_time;
|
||||||
|
|
||||||
|
unsigned long time_in_state()
|
||||||
|
{
|
||||||
|
return millis() - state_entry_time;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
init();
|
init();
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
// Serial.begin(9600);
|
||||||
cli();
|
|
||||||
encoder.begin();
|
|
||||||
motor.begin();
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
Initialization,
|
||||||
|
Operational,
|
||||||
|
Pre_Operational,
|
||||||
|
Stopped
|
||||||
|
};
|
||||||
|
|
||||||
|
State state = State::Initialization; // Define state variable and set the initial state
|
||||||
|
|
||||||
|
// cli();
|
||||||
|
// encoder.begin();
|
||||||
|
// motor.begin();
|
||||||
|
// sei();
|
||||||
|
|
||||||
// Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst
|
// Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst
|
||||||
motor.stop();
|
// motor.stop();
|
||||||
_delay_ms(2000);
|
// _delay_ms(2000);
|
||||||
|
|
||||||
Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual");
|
// Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual");
|
||||||
Serial.println("0.0,0.0,0.0,0.0,0.0");
|
// Serial.println("0.0,0.0,0.0,0.0,0.0");
|
||||||
|
|
||||||
encoder.reset_speed_measurement();
|
// encoder.reset_speed_measurement();
|
||||||
|
|
||||||
uint16_t sample = 0;
|
// uint16_t sample = 0;
|
||||||
double ref = 0.0;
|
// double ref = 0.0;
|
||||||
|
|
||||||
while (true) {
|
while (1) {
|
||||||
if (encoder.new_speed_available()) {
|
int c = 0; // for incoming serial data
|
||||||
sample++;
|
|
||||||
|
|
||||||
float time_ms = sample * 4.0f;
|
if (Serial.available() > 0)
|
||||||
|
{
|
||||||
|
c = Serial.read();
|
||||||
|
Serial.print("I received: ");
|
||||||
|
Serial.println(c, DEC);
|
||||||
|
}
|
||||||
|
|
||||||
// Step-inntak: viðmiðið hoppar úr 0 í REF_RPM á STEP_AT_SAMPLE
|
switch (state)
|
||||||
if (sample == STEP1_AT_SAMPLE) {
|
{
|
||||||
ref = REF_RPM1;
|
case State::Initialization:
|
||||||
|
if (c == 'p')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Idle exit action
|
||||||
|
// event1 transition action
|
||||||
|
Serial.println("Initialization finished");
|
||||||
|
// set new target state
|
||||||
|
state = State::Pre_Operational;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Going into Pre Operation");
|
||||||
|
}
|
||||||
|
if (c == 'o')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Idle exit action
|
||||||
|
// event2 transition action
|
||||||
|
Serial.println("Initialization finished");
|
||||||
|
// set new target state
|
||||||
|
state = State::Operational;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Going straight to Operation");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::Operational:
|
||||||
|
if (c == 'i')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Cooling exit action
|
||||||
|
Serial.println("Operation over");
|
||||||
|
// event3 transition action
|
||||||
|
// set new target state
|
||||||
|
state = State::Initialization;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Initialization state");
|
||||||
|
}
|
||||||
|
if (c == 's')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Cooling exit action
|
||||||
|
Serial.println("Operation over");
|
||||||
|
// event4 transition action
|
||||||
|
// set new target state
|
||||||
|
state = State::Stopped;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Stopping");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::Pre_Operational:
|
||||||
|
if (c == 'o')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Heating exit action
|
||||||
|
Serial.println("Pre Operation over");
|
||||||
|
// event3 transition action
|
||||||
|
// set new target state
|
||||||
|
state = State::Operational;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Going to Operation");
|
||||||
|
}
|
||||||
|
if (c == 's')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Heating exit action
|
||||||
|
Serial.println("Pre Operation over");
|
||||||
|
// event4 transition action
|
||||||
|
// set new target state
|
||||||
|
state = State::Stopped;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Stopping");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::Stopped:
|
||||||
|
if (c == 'i')
|
||||||
|
{
|
||||||
|
state_entry_time = millis();
|
||||||
|
// State::Failure exit action
|
||||||
|
Serial.println("Stop over");
|
||||||
|
// event3 transition action
|
||||||
|
// set new target state
|
||||||
|
state = State::Initialization;
|
||||||
|
// target state entry action
|
||||||
|
Serial.println("Initialization state");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sample == STEP2_AT_SAMPLE) {
|
|
||||||
ref = REF_RPM2;
|
|
||||||
}
|
|
||||||
|
|
||||||
double actual = encoder.rpm();
|
|
||||||
|
|
||||||
double pwm_percent = controller.update(ref, actual);
|
|
||||||
double pwm_requested = pwm_percent * 2.55;
|
|
||||||
|
|
||||||
double pwm_actual = pwm_requested;
|
|
||||||
if (pwm_actual > 255.0) pwm_actual = 255.0;
|
|
||||||
if (pwm_actual < -255.0) pwm_actual = -255.0;
|
|
||||||
|
|
||||||
motor.set(pwm_actual);
|
|
||||||
|
|
||||||
Serial.print(time_ms, 1);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.print(ref, 1);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.print(actual, 2);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.print(pwm_requested, 1);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.println(pwm_actual, 1);
|
|
||||||
|
|
||||||
if (sample >= STOP_AT_SAMPLE) {
|
|
||||||
motor.stop();
|
|
||||||
while (true) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user