Skip to content

Commit

Permalink
dyno: update encoder properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeferguson committed Jan 2, 2025
1 parent 42e53ee commit e176906
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions projects/dyno/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ $(BUILDDIRS):
echo "target remote localhost:3333" > .gdbinit
echo "mon reset init" >> .gdbinit
echo "" >> .gdbinit
echo "def flash" >> .gdbinit
echo "define flash" >> .gdbinit
echo " file $(BUILD_PATH)/$(PRG).elf" >> .gdbinit
echo " mon reset halt" >> .gdbinit
echo " mon flash erase_address pad 0x08000000 [file size $(PWD)/$(BUILD_PATH)/$(PRG).bin]" >> .gdbinit
echo " mon flash write_bank 0 $(PWD)/$(BUILD_PATH)/$(PRG).bin 0" >> .gdbinit
echo " mon reset init" >> .gdbinit
echo "end" >> .gdbinit
echo "" >> .gdbinit
echo "def reset" >> .gdbinit
echo "define reset" >> .gdbinit
echo " mon reset halt" >> .gdbinit
echo " mon reset init" >> .gdbinit
echo "end" >> .gdbinit
echo "def erase_all" >> .gdbinit
echo "define erase_all" >> .gdbinit
echo " mon reset halt" >> .gdbinit
echo " mon stm32f2x mass_erase 0" >> .gdbinit
echo " mon reset init" >> .gdbinit
Expand Down
16 changes: 10 additions & 6 deletions projects/dyno/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2023, Michael E. Ferguson
* Copyright (c) 2012-2025, Michael E. Ferguson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -56,8 +56,11 @@ ADS8684<SPI1_BASE, adc_cs, adc_reset, spi1_read_dma, spi1_write_dma> adc;
#include "dac121.hpp"
DAC121<SPI2_BASE, dut_dac_cs> dac;

// Encoder is AMT102-V, set to 2048PPR = 8192CPR
#include "encoder.hpp"
Encoder<TIM3_BASE> absorber_enc;
#define ABSORBER_CPR 8192.0f
#define TO_RADIANS (2.0f * 3.141592653589793f)

#include "copy_float.hpp"

Expand Down Expand Up @@ -300,7 +303,7 @@ int main(void)
dac.init();

// Setup systick
SysTick_Config(SystemCoreClock/25000);
SysTick_Config(SystemCoreClock / 25000);

LwIP_Init();
if (!udp_interface_init())
Expand All @@ -319,6 +322,7 @@ int main(void)
extern "C"
{

// Systick runs at 25khz
void SysTick_Handler(void)
{
// Update system time
Expand All @@ -328,17 +332,17 @@ void SysTick_Handler(void)
// Get system voltage
// adc is 12 bit (4096 count) spread over 3.3V
// voltage divider is 15k/1k
dyno.system_voltage = (ADC1->JDR1/4096.0f) * 3.3f * 16.0f;
dyno.system_voltage = (ADC1->JDR1 / 4096.0f) * 3.3f * 16.0f;
ADC1->CR2 |= ADC_CR2_JSWSTART;

// TODO: make the sampling time dynamic based on velocity
// Encoder update runs at 250hz
if (dyno.system_time % 100 == 0)
{
int32_t pos = absorber_enc.read();
int32_t vel = absorber_enc.read_speed();
// TODO: calculate position
dyno.position = 0.0f;
dyno.velocity = (static_cast<float>(vel) * 250.0f / 7200.0f) * 2.0f * 3.141592653589793f;
dyno.position = static_cast<float>(pos) / ABSORBER_CPR * TO_RADIANS;
dyno.velocity = (static_cast<float>(vel) * 250.0f / ABSORBER_CPR) * TO_RADIANS;
}

adc.update();
Expand Down

0 comments on commit e176906

Please sign in to comment.