Skip to content

Commit

Permalink
pwm rudder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Raventhatfly committed Jul 12, 2023
1 parent ce9466b commit 3d4637b
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ add_executable(ut_oled
dev/interface/oled/oled_interface.cpp)
target_include_directories(ut_oled PRIVATE dev/application/unit_tests/ut_oled)

add_executable(ut_rudder
dev/application/unit_tests/ut_rudder/main.cpp
dev/interface/rudder/rudder_interface.cpp)
target_include_directories(ut_rudder PRIVATE dev/interface/rudder
dev/application/unit_tests/ut_rudder)

# Added by Tony Zhang
# Note, this Unit test is deprecated since advance of new sentry
#
Expand Down
29 changes: 29 additions & 0 deletions dev/application/unit_tests/ut_rudder/hardware_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Created by 钱晨 on 3/16/22.
// This file contains some feature enabling masks.
//

#ifndef META_INFANTRY_HARDWARE_CONF_H
#define META_INFANTRY_HARDWARE_CONF_H

#if !defined(ENABLE_VISION) || defined(__DOXYGEN__)
#define ENABLE_VISION FALSE
#endif

#if !defined(ENABLE_REFEREE) || defined(__DOXYGEN__)
#define ENABLE_REFEREE TRUE
#endif

#if !defined(ENABLE_SUBPITCH) || defined(__DOXYGEN__)
#define ENABLE_SUBPITCH FALSE
#endif

#if !defined(ENABLE_CAPACITOR) || defined(__DOXYGEN__)
#define ENABLE_CAPACITOR TRUE
#endif

#if !defined(ENABLE_USB_SHELL) || defined(__DOXYGEN__)
#define ENABLE_USB_SHELL TRUE
#endif

#endif //META_INFANTRY_HARDWARE_CONF_H
34 changes: 34 additions & 0 deletions dev/application/unit_tests/ut_rudder/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Wu Feiyang on 2023/7/11.
//

/**
* @brief This is example code to use the Rudder Interface.
*/

#include "hal.h"
#include "ch.hpp"
#include "rudder_interface.h"


int main(){
halInit();
chibios_rt::System::init();
// nullptr means using the default configuration, which is advised
Rudder rudder1(&PWMD4,nullptr,0,Rudder::MG995);

rudder1.start();
rudder1.set_rudder_angle(90);
while(1){
chThdSleepMilliseconds(2000);
rudder1.set_rudder_angle(0);
chThdSleepMilliseconds(2000);
rudder1.set_rudder_angle(90);
chThdSleepMilliseconds(2000);
rudder1.set_rudder_angle(180);
chThdSleepMilliseconds(2000);
rudder1.set_rudder_angle(90);

}
}

14 changes: 7 additions & 7 deletions dev/board_pin/rm_board_2018_a/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
#define GPIOD_USART3_RX 9U
#define GPIOD_PIN10 10U
#define GPIOD_PIN11 11U
#define GPIOD_LED4 12U
#define RUDDER 12U
#define GPIOD_LED3 13U
#define GPIOD_LED5 14U
#define GPIOD_LED6 15U
Expand Down Expand Up @@ -675,7 +675,7 @@
PIN_MODE_ALTERNATE(GPIOD_USART3_RX) | \
PIN_MODE_INPUT(GPIOD_PIN10) | \
PIN_MODE_INPUT(GPIOD_PIN11) | \
PIN_MODE_OUTPUT(GPIOD_LED4) | \
PIN_MODE_ALTERNATE(RUDDER) | \
PIN_MODE_OUTPUT(GPIOD_LED3) | \
PIN_MODE_OUTPUT(GPIOD_LED5) | \
PIN_MODE_OUTPUT(GPIOD_LED6))
Expand All @@ -691,7 +691,7 @@
PIN_OTYPE_PUSHPULL(GPIOD_USART3_RX) | \
PIN_OTYPE_PUSHPULL(GPIOD_PIN10) | \
PIN_OTYPE_PUSHPULL(GPIOD_PIN11) | \
PIN_OTYPE_PUSHPULL(GPIOD_LED4) | \
PIN_OTYPE_PUSHPULL(RUDDER) | \
PIN_OTYPE_PUSHPULL(GPIOD_LED3) | \
PIN_OTYPE_PUSHPULL(GPIOD_LED5) | \
PIN_OTYPE_PUSHPULL(GPIOD_LED6))
Expand All @@ -707,7 +707,7 @@
PIN_OSPEED_HIGH(GPIOD_USART3_RX) | \
PIN_OSPEED_HIGH(GPIOD_PIN10) | \
PIN_OSPEED_HIGH(GPIOD_PIN11) | \
PIN_OSPEED_HIGH(GPIOD_LED4) | \
PIN_OSPEED_HIGH(RUDDER) | \
PIN_OSPEED_HIGH(GPIOD_LED3) | \
PIN_OSPEED_HIGH(GPIOD_LED5) | \
PIN_OSPEED_HIGH(GPIOD_LED6))
Expand All @@ -723,7 +723,7 @@
PIN_PUPDR_PULLUP(GPIOD_USART3_RX) | \
PIN_PUPDR_PULLUP(GPIOD_PIN10) | \
PIN_PUPDR_PULLUP(GPIOD_PIN11) | \
PIN_PUPDR_FLOATING(GPIOD_LED4) | \
PIN_PUPDR_FLOATING(RUDDER) | \
PIN_PUPDR_FLOATING(GPIOD_LED3) | \
PIN_PUPDR_FLOATING(GPIOD_LED5) | \
PIN_PUPDR_FLOATING(GPIOD_LED6))
Expand All @@ -739,7 +739,7 @@
PIN_ODR_HIGH(GPIOD_USART3_RX) | \
PIN_ODR_HIGH(GPIOD_PIN10) | \
PIN_ODR_HIGH(GPIOD_PIN11) | \
PIN_ODR_LOW(GPIOD_LED4) | \
PIN_ODR_LOW(RUDDER) | \
PIN_ODR_LOW(GPIOD_LED3) | \
PIN_ODR_LOW(GPIOD_LED5) | \
PIN_ODR_LOW(GPIOD_LED6))
Expand All @@ -755,7 +755,7 @@
PIN_AFIO_AF(GPIOD_USART3_RX, 7U) | \
PIN_AFIO_AF(GPIOD_PIN10, 0U) | \
PIN_AFIO_AF(GPIOD_PIN11, 0U) | \
PIN_AFIO_AF(GPIOD_LED4, 0U) | \
PIN_AFIO_AF(RUDDER, 2U) | \
PIN_AFIO_AF(GPIOD_LED3, 0U) | \
PIN_AFIO_AF(GPIOD_LED5, 0U) | \
PIN_AFIO_AF(GPIOD_LED6, 0U))
Expand Down
53 changes: 53 additions & 0 deletions dev/interface/rudder/rudder_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by Wu Feiyang on 2023/7/11.
//
#include "rudder_interface.h"


/**
* @brief Constructor of the rudder
*
* @param[in] driver pointer to a @p PWMDriver object
* @param[in] config pointer to a @p PWMConfig object
* @param[in] channel PWM channel of the rudder. Note that 0 stand for channel 1 and vice versa.
* @param[in] rudderType type of the rudder
*
* @api
*/
Rudder::Rudder(PWMDriver *driver, PWMConfig *config, pwmchannel_t channel,rudderType_t rudderType) {
if(config == nullptr){
config_ = &pwm_default_config;
}else{
config_ = config;
}
driver_ = driver;
channel_ = channel;
rudderType_ = rudderType;
}

Rudder::~Rudder() {
pwmStop(driver_);
}
void Rudder::start() {
pwmStart(driver_,config_);
}

void Rudder::set_rudder_angle(int angle) {
int percentage = 0;
if(rudderType_ == MG995){
/**
* MG995: 0.5ms - 0 degrees
* 1.0ms - 45 degrees
* 1.5ms - 90 degrees
* 2.5ms - 180 degrees
*/
// 180 - 2000
percentage = angle * 2000 / 180 + 500;
}
pwmEnableChannel(driver_,channel_,PWM_PERCENTAGE_TO_WIDTH(driver_, percentage));
}
void Rudder::stop() {
pwmDisableChannel(driver_,channel_);
}


44 changes: 44 additions & 0 deletions dev/interface/rudder/rudder_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Created by Wu Feiyang on 2023/7/11.
//

#ifndef META_RUDDER_INTERFACE_H
#define META_RUDDER_INTERFACE_H
#include "ch.hpp"
#include "hal.h"
#include "hal_pwm.h"

class Rudder{
public:
typedef enum{
MG995,
RUDDER_TYPE_CNT
}rudderType_t;

Rudder(PWMDriver* driver,PWMConfig* config,pwmchannel_t channel, rudderType_t rudderType);
~Rudder();
void start();
void stop();
void set_rudder_angle(int angle);

private:
PWMConfig pwm_default_config = {
10000, // frequency 10000Hz
100, // 10 ms, 1/10000s * 100 = 0.01s = 10ms
nullptr,
{
{PWM_OUTPUT_ACTIVE_HIGH, nullptr}, // it's all CH1 for current support boards
{PWM_COMPLEMENTARY_OUTPUT_DISABLED, nullptr},
{PWM_COMPLEMENTARY_OUTPUT_DISABLED, nullptr},
{PWM_COMPLEMENTARY_OUTPUT_DISABLED, nullptr}
},
0,
0
};
PWMDriver * driver_;
PWMConfig * config_;
pwmchannel_t channel_;
rudderType_t rudderType_;
};

#endif //META_RUDDER_INTERFACE_H

0 comments on commit 3d4637b

Please sign in to comment.