Skip to content

Commit

Permalink
update cdnet; update bias_encoder when finish calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
dukelec committed Oct 24, 2024
1 parent 92cdffe commit 72c3a16
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ Also be careful to prevent the motor from overheating.
Then write 1 to `state` to enter the drag mode, where a constant current is continuously passed through some of the motor's coils
(please complete the subsequent operations as soon as possible to prevent the motor from overheating).

Write 1 to `cali_run` to start the calibration, the motor coil will be energized sequentially, the motor will turn clockwise first, then counterclockwise,
and finally print the calculated encoder offset value, write it to `bias_encoder`, and finally save it.
Write 1 to `cali_run` to start the calibration. The motor coils will be energized sequentially, causing the motor to turn clockwise first, then counterclockwise.
Afterward, the calculated encoder offset value will be printed and written to `bias_encoder`. Be sure to save it to flash.

Write 1 to `cali_run` as soon as possible after writing 1 to `state`, so that the coils can be energized in turn, sharing the strain on individual coils.
Write 1 to `cali_run` as soon as possible after writing 1 to `state`, so that the coils can be energized in turn, sharing the strain on individual coils.

If the motor does not rotate clockwise first during calibration (the encoder value not increases),
you will need to swap any two wires of the motor to change the direction of motor rotation.
you will need to swap any two wires of the motor to change the direction of motor rotation.


### Torque Mode (state = 2)
Expand Down
5 changes: 2 additions & 3 deletions mdrv_bl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_uart_ex.c \
Core/Src/system_stm32g4xx.c \
Core/Src/sysmem.c \
Core/Src/syscalls.c \
cdnet/dispatch/cdnet_dispatch.c \
cdnet/dispatch/helper.c \
cdnet/core/cdnet_core.c \
cdnet/parser/cdnet_l0.c \
cdnet/parser/cdnet_l1.c \
cdnet/dev/cdctl.c \
Expand Down Expand Up @@ -138,7 +137,7 @@ C_INCLUDES = \
-IDrivers/CMSIS/Device/ST/STM32G4xx/Include \
-IDrivers/CMSIS/Include \
-Icdnet/parser \
-Icdnet/dispatch \
-Icdnet/core \
-Icdnet/utils \
-Icdnet/dev \
-Icdnet/arch/stm32 \
Expand Down
2 changes: 1 addition & 1 deletion mdrv_bl/usr/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef __APP_MAIN_H__
#define __APP_MAIN_H__

#include "cdnet_dispatch.h"
#include "cdnet_core.h"
#include "cd_debug.h"
#include "cdbus_uart.h"
#include "cdctl.h"
Expand Down
5 changes: 2 additions & 3 deletions mdrv_fw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_uart_ex.c \
Core/Src/system_stm32g4xx.c \
Core/Src/sysmem.c \
Core/Src/syscalls.c \
cdnet/dispatch/cdnet_dispatch.c \
cdnet/dispatch/helper.c \
cdnet/core/cdnet_core.c \
cdnet/parser/cdnet_l0.c \
cdnet/parser/cdnet_l1.c \
cdnet/dev/cdctl_it.c \
Expand Down Expand Up @@ -147,7 +146,7 @@ C_INCLUDES = \
-IDrivers/CMSIS/Device/ST/STM32G4xx/Include \
-IDrivers/CMSIS/Include \
-Icdnet/parser \
-Icdnet/dispatch \
-Icdnet/core \
-Icdnet/utils \
-Icdnet/dev \
-Icdnet/arch/stm32 \
Expand Down
40 changes: 10 additions & 30 deletions mdrv_fw/usr/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,6 @@ static void device_init(void)
}


extern uint32_t end; // end of bss
#define STACK_CHECK_SKIP 0x200
#define STACK_CHECK_SIZE (64 + STACK_CHECK_SKIP)

static void stack_check_init(void)
{
int i;
printf("stack_check_init: skip: %p ~ %p, to %p\n",
&end, &end + STACK_CHECK_SKIP, &end + STACK_CHECK_SIZE);
for (i = STACK_CHECK_SKIP; i < STACK_CHECK_SIZE; i+=4)
*(uint32_t *)(&end + i) = 0xababcdcd;
}

static void stack_check(void)
{
int i;
for (i = STACK_CHECK_SKIP; i < STACK_CHECK_SIZE; i+=4) {
if (*(uint32_t *)(&end + i) != 0xababcdcd) {
printf("stack overflow %p (skip: %p ~ %p): %08lx\n",
&end + i, &end, &end + STACK_CHECK_SKIP, *(uint32_t *)(&end + i));
d_error("stack overflow %p (skip: %p ~ %p): %08lx\n",
&end + i, &end, &end + STACK_CHECK_SKIP, *(uint32_t *)(&end + i));
while (true);
}
}
}

#if 0
static void dump_hw_status(void)
{
Expand Down Expand Up @@ -317,7 +290,8 @@ void cali_elec_angle(void)
d_info("cali: finished, result:\n");
d_info("cali: cw: %02x\n", (amount_f / csa.motor_poles) & 0xffff);
d_info("cali: ccw: %02x\n", (amount_r / csa.motor_poles) & 0xffff);
d_info("cali: avg: %02x\n", ((amount_f + amount_r) / csa.motor_poles / 2) & 0xffff);
csa.bias_encoder = ((amount_f + amount_r) / csa.motor_poles / 2) & 0xffff;
d_info("cali: avg: %02x, updated to bias_encoder\n", csa.bias_encoder);
uint8_t dat = ST_STOP;
state_w_hook_before(0, 1, &dat);
csa.state = ST_STOP;
Expand Down Expand Up @@ -354,11 +328,13 @@ void cali_elec_angle(void)

void app_main(void)
{
uint64_t *stack_check = (uint64_t *)((uint32_t)&end + 256);

gpio_set_value(&led_r, 1);
gpio_set_value(&led_g, 1);
printf("\nstart app_main (mdrv)...\n");
*stack_check = 0xababcdcd12123434;

stack_check_init();
load_conf();
debug_init(&dft_ns, &csa.dbg_dst, &csa.dbg_en);
device_init();
Expand Down Expand Up @@ -452,13 +428,17 @@ void app_main(void)
}
}

stack_check();
app_motor_routine();
cdn_routine(&dft_ns); // handle cdnet
common_service_routine();
cali_elec_angle();
//dump_hw_status();
debug_flush(false);

if (*stack_check != 0xababcdcd12123434) {
printf("stack overflow\n");
while (true);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion mdrv_fw/usr/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef __APP_MAIN_H__
#define __APP_MAIN_H__

#include "cdnet_dispatch.h"
#include "cdnet_core.h"
#include "cd_debug.h"
#include "cdbus_uart.h"
#include "cdctl_it.h"
Expand Down Expand Up @@ -222,6 +222,8 @@ extern gpio_t sen_int;
extern cdn_ns_t dft_ns;
extern list_head_t frame_free_head;

extern uint32_t end; // end of bss


inline void encoder_isr(void)
{
Expand Down

0 comments on commit 72c3a16

Please sign in to comment.