Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CAN rgb led #14710

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/drivers/uavcan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ px4_add_module(
sensors/flow.cpp
sensors/gnss.cpp
sensors/mag.cpp
sensors/rgb.cpp

DEPENDS
px4_uavcan_dsdlc
Expand Down
123 changes: 123 additions & 0 deletions src/drivers/uavcan/sensors/rgb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file rgb.cpp
*
* @author CUAVcaijie <[email protected]>
*/

#include "rgb.hpp"
#include <systemlib/err.h>


UavcanRgb::UavcanRgb(uavcan::INode &node) :
_rgb_pub(node),
_timer(node)
{
}

int UavcanRgb::init()
{
/*
* Setup timer and call back function for periodic updates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the description to the header file? Thanks

*/
if (!_timer.isRunning()) {
_timer.setCallback(TimerCbBinder(this, &UavcanRgb::periodic_update));
_timer.startPeriodic(uavcan::MonotonicDuration::fromMSec(1000 / MAX_RATE_HZ));
}

return 0;
}

void UavcanRgb::periodic_update(const uavcan::TimerEvent &)
{
LedControlData led_control_data;

if (_led_controller.update(led_control_data) == 1) {

float _brightness;
uint8_t _r = 0, _g = 0, _b = 0;

switch (led_control_data.leds[0].color) {
case led_control_s::COLOR_RED:
_r = 255; _g = 0; _b = 0;
break;

case led_control_s::COLOR_GREEN:
_r = 0; _g = 255; _b = 0;
break;

case led_control_s::COLOR_BLUE:
_r = 0; _g = 0; _b = 255;
break;

case led_control_s::COLOR_AMBER: // make it the same as yellow
case led_control_s::COLOR_YELLOW:
_r = 255; _g = 255; _b = 0;
break;

case led_control_s::COLOR_PURPLE:
_r = 255; _g = 0; _b = 255;
break;

case led_control_s::COLOR_CYAN:
_r = 0; _g = 255; _b = 255;
break;

case led_control_s::COLOR_WHITE:
_r = 255; _g = 255; _b = 255;
break;

default:
_r = 0; _g = 0; _b = 0;
break;
}

_brightness = (float)led_control_data.leds[0].brightness / 255.f;
_r = _r * _brightness;
_g = _g * _brightness;
_b = _b * _brightness;

uavcan::equipment::indication::LightsCommand msg;
uavcan::equipment::indication::SingleLightCommand cmd;

cmd.light_id = 0;
cmd.color.red = _r >> 3;
cmd.color.green = _g >> 2;
cmd.color.blue = _b >> 3;
msg.commands.push_back(cmd);

(void)_rgb_pub.broadcast(msg);
}
}
81 changes: 81 additions & 0 deletions src/drivers/uavcan/sensors/rgb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file rgb.hpp
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a description with @brief annotation? Thanks

* @author CUAVcaijie <[email protected]>
*
* @brief Control CAN Rgb led by subscribing to led_control
*/

#pragma once

#include <uavcan/uavcan.hpp>
#include <uavcan/equipment/indication/LightsCommand.hpp>
#include <perf/perf_counter.h>
#include <lib/led/led.h>


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

class UavcanRgb
{
public:
UavcanRgb(uavcan::INode &node);

/*
* setup periodic updater
*/
int init();

private:
/*
* Max update rate to avoid exessive bus traffic
*/
static constexpr unsigned MAX_RATE_HZ = 20;

/*
* Setup timer and call back function for periodic updates
*/
void periodic_update(const uavcan::TimerEvent &);

typedef uavcan::MethodBinder<UavcanRgb *, void (UavcanRgb::*)(const uavcan::TimerEvent &)>
TimerCbBinder;
LedController _led_controller;

/*
* Publish CAN Rgb led
*/
uavcan::Publisher<uavcan::equipment::indication::LightsCommand> _rgb_pub;

uavcan::TimerEventForwarder<TimerCbBinder> _timer;
};
7 changes: 7 additions & 0 deletions src/drivers/uavcan/uavcan_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ UavcanNode::UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &sys
_node(can_driver, system_clock, _pool_allocator),
_esc_controller(_node),
_hardpoint_controller(_node),
_rgb_controller(_node),
_time_sync_master(_node),
_time_sync_slave(_node),
_node_status_monitor(_node),
Expand Down Expand Up @@ -621,6 +622,12 @@ UavcanNode::init(uavcan::NodeID node_id, UAVCAN_DRIVER::BusEvent &bus_events)
return ret;
}

ret = _rgb_controller.init();

if (ret < 0) {
return ret;
}

// Sensor bridges
IUavcanSensorBridge::make_all(_node, _sensor_bridges);

Expand Down
2 changes: 2 additions & 0 deletions src/drivers/uavcan/uavcan_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "actuators/esc.hpp"
#include "actuators/hardpoint.hpp"
#include "sensors/sensor_bridge.hpp"
#include "sensors/rgb.hpp"

#include <uavcan/helpers/heap_based_pool_allocator.hpp>
#include <uavcan/protocol/global_time_sync_master.hpp>
Expand Down Expand Up @@ -200,6 +201,7 @@ class UavcanNode : public cdev::CDev, public px4::ScheduledWorkItem, public Modu
UavcanEscController _esc_controller;
UavcanMixingInterface _mixing_interface{_node_mutex, _esc_controller};
UavcanHardpointController _hardpoint_controller;
UavcanRgb _rgb_controller;
uavcan::GlobalTimeSyncMaster _time_sync_master;
uavcan::GlobalTimeSyncSlave _time_sync_slave;
uavcan::NodeStatusMonitor _node_status_monitor;
Expand Down