Part 4
This commit is contained in:
@@ -1 +1,6 @@
|
||||
.pio
|
||||
venv
|
||||
*.py
|
||||
*.csv
|
||||
scripts
|
||||
*.typ
|
||||
Vendored
+19
-19
@@ -8,29 +8,29 @@
|
||||
{
|
||||
"name": "PlatformIO",
|
||||
"includePath": [
|
||||
"C:/Users/isakt/Embedded/ESP_Project2_H26/include",
|
||||
"C:/Users/isakt/Embedded/ESP_Project2_H26/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
|
||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include",
|
||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src",
|
||||
"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/libraries/EEPROM/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
|
||||
""
|
||||
],
|
||||
"browse": {
|
||||
"limitSymbolsToIncludedHeaders": true,
|
||||
"path": [
|
||||
"C:/Users/isakt/Embedded/ESP_Project2_H26/include",
|
||||
"C:/Users/isakt/Embedded/ESP_Project2_H26/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
|
||||
"C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
|
||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include",
|
||||
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src",
|
||||
"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/libraries/EEPROM/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
|
||||
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
|
||||
""
|
||||
]
|
||||
},
|
||||
@@ -45,7 +45,7 @@
|
||||
],
|
||||
"cStandard": "gnu11",
|
||||
"cppStandard": "gnu++11",
|
||||
"compilerPath": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
|
||||
"compilerPath": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
|
||||
"compilerArgs": [
|
||||
"-mmcu=atmega328p",
|
||||
""
|
||||
|
||||
Vendored
+6
-6
@@ -12,9 +12,9 @@
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug",
|
||||
"executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"projectEnvName": "nanoatmega328new",
|
||||
"toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"preLaunchTask": {
|
||||
"type": "PlatformIO",
|
||||
@@ -25,18 +25,18 @@
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug (skip Pre-Debug)",
|
||||
"executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"projectEnvName": "nanoatmega328new",
|
||||
"toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug (without uploading)",
|
||||
"executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
|
||||
"projectEnvName": "nanoatmega328new",
|
||||
"toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"loadMode": "manual"
|
||||
}
|
||||
|
||||
@@ -8,11 +8,16 @@
|
||||
#include "timer_usec.h"
|
||||
|
||||
class RotaryEncoderDriver {
|
||||
static const uint8_t HIST_LEN = 4; // 8 x 5 ms = 40 ms gluggi fyrir rpm-utreikning
|
||||
|
||||
volatile uint8_t cycle;
|
||||
volatile int16_t count;
|
||||
volatile int16_t prev_count;
|
||||
volatile bool speed_ready;
|
||||
|
||||
volatile int16_t history[HIST_LEN];
|
||||
volatile uint8_t hist_idx;
|
||||
|
||||
Digital_out led;
|
||||
|
||||
Digital_in eA;
|
||||
|
||||
+47
-39
@@ -1,13 +1,24 @@
|
||||
/* #include <avr/io.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "rotary_encoder_driver.h"
|
||||
#include "motor_driver.h"
|
||||
#include "p_controller.h"
|
||||
|
||||
RotaryEncoderDriver encoder;
|
||||
MotorDriver motor;
|
||||
|
||||
P_controller controller(12.5);
|
||||
|
||||
const double REF_RPM1 = 30.0;
|
||||
const double REF_RPM2 = 50.0;
|
||||
|
||||
const uint16_t STEP1_AT_SAMPLE = 50;
|
||||
const uint16_t STEP2_AT_SAMPLE = 500;
|
||||
|
||||
const uint16_t STOP_AT_SAMPLE = 1000;
|
||||
|
||||
int main() {
|
||||
init();
|
||||
Serial.begin(115200);
|
||||
@@ -15,65 +26,62 @@ int main() {
|
||||
cli();
|
||||
encoder.begin();
|
||||
motor.begin();
|
||||
|
||||
|
||||
|
||||
|
||||
sei();
|
||||
|
||||
|
||||
|
||||
// Make sure motor is stopped before starting test
|
||||
// Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst
|
||||
motor.stop();
|
||||
|
||||
_delay_ms(2000);
|
||||
|
||||
Serial.println("time_ms,rpm");
|
||||
Serial.println("0.0,0.0");
|
||||
Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual");
|
||||
Serial.println("0.0,0.0,0.0,0.0,0.0");
|
||||
|
||||
// Synchronize the RPM measurement window with the step input
|
||||
encoder.reset_speed_measurement();
|
||||
|
||||
// Apply unit-step input
|
||||
motor.forward();
|
||||
|
||||
uint16_t sample = 0;
|
||||
double ref = 0.0;
|
||||
|
||||
while (true) {
|
||||
|
||||
if (encoder.new_speed_available())
|
||||
{
|
||||
if (encoder.new_speed_available()) {
|
||||
sample++;
|
||||
|
||||
float time_ms = sample * 5.0f;
|
||||
float speed = encoder.rpm();
|
||||
float time_ms = sample * 4.0f;
|
||||
|
||||
// Step-inntak: viðmiðið hoppar úr 0 í REF_RPM á STEP_AT_SAMPLE
|
||||
if (sample == STEP1_AT_SAMPLE) {
|
||||
ref = REF_RPM1;
|
||||
}
|
||||
|
||||
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.println(speed, 2);
|
||||
Serial.print(ref, 1);
|
||||
Serial.print(",");
|
||||
Serial.print(actual, 2);
|
||||
Serial.print(",");
|
||||
Serial.print(pwm_requested, 1);
|
||||
Serial.print(",");
|
||||
Serial.println(pwm_actual, 1);
|
||||
|
||||
|
||||
// Stop after about 8 seconds {2000 (<- input in sample >= xxxx) * 4 = 8 sec}
|
||||
if (sample >= 400)
|
||||
{
|
||||
if (sample >= STOP_AT_SAMPLE) {
|
||||
motor.stop();
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
while (true) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Serial.print("POSITION: ");
|
||||
//Serial.print("RPM: ");
|
||||
|
||||
|
||||
//Serial.println(encoder.position());
|
||||
//Serial.println(encoder.rpm());
|
||||
|
||||
//_delay_ms(50);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
@@ -8,9 +8,15 @@ RotaryEncoderDriver::RotaryEncoderDriver()
|
||||
, count(0)
|
||||
, prev_count(0)
|
||||
, speed_ready(false)
|
||||
, hist_idx(0)
|
||||
, led(5)
|
||||
, eA(2)
|
||||
, eB(5) {}
|
||||
, eB(5)
|
||||
{
|
||||
for (uint8_t i = 0; i < HIST_LEN; i++) {
|
||||
history[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RotaryEncoderDriver::begin() {
|
||||
@@ -23,7 +29,7 @@ void RotaryEncoderDriver::begin() {
|
||||
EICRA |= (1<<ISC01) | (1<<ISC00);
|
||||
EIMSK |= (1<<INT0);
|
||||
|
||||
timer.init(5000);
|
||||
timer.init(4000);
|
||||
}
|
||||
|
||||
void RotaryEncoderDriver::update() {
|
||||
@@ -47,13 +53,17 @@ int RotaryEncoderDriver::position() {
|
||||
}
|
||||
|
||||
void RotaryEncoderDriver::update_delta() {
|
||||
|
||||
delta = count - prev_count;
|
||||
|
||||
prev_count = count;
|
||||
int16_t oldest = history[hist_idx];
|
||||
history[hist_idx] = count;
|
||||
hist_idx = (hist_idx + 1) % HIST_LEN;
|
||||
|
||||
delta = count - oldest; // breyting yfir HIST_LEN * Ts (40 ms) glugga
|
||||
|
||||
prev_count = count;
|
||||
|
||||
speed_ready = true;
|
||||
|
||||
speed_ready = true;
|
||||
|
||||
led.set_lo();
|
||||
}
|
||||
|
||||
@@ -67,7 +77,7 @@ float RotaryEncoderDriver::count_per_sec() {
|
||||
|
||||
SREG = oldSREG;
|
||||
|
||||
return local_delta / 0.005f;
|
||||
return local_delta / (HIST_LEN * 0.004f);
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +118,11 @@ void RotaryEncoderDriver::reset_speed_measurement()
|
||||
delta = 0;
|
||||
speed_ready = false;
|
||||
|
||||
for (uint8_t i = 0; i < HIST_LEN; i++) {
|
||||
history[i] = count;
|
||||
}
|
||||
hist_idx = 0;
|
||||
|
||||
SREG = oldSREG;
|
||||
}
|
||||
|
||||
|
||||
+6
-12
@@ -1,4 +1,6 @@
|
||||
/* #include <Arduino.h>
|
||||
/* Óvirkt núna, "alvöru" main() er í main.cpp. Þessi skrá var notuð í undirbúning/prófun
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "p_controller.h"
|
||||
|
||||
@@ -20,7 +22,7 @@ int main()
|
||||
}
|
||||
|
||||
return 0;
|
||||
} */
|
||||
}
|
||||
|
||||
|
||||
#include <Arduino.h>
|
||||
@@ -36,9 +38,6 @@ int main()
|
||||
|
||||
motor.begin();
|
||||
|
||||
//_delay_ms(3000);
|
||||
//motor.set(200);
|
||||
|
||||
while (true)
|
||||
{
|
||||
motor.set(60);
|
||||
@@ -49,11 +48,6 @@ int main()
|
||||
|
||||
motor.set(240);
|
||||
_delay_ms(3000);
|
||||
|
||||
//motor.stop();
|
||||
//_delay_ms(2000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user