Skip to content

Commit

Permalink
add first test for moveTimed
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Feb 20, 2025
1 parent 2ee1cd4 commit 39ca55e
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 17 deletions.
37 changes: 20 additions & 17 deletions examples/MoveTimed/MoveTimed.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#if defined(ARDUINO_ARCH_AVR)
#include "AVRStepperPins.h"
#include "FastAccelStepper.h"
#ifdef SIMULATOR
#include <avr/sleep.h>
#endif

// As in StepperDemo for Motor 1 on AVR
#define dirPinStepperX 5
Expand All @@ -19,14 +22,13 @@ FastAccelStepper *stepperY = NULL;
struct control_s {
uint64_t time;
uint16_t phi;
bool is_running;
uint16_t drift; // time delta in ticks
};
struct control_s controlX = {
.time = 0, .phi = 0, .is_running = false, .drift = 0
.time = 0, .phi = 0, .drift = 0
};
struct control_s controlY = {
.time = 0, .phi = 90, .is_running = false, .drift = 0
.time = 0, .phi = 90, .drift = 0
};

// This means, the circle is executed in 360*4ms = 1440ms
Expand Down Expand Up @@ -57,6 +59,8 @@ void setup() {
stepperY->setEnablePin(enablePinStepperY);
stepperY->setAutoEnable(false);

stepperY->setCurrentPosition(steps[90]);

// Without auto enable, need to enable manually both steppers
stepperX->enableOutputs();
stepperY->enableOutputs();
Expand All @@ -71,12 +75,10 @@ void setup() {
}

void moveStepper(FastAccelStepper *stepper, struct control_s *control) {
if (control->phi == 360) {
control->phi = 0;
}
uint16_t phi = control->phi % 360;

// The table contains only one quadrant
uint16_t index = control->phi;
uint16_t index = phi;
bool negate = false;
if (index > 180) {
negate = true;
Expand All @@ -90,16 +92,6 @@ void moveStepper(FastAccelStepper *stepper, struct control_s *control) {
position = -position;
}
int32_t current_position = stepper->getPositionAfterCommandsCompleted();
if (!control->is_running) {
// we need to wait until current_position and position match
if (position != current_position) {
// just advance time and phi
control->time += TIMESTEP_TICKS;
control->phi += 1;
return;
}
control->is_running = true;
}
int32_t steps = position - current_position;
uint32_t actual;
int8_t rc;
Expand Down Expand Up @@ -141,6 +133,8 @@ void loop() {
Serial.print(' ');
Serial.print(stepperX->getPositionAfterCommandsCompleted());
Serial.print(' ');
Serial.print(stepperY->getPositionAfterCommandsCompleted());
Serial.print(' ');
Serial.println(millis());
last_millis = millis();
}
Expand All @@ -151,6 +145,15 @@ void loop() {
else {
moveStepper(stepperY, &controlY);
}
if ((controlX.phi != 361) && (controlY.phi != 451)) {
return;
}
// Full round should be completed
#ifdef SIMULATOR
delay(1000);
noInterrupts();
sleep_cpu();
#endif
}
#else
void setup() {}
Expand Down
2 changes: 2 additions & 0 deletions extras/tests/simavr_based/test_moveTimed/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/
x.log
130 changes: 130 additions & 0 deletions extras/tests/simavr_based/test_moveTimed/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#
# In order to execute the test for one directory use:
#
# make -C test_sd_01b_328p -f ../Makefile.test

SRC=$(wildcard ../../../src/*) $(wildcard src/*)

# platformio should contain only one env section.
# This section states the dut name
# atmega168
# atmega168p
# atmega328
# atmega328p
# atmega2560_timer1
# atmega2560_timer3
# atmega2560_timer4
# atmega2560_timer5
#

DUT=$(shell gawk '/env:/{print(substr($$1,6,length($$1)-6))}' platformio.ini)

TRACES=-at StepISR=trace@0x25/0x08 # PB3
TRACES+=-at FillISR=trace@0x25/0x10 # PB4

#
ifeq ($(DUT),atmega2560_timer1)
DEVICE=atmega2560
TRACES+=-at StepA=trace@0x025/0x20 #OC1A PB5 11 ATMega2560
TRACES+=-at StepB=trace@0x025/0x40 #OC1B PB6 12 ATMega2560
TRACES+=-at StepC=trace@0x025/0x80 #OC1C PB7 13 ATMega2560
#
else ifeq ($(DUT),atmega2560_timer3)
DEVICE=atmega2560
TRACES+=-at StepA=trace@0x02e/0x08 #OC3A PE3 5 ATMega2560
TRACES+=-at StepB=trace@0x02e/0x10 #OC3B PE4 2 ATMega2560
TRACES+=-at StepC=trace@0x02e/0x20 #OC3C PE5 3 ATMega2560
#
else ifeq ($(DUT),atmega2560_timer4)
DEVICE=atmega2560
TRACES+=-at StepA=trace@0x102/0x08 #OC4A PH3 6 ATMega2560
TRACES+=-at StepB=trace@0x102/0x10 #OC4B PH4 7 ATMega2560
TRACES+=-at StepC=trace@0x102/0x20 #OC4C PH5 8 ATMega2560
#
else ifeq ($(DUT),atmega2560_timer5)
DEVICE=atmega2560
TRACES+=-at StepA=trace@0x10b/0x08 #OC5A PL3 46 ATMega2560
TRACES+=-at StepB=trace@0x10b/0x10 #OC5B PL4 45 ATMega2560
TRACES+=-at StepC=trace@0x10b/0x20 #OC5C PL5 44 ATMega2560

else ifeq ($(DUT),atmega168)
DEVICE=atmega168
TRACES+=-at StepA=trace@0x25/0x02 #OC1A PB1 9 atmega168
TRACES+=-at StepB=trace@0x25/0x04 #OC1B PB2 10 atmega168

else ifeq ($(DUT),atmega168p)
DEVICE=atmega168p
TRACES+=-at StepA=trace@0x25/0x02 #OC1A PB1 9 atmega168p
TRACES+=-at StepB=trace@0x25/0x04 #OC1B PB2 10 atmega168p

else ifeq ($(DUT),atmega328)
DEVICE=atmega328
TRACES+=-at StepA=trace@0x25/0x02 #OC1A PB1 9 ATMega328
TRACES+=-at StepB=trace@0x25/0x04 #OC1B PB2 10 ATMega328

else ifeq ($(DUT),atmega328p)
DEVICE=atmega328p
TRACES+=-at StepA=trace@0x25/0x02 #OC1A PB1 9 ATMega328p
TRACES+=-at StepB=trace@0x25/0x04 #OC1B PB2 10 ATMega328p

else ifeq ($(DUT),atmega32u4)
DEVICE=atmega32u4
TRACES+=-at StepA=trace@0x025/0x20 #OC1A PB5 11
TRACES+=-at StepB=trace@0x025/0x40 #OC1B PB6 12
#TRACES+=-at StepC=trace@0x025/0x80 #OC1C PB7 13

endif

ifeq ($(DEVICE),atmega2560)
TRACES+=-at DirA=trace@0x2b/0x01 # Pin 21 PD0
TRACES+=-at DirB=trace@0x2b/0x02 # Pin 20 PD1
TRACES+=-at DirC=trace@0x10b/0x80 # Pin 42 PL7
TRACES+=-at EnableA=trace@0x2b/0x04 # Pin 19 PD2
TRACES+=-at EnableB=trace@0x2b/0x08 # Pin 18 PD3
TRACES+=-at EnableC=trace@0x10b/0x40 # Pin 43 PL6

else ifeq ($(DEVICE),$(filter $(DEVICE),atmega168 atmega168p atmega328 atmega328p))
TRACES+=-at DirA=trace@0x2b/0x20 # Pin 5 PD5
TRACES+=-at DirB=trace@0x2b/0x80 # Pin 7 PD7
TRACES+=-at EnableA=trace@0x2b/0x40 # Pin 6 PD6
TRACES+=-at EnableB=trace@0x25/0x01 # Pin 8 PB0

else ifeq ($(DUT),atmega32u4)
TRACES+=-at DirA=trace@0x25/0x10 # Pin 26 PB4
TRACES+=-at DirB=trace@0x25/0x08 # Pin 14 PB3
#TRACES+=-at DirC=trace@0x10b/0x80 # Pin 42 PL7
TRACES+=-at EnableA=trace@0x25/0x04 # Pin 16 PB2
TRACES+=-at EnableB=trace@0x25/0x02 # Pin 15 PB1
#TRACES+=-at EnableC=trace@0x10b/0x40 # Pin 43 PL6

endif

FIRMWARE=".pio/build/$(DUT)/firmware.elf"

DIR=$(shell env pwd)

test: .tested

.tested: result.txt ../judge_pos0.awk
echo DUT=$(DUT)
rm -f .tested
gawk -f ../judge_pos0.awk -v DIR=$(DIR) result.txt
test -f .tested

result.txt: x.vcd
gawk -f ../eval.awk x.vcd >x.log
mv result.txt result.tmp
gawk -f log2pos.awk x.log
mv result.tmp result.txt

x.vcd: $(SRC) ../run_avr platformio.ini src/MoveTimed.ino
~/.platformio/penv/bin/pio run -e $(DUT) || ~/.local/bin/pio run -e $(DUT) || env pio run -e $(DUT)
../run_avr $(FIRMWARE) -m $(DEVICE) -o x.vcd $(TRACES)

src/MoveTimed.ino:
mkdir -p src
cd src; ln -s ../../../../../examples/MoveTimed/MoveTimed.ino .

clean:
rm -fR .pio .tested x.vcd result.txt

24 changes: 24 additions & 0 deletions extras/tests/simavr_based/test_moveTimed/log2pos.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BEGIN {
posX = 0
posY = 1600
FS = "[ =]"
ret = 0
}

/A: position=/{
posX = $6
}
/B: position=/{
posY = $6+1600
}
/: position/ {
r = sqrt(posX*posX+posY*posY)
if ((r > 1602) || (r < 1597)) {
print(posX,posY,r)
ret = -1
}
}

END {
exit(ret)
}
31 changes: 31 additions & 0 deletions extras/tests/simavr_based/test_moveTimed/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]

# There should be only one env section for the DUT under test.
# One of
# atmega168p
# atmega328p
# atmega2560_timer1
# atmega2560_timer3
# atmega2560_timer4
# atmega2560_timer5
#
[common]
# This is the line input to StepperDemo:
build_flags = -D SIMULATOR -D SIMAVR_TIME_MEASUREMENT

[env:atmega328p]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_flags = -Werror -Wall ${common.build_flags}
lib_extra_dirs = .

0 comments on commit 39ca55e

Please sign in to comment.