add systick ; issue -- i2c transfer unstable

This commit is contained in:
2025-11-12 21:26:40 +08:00
parent 4dc04bdd7e
commit 79d7fca725
11 changed files with 241 additions and 165 deletions

View File

@@ -1,6 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <ti_msp_dl_config.h>
float y = 0; float y = 0;
float Lowpassfilter_sim(float x) float Lowpassfilter_sim(float x)
@@ -16,14 +16,20 @@ float Lowpassfilter(float Tf, float x)
{ {
float dt = 0.0; float dt = 0.0;
//TODO uint32_t Timesamp = DL_SYSTICK_getValue();
uint32_t Timesamp = 0; if(Timesamp < Last_Timesamp)
//uint32_t Timestamp = SysTick->VAL; {
if(Timesamp < Last_Timesamp) dt = (float)(Last_Timesamp - Timesamp)/9*1e-6; dt = (float)(Last_Timesamp - Timesamp) / 32 * 1e-6;
}
else else
dt = (float)(0xFFFFFF - Timesamp + Last_Timesamp)/9*1e-6; {
dt = (float)(0xFFFFFF - Timesamp + Last_Timesamp) / 32 * 1e-6;
}
if(dt<0.0||dt==0) dt = 0.0015f; if(dt < 0.0 || dt == 0)
{
dt = 0.0015f;
}
else if(dt > 0.005f) else if(dt > 0.005f)
{ {
Last_y = x; Last_y = x;

View File

@@ -78,9 +78,10 @@ void MT6701_iic_read_angel(void)
if(DL_I2C_getControllerStatus(I2C_1_INST) & if(DL_I2C_getControllerStatus(I2C_1_INST) &
DL_I2C_CONTROLLER_STATUS_ERROR) DL_I2C_CONTROLLER_STATUS_ERROR)
{ {
//printf("i2c error \n"); printf("i2c error --------------------------------------------------------------------------------\n");
/* LED will remain high if there is an error */ /* LED will remain high if there is an error */
__BKPT(0); __BKPT(0);
return;
} }
/* Wait for I2C to be Idle */ /* Wait for I2C to be Idle */
@@ -139,10 +140,19 @@ float GetVelocity(void)
{ {
volatile float dt = 0.0; volatile float dt = 0.0;
volatile float Vel_ts = SysTick -> VAL; volatile float Vel_ts = SysTick -> VAL;
if(Vel_ts < Last_Vel_ts) dt = (Last_Vel_ts - Vel_ts)/9*1e-6f; if(Vel_ts < Last_Vel_ts)
else dt = (0xFFFFFF - Vel_ts + Last_Vel_ts)/9*1e-6f; {
dt = (Last_Vel_ts - Vel_ts) / 9 * 1e-6f;
}
else
{
dt = (0xFFFFFF - Vel_ts + Last_Vel_ts) / 9 * 1e-6f;
}
if(dt < 0.0001) dt = 10000; if(dt < 0.0001)
{
dt = 10000;
}
float Vel_Angle = GetAngle(); float Vel_Angle = GetAngle();

View File

@@ -1,5 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include <ti_msp_dl_config.h>
#define limit 6.3 #define limit 6.3
#define Output_ramp 10000 #define Output_ramp 10000
@@ -16,14 +16,20 @@ float Last_Output = 0.0;
float PID_Controller(float Kp, float Ki, float Kd, float Error) float PID_Controller(float Kp, float Ki, float Kd, float Error)
{ {
float Ts = 0.0; float Ts = 0.0;
//TODO uint32_t Timestamp = DL_SYSTICK_getValue();
uint32_t Timestamp = 0; if(Timestamp < Timestamp_Last)
//uint32_t Timestamp = SysTick->VAL; {
if(Timestamp < Timestamp_Last) Ts = (float)(Timestamp_Last - Timestamp)/9*1e-6; Ts = (float)(Timestamp_Last - Timestamp) / 32 * 1e-6;
}
else else
Ts = (0xFFFFFF - Timestamp + Timestamp_Last)/9*1e-6; {
Ts = (0xFFFFFF - Timestamp + Timestamp_Last) / 32 * 1e-6;
}
if(Ts<=0 || Ts > 0.05f) Ts = 0.001; if(Ts <= 0 || Ts > 0.05f)
{
Ts = 0.001;
}
float proportion = Kp * Error;//P环 float proportion = Kp * Error;//P环

View File

@@ -45,6 +45,9 @@ const float num_f = 0.123456f;
volatile uint16_t count = 0 ; volatile uint16_t count = 0 ;
int pp = 7; //电机极对数
int Dir = 1; //电机编码器方向
int main(void) int main(void)
{ {
SYSCFG_DL_init(); SYSCFG_DL_init();
@@ -53,10 +56,12 @@ int main(void)
while (1) { while (1) {
DL_GPIO_togglePins(LED_PORT,LED_PA0_PIN); DL_GPIO_togglePins(LED_PORT,LED_PA0_PIN);
//MT6701_get_angle_degree(); //MT6701_get_angle_degree();
delay_ms(20); delay_ms(50);
volatile float angle_rad = GetAngle(); volatile float angle_rad = GetAngle();
//printf("test \n"); //printf("test \n");
printf("angle %.5f \n" , angle_rad); printf("angle %.5f \n" , angle_rad);
volatile uint32_t tick = DL_SYSTICK_getValue();
printf("systick is %x \n",tick);
PWM_Channel1(count ++); PWM_Channel1(count ++);
if(count == 750) if(count == 750)

View File

@@ -16,6 +16,7 @@ const I2C1 = I2C.addInstance();
const PWM = scripting.addModule("/ti/driverlib/PWM", {}, false); const PWM = scripting.addModule("/ti/driverlib/PWM", {}, false);
const PWM1 = PWM.addInstance(); const PWM1 = PWM.addInstance();
const SYSCTL = scripting.addModule("/ti/driverlib/SYSCTL"); const SYSCTL = scripting.addModule("/ti/driverlib/SYSCTL");
const SYSTICK = scripting.addModule("/ti/driverlib/SYSTICK");
const UART = scripting.addModule("/ti/driverlib/UART", {}, false); const UART = scripting.addModule("/ti/driverlib/UART", {}, false);
const UART1 = UART.addInstance(); const UART1 = UART.addInstance();
@@ -71,6 +72,10 @@ PWM1.ccp2PinConfig.$name = "ti_driverlib_gpio_GPIOPinGeneric6";
SYSCTL.forceDefaultClkConfig = true; SYSCTL.forceDefaultClkConfig = true;
SYSCTL.peripheral.$assign = "SYSCTL"; SYSCTL.peripheral.$assign = "SYSCTL";
SYSTICK.periodEnable = true;
SYSTICK.systickEnable = true;
SYSTICK.period = 16000000;
UART1.$name = "UART_0"; UART1.$name = "UART_0";
UART1.rxFifoThreshold = "DL_UART_RX_FIFO_LEVEL_ONE_ENTRY"; UART1.rxFifoThreshold = "DL_UART_RX_FIFO_LEVEL_ONE_ENTRY";
UART1.targetBaudRate = 115200; UART1.targetBaudRate = 115200;

View File

@@ -449,6 +449,36 @@
<FileType>5</FileType> <FileType>5</FileType>
<FilePath>..\3rd\pwm.h</FilePath> <FilePath>..\3rd\pwm.h</FilePath>
</File> </File>
<File>
<FileName>dfoc.c</FileName>
<FileType>1</FileType>
<FilePath>..\3rd\dfoc.c</FilePath>
</File>
<File>
<FileName>dfoc.h</FileName>
<FileType>5</FileType>
<FilePath>..\3rd\dfoc.h</FilePath>
</File>
<File>
<FileName>lowpass_filter.c</FileName>
<FileType>1</FileType>
<FilePath>..\3rd\lowpass_filter.c</FilePath>
</File>
<File>
<FileName>lowpass_filter.h</FileName>
<FileType>5</FileType>
<FilePath>..\3rd\lowpass_filter.h</FilePath>
</File>
<File>
<FileName>pid_control.c</FileName>
<FileType>1</FileType>
<FilePath>..\3rd\pid_control.c</FilePath>
</File>
<File>
<FileName>pid_control.h</FileName>
<FileType>5</FileType>
<FilePath>..\3rd\pid_control.h</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>

View File

@@ -56,6 +56,7 @@ SYSCONFIG_WEAK void SYSCFG_DL_init(void)
SYSCFG_DL_I2C_1_init(); SYSCFG_DL_I2C_1_init();
SYSCFG_DL_UART_0_init(); SYSCFG_DL_UART_0_init();
SYSCFG_DL_DMA_init(); SYSCFG_DL_DMA_init();
SYSCFG_DL_SYSTICK_init();
/* Ensure backup structures have no valid state */ /* Ensure backup structures have no valid state */
gPWM_0Backup.backupRdy = false; gPWM_0Backup.backupRdy = false;
@@ -93,12 +94,14 @@ SYSCONFIG_WEAK void SYSCFG_DL_initPower(void)
DL_UART_Main_reset(UART_0_INST); DL_UART_Main_reset(UART_0_INST);
DL_GPIO_enablePower(GPIOA); DL_GPIO_enablePower(GPIOA);
DL_GPIO_enablePower(GPIOB); DL_GPIO_enablePower(GPIOB);
DL_TimerA_enablePower(PWM_0_INST); DL_TimerA_enablePower(PWM_0_INST);
DL_I2C_enablePower(I2C_1_INST); DL_I2C_enablePower(I2C_1_INST);
DL_UART_Main_enablePower(UART_0_INST); DL_UART_Main_enablePower(UART_0_INST);
delay_cycles(POWER_STARTUP_DELAY); delay_cycles(POWER_STARTUP_DELAY);
} }
@@ -296,3 +299,11 @@ SYSCONFIG_WEAK void SYSCFG_DL_DMA_init(void){
} }
SYSCONFIG_WEAK void SYSCFG_DL_SYSTICK_init(void)
{
/* Initialize the period to 500.00 ms */
DL_SYSTICK_init(16000000);
/* Enable the SysTick and start counting */
DL_SYSTICK_enable();
}

View File

@@ -153,6 +153,8 @@ extern "C" {
#define LED_PA0_IOMUX (IOMUX_PINCM1) #define LED_PA0_IOMUX (IOMUX_PINCM1)
/* clang-format on */ /* clang-format on */
void SYSCFG_DL_init(void); void SYSCFG_DL_init(void);
@@ -164,6 +166,7 @@ void SYSCFG_DL_I2C_1_init(void);
void SYSCFG_DL_UART_0_init(void); void SYSCFG_DL_UART_0_init(void);
void SYSCFG_DL_DMA_init(void); void SYSCFG_DL_DMA_init(void);
void SYSCFG_DL_SYSTICK_init(void);
bool SYSCFG_DL_saveConfiguration(void); bool SYSCFG_DL_saveConfiguration(void);
bool SYSCFG_DL_restoreConfiguration(void); bool SYSCFG_DL_restoreConfiguration(void);