This commit is contained in:
bmy
2024-05-19 16:04:33 +08:00
parent f15fcd4b10
commit f9a87063d8
5 changed files with 440 additions and 287 deletions

142
app/by_servo.c Normal file
View File

@@ -0,0 +1,142 @@
#include "by_servo.h"
#include "at32f415.h"
#include "lwprintf.h"
/**
* @brief 设置云台摇臂
*
* @param angle 角度 (0-180)
*/
void by_servo_set_claw_arm(int16_t angle)
{
#define PULSE_WIDTH_MIN 0.5f
#define PULSE_WIDTH_MAX 2.5f
uint32_t ch_val = 0;
if (angle < 0) {
angle = 0;
} else if (angle > 180) {
angle = 180;
}
ch_val = (uint32_t)(((float)angle / 180.0f * (PULSE_WIDTH_MAX - PULSE_WIDTH_MIN) + PULSE_WIDTH_MIN) / 20.0f * 9999.0f);
tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, ch_val);
#undef PULSE_WIDTH_MIN
#undef PULSE_WIDTH_MAX
}
/**
* @brief 设置云台摇臂
*
* @param angle 角度 (0-180)
*/
void by_servo_set_claw(int16_t angle)
{
#define PULSE_WIDTH_MIN 1.5f
#define PULSE_WIDTH_MAX 2.5f
uint32_t ch_val = 0;
if (angle < 0) {
angle = 0;
} else if (angle > 180) {
angle = 180;
}
ch_val = (uint32_t)(((float)angle / 180.0f * (PULSE_WIDTH_MAX - PULSE_WIDTH_MIN) + PULSE_WIDTH_MIN) / 20.0f * 9999.0f);
tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, ch_val);
#undef PULSE_WIDTH_MIN
#undef PULSE_WIDTH_MAX
}
/**
* @brief 设置云台摇臂
*
* @param angle 角度 (0-180)
*/
void by_servo_set_camera(int16_t angle)
{
#define PULSE_WIDTH_MIN 1.5f
#define PULSE_WIDTH_MAX 2.5f
uint32_t ch_val = 0;
if (angle < 0) {
angle = 0;
} else if (angle > 180) {
angle = 180;
}
ch_val = (uint32_t)(((float)angle / 180.0f * (PULSE_WIDTH_MAX - PULSE_WIDTH_MIN) + PULSE_WIDTH_MIN) / 20.0f * 9999.0f);
tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_4, ch_val);
#undef PULSE_WIDTH_MIN
#undef PULSE_WIDTH_MAX
}
/**
* @brief 设置云台摇臂
*
* @param angle 角度 (0-180)
*/
void by_servo_set_scoop(int16_t angle)
{
#define PULSE_WIDTH_MIN 1.5f
#define PULSE_WIDTH_MAX 2.5f
uint32_t ch_val = 0;
if (angle < 0) {
angle = 0;
} else if (angle > 180) {
angle = 180;
}
ch_val = (uint32_t)(((float)angle / 180.0f * (PULSE_WIDTH_MAX - PULSE_WIDTH_MIN) + PULSE_WIDTH_MIN) / 20.0f * 9999.0f);
tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_3, ch_val);
#undef PULSE_WIDTH_MIN
#undef PULSE_WIDTH_MAX
}
void by_servo_init(void)
{
// 后续要不全初始化为空值,由主控按顺序初始化
by_servo_set_claw_arm(0);
by_servo_set_claw(0);
by_servo_set_camera(0);
by_servo_set_scoop(0);
}
void by_servo_can_handle(uint16_t stdd_id, const uint8_t *data, uint8_t len)
{
int16_t angle = 0;
angle = (int16_t)(data[0] | (data[1] << 8));
switch (stdd_id) {
case 0x009:
by_servo_set_claw_arm(angle);
break;
case 0x00A:
by_servo_set_claw(angle);
break;
case 0x00B:
by_servo_set_camera(angle);
break;
case 0x00C:
by_servo_set_scoop(angle);
break;
default:
break;
}
}

13
app/by_servo.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef _BY_SERVO_H__
#define _BY_SERVO_H__
#include "at32f415.h"
extern void by_servo_set_claw_arm(int16_t angle);
extern void by_servo_set_claw(int16_t angle);
extern void by_servo_set_camera(int16_t angle);
extern void by_servo_set_scoop(int16_t angle);
extern void by_servo_init(void);
extern void by_servo_can_handle(uint16_t stdd_id, const uint8_t *data, uint8_t len);
#endif

View File

@@ -29,7 +29,7 @@
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "by_servo.h"
/* add user code end private includes */
/* private typedef -----------------------------------------------------------*/
@@ -94,8 +94,7 @@ void HardFault_Handler(void)
/* add user code end HardFault_IRQ 0 */
/* go to infinite loop when hard fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_HardFault_IRQ 0 */
/* add user code end W1_HardFault_IRQ 0 */
@@ -113,8 +112,7 @@ void MemManage_Handler(void)
/* add user code end MemoryManagement_IRQ 0 */
/* go to infinite loop when memory manage exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_MemoryManagement_IRQ 0 */
/* add user code end W1_MemoryManagement_IRQ 0 */
@@ -132,8 +130,7 @@ void BusFault_Handler(void)
/* add user code end BusFault_IRQ 0 */
/* go to infinite loop when bus fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_BusFault_IRQ 0 */
/* add user code end W1_BusFault_IRQ 0 */
@@ -151,8 +148,7 @@ void UsageFault_Handler(void)
/* add user code end UsageFault_IRQ 0 */
/* go to infinite loop when usage fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_UsageFault_IRQ 0 */
/* add user code end W1_UsageFault_IRQ 0 */
@@ -212,7 +208,14 @@ void PendSV_Handler(void)
void CAN1_RX0_IRQHandler(void)
{
/* add user code begin CAN1_RX0_IRQ 0 */
if (SET == can_flag_get(CAN1, CAN_RF0MN_FLAG)) {
can_rx_message_type can_rx_message;
can_message_receive(CAN1, CAN_RX_FIFO0, &can_rx_message);
by_servo_can_handle((uint16_t)can_rx_message.standard_id, can_rx_message.data, can_rx_message.dlc);
can_flag_clear(CAN1, CAN_RF0MN_FLAG);
}
/* add user code end CAN1_RX0_IRQ 0 */
/* add user code begin CAN1_RX0_IRQ 1 */

View File

@@ -90,16 +90,14 @@ void wk_system_clock_config(void)
crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
/* wait till lick is ready */
while(crm_flag_get(CRM_LICK_STABLE_FLAG) != SET)
{
while (crm_flag_get(CRM_LICK_STABLE_FLAG) != SET) {
}
/* enable hick */
crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE);
/* wait till hick is ready */
while(crm_flag_get(CRM_HICK_STABLE_FLAG) != SET)
{
while (crm_flag_get(CRM_HICK_STABLE_FLAG) != SET) {
}
/* config pll clock resource */
@@ -109,8 +107,7 @@ void wk_system_clock_config(void)
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
/* wait till pll is ready */
while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
{
while (crm_flag_get(CRM_PLL_STABLE_FLAG) != SET) {
}
/* config ahbclk */
@@ -129,8 +126,7 @@ void wk_system_clock_config(void)
crm_sysclk_switch(CRM_SCLK_PLL);
/* wait till pll is used as system clock source */
while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
{
while (crm_sysclk_switch_status_get() != CRM_SCLK_PLL) {
}
/* disable auto step mode */
@@ -360,7 +356,6 @@ void wk_tmr1_init(void)
tmr_brkdt_struct.deadtime = 0;
tmr_brkdt_config(TMR1, &tmr_brkdt_struct);
tmr_output_enable(TMR1, TRUE);
tmr_counter_enable(TMR1, TRUE);
@@ -488,7 +483,6 @@ void wk_tmr3_init(void)
tmr_output_channel_immediately_set(TMR3, TMR_SELECT_CHANNEL_4, FALSE);
tmr_counter_enable(TMR3, TRUE);
/* add user code begin tmr3_init 2 */
@@ -578,7 +572,7 @@ void wk_can1_init(void)
*/
/* add user code begin can1_init 2 */
can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);
/* add user code end can1_init 2 */
}

View File

@@ -30,6 +30,7 @@
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "by_debug.h"
#include "by_servo.h"
/* add user code end private includes */
/* private typedef -----------------------------------------------------------*/
@@ -102,12 +103,12 @@ int main(void)
/* add user code begin 2 */
delay_init();
by_servo_init();
/* add user code end 2 */
while(1)
{
/* add user code begin 3 */
while (1) {
gpio_bits_write(GPIOA, GPIO_PINS_10, !gpio_output_data_bit_read(GPIOA, GPIO_PINS_10));
delay_ms(1000);
/* add user code end 3 */
}
}