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 beep #14712

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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/beep.cpp
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 please move this above and make sure it is alphabetically ordered?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok


DEPENDS
px4_uavcan_dsdlc
Expand Down
106 changes: 106 additions & 0 deletions src/drivers/uavcan/sensors/beep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/****************************************************************************
*
* Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
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
* Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
* 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 beep.cpp
*
* @author CUAVcaijie <caijie@cuav.net>
*/


#include "beep.hpp"

UavcanUavcanBeep::UavcanUavcanBeep(uavcan::INode &node) :
_beep_pub(node),
_timer(node)
{
}

int
UavcanUavcanBeep::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 this description to the header file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I will modify

*/

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

return 0;
}

void
UavcanUavcanBeep::periodic_update(const uavcan::TimerEvent &)
{
if (_tune_control_sub.updated()) {
_tune_control_sub.copy(&_tune);

if (_tune.timestamp > 0) {
_play_tone = _tunes.set_control(_tune) == 0;
}
}

if ((hrt_absolute_time() - interval_timestamp <= duration)
|| (!_play_tone)) {
return;
}

interval_timestamp = hrt_absolute_time();

if (_silence_length > 0) {
duration = _silence_length;
_silence_length = 0;

} else if (_play_tone) {
int parse_ret_val = _tunes.get_next_note(frequency, duration, _silence_length);

if (parse_ret_val > 0) {
// Continue playing.
_play_tone = true;

if (frequency > 0) {
// Start playing the note.
uavcan::equipment::indication::BeepCommand cmd;
cmd.frequency = frequency;
cmd.duration = duration / 1000000.f;
(void)_beep_pub.broadcast(cmd);
}

} else {
_play_tone = false;
}

}
}
91 changes: 91 additions & 0 deletions src/drivers/uavcan/sensors/beep.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/****************************************************************************
*
* Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
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
* Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
* 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 beep.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 here with @brief?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

* @author CUAVcaijie <caijie@cuav.net>
*/


#pragma once

#include <uavcan/uavcan.hpp>
#include <uavcan/equipment/indication/BeepCommand.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/topics/tune_control.h>
#include <lib/tunes/tunes.h>

/**
* @brief The UavcanUavcanBeep class
*/

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 UavcanUavcanBeep
{
public:
UavcanUavcanBeep(uavcan::INode &node);

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

private:
/*
* Max update rate to avoid exessive bus traffic
*/
static constexpr unsigned MAX_RATE_HZ = 100;
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
static constexpr unsigned MAX_RATE_HZ = 100;
static constexpr unsigned MAX_RATE_HZ = 100;


void periodic_update(const uavcan::TimerEvent &);

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

uORB::Subscription _tune_control_sub{ORB_ID(tune_control)};

/*
* libuavcan related things
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 replace with details on what each thing is doing instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

*/
uavcan::Publisher<uavcan::equipment::indication::BeepCommand> _beep_pub;

uavcan::TimerEventForwarder<TimerCbBinder> _timer;
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
uavcan::TimerEventForwarder<TimerCbBinder> _timer;
uavcan::TimerEventForwarder<TimerCbBinder> _timer;


hrt_abstime interval_timestamp{0};
tune_control_s _tune{};
Tunes _tunes = Tunes();
bool _play_tone{false};
unsigned int _silence_length{0}; ///< If nonzero, silence before next note.
unsigned int frequency{0};
unsigned int duration{0};

};
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),
_beep_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 = _beep_controller.init();

if (ret < 0) {
return ret;
}

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

Expand Down
3 changes: 3 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/beep.hpp"

#include <uavcan/helpers/heap_based_pool_allocator.hpp>
#include <uavcan/protocol/global_time_sync_master.hpp>
Expand Down Expand Up @@ -200,6 +201,8 @@ class UavcanNode : public cdev::CDev, public px4::ScheduledWorkItem, public Modu
UavcanEscController _esc_controller;
UavcanMixingInterface _mixing_interface{_node_mutex, _esc_controller};
UavcanHardpointController _hardpoint_controller;
UavcanUavcanBeep _beep_controller;

uavcan::GlobalTimeSyncMaster _time_sync_master;
uavcan::GlobalTimeSyncSlave _time_sync_slave;
uavcan::NodeStatusMonitor _node_status_monitor;
Expand Down