APM-RGB灯颜色控制-通过增加参数实现

这篇博客详细介绍了如何通过qgroundcontrol或missionplanner软件修改无人机RGBLED的颜色。通过参数RGBFLAG启用或禁用RGB颜色,并可以手动设置RGB-R、RGB-G和RGB-B的值来定制颜色。在AP_Notify库中,根据RGB_LED_FLAG的不同值,实现不同颜色的显示。更新过程中,RGBLed类会根据设定的亮度和颜色序列动态调整LED颜色。
摘要由CSDN通过智能技术生成

效果:qgroundcontrol或者mission planner 的参数里面增加,flag,r,g,b值,
flag=0代表默认变化
flag=1代表手动修改r,g,b颜色值,在无人机中体现
libraries/AP_Notify/AP_Notify.cpp

 // @Param: BUZZ_VOLUME
    // @DisplayName: Buzzer volume
    // @Description: Enable or disable the buzzer.
    // @Range: 0 100
    // @Units: %
    AP_GROUPINFO("BUZZ_VOLUME", 8, AP_Notify, _buzzer_volume, 100),
    
    // @Param: RGBFLAG
    // @DisplayName:RGBFLAG
    // @Description: Enable or disable the RGB.
    // @Values: 0:Disable,1:Enable
    // @User: Advanced
    AP_GROUPINFO("RGBFLAG", 9, AP_Notify, _rgb_led_lib_flag, 0),

    // @Param: RGB-R
    // @DisplayName:RGB-R
    // @Description: the RGB-R.
    // @Range: 0 255
    // @User: Advanced
    AP_GROUPINFO("RGB-R", 10, AP_Notify,_rgb_led_lib_r_value, 3),

    // @Param: RGB-G
    // @DisplayName:RGB-G
    // @Description: the RGB-G.
    // @Range: 0 255
    // @User: Advanced
    AP_GROUPINFO("RGB-G", 11, AP_Notify,_rgb_led_lib_g_value, 3),

    // @Param: RGB-B
    // @DisplayName:RGB-B
    // @Description: the RGB-B.
    // @Range: 0 255
    // @User: Advanced
    AP_GROUPINFO("RGB-B", 12, AP_Notify,_rgb_led_lib_b_value, 3),

libraries/AP_Notify/AP_Notify.h

    // parameters
    AP_Int8 _rgb_led_brightness;
    AP_Int8 _rgb_led_override;
    AP_Int8 _buzzer_enable;
    AP_Int8 _display_type;
    AP_Int8 _oreo_theme;
    AP_Int8 _buzzer_pin;
    AP_Int32 _led_type;
    AP_Int8 _buzzer_level;
    AP_Int8 _buzzer_volume;
    AP_Int8 _rgb_led_lib_flag;
    AP_Int8 _rgb_led_lib_r_value;
    AP_Int8 _rgb_led_lib_g_value;
    AP_Int8 _rgb_led_lib_b_value;

libraries/AP_Notify/RGBLed.cpp

// update - updates led according to timed_updated.  Should be called
// at 50Hz
void RGBLed::update()
{
    uint32_t current_colour_sequence = 0;

    switch (rgb_source()) {
    case mavlink:
        update_override();
        return; // note this is a return not a break!
    case standard:
        current_colour_sequence = get_colour_sequence();
        break;
    case obc:
        current_colour_sequence = get_colour_sequence_obc();
        break;
    case traffic_light:
        current_colour_sequence = get_colour_sequence_traffic_light();
        break;
    }

    const uint8_t brightness = get_brightness();

    uint8_t step = (AP_HAL::millis()/100) % 10;

    // ensure we can't skip a step even with awful timing
    if (step != last_step) {
        step = (last_step+1) % 10;
        last_step = step;
    }

    const uint8_t colour = (current_colour_sequence >> (step*3)) & 7;

    _red_des = (colour & RED) ? brightness : 0;
    _green_des = (colour & GREEN) ? brightness : 0;
    _blue_des = (colour & BLUE) ? brightness : 0;

    
     switch (pNotify->_rgb_led_lib_flag) {
    case 0:
        set_rgb(_red_des, _green_des, _blue_des);
        break;
    case 1:
        set_rgb(pNotify->_rgb_led_lib_r_value,pNotify->_rgb_led_lib_g_value, pNotify->_rgb_led_lib_b_value);
        break;
    case 2:
        set_rgb(RED,0,0);
        break;    
    case 3:
        set_rgb(0,GREEN,0);
        break;  
    case 4:
        set_rgb(0,0,BLUE);
        break;  
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值