-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
+214
−0
Closed
Add CAN rgb led #14710
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||||
* | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a description with |
||||
* @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> | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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