ADTF_DEVICE_TOOLBOX  3.12.1 (ADTF 3.18.3)
Source Code for Demo CAN Coder Generator
Location
./src/examples/src/can_coder_generator/
This example shows:
Header
#pragma once
#include <adtf_filtersdk.h>
#include <adtf_base.h>
#include <adtf_systemsdk.h>
class cCanCoderGenerator : public adtf::filter::cSampleStreamingSource
{
public:
ADTF_CLASS_ID_NAME(cCanCoderGenerator, "demo_can_coder_generator.streaming_source.devicetb.cid","Demo CAN Coder Generator");
ADTF_CLASS_DEPENDENCIES(REQUIRE_INTERFACE(adtf::services::IReferenceClock),
cCanCoderGenerator();
tResult Init() override;
tResult StartStreaming() override;
tResult StopStreaming() override;
private:
adtf::base::property_variable<tUInt> m_nTimer = 10;
adtf::base::property_variable<adtf::util::cString> m_strSignal;
adtf::base::property_variable<tUInt> m_nChannelID = 1;
adtf::base::property_variable<tInt> m_nSignalValue = 0;
adtf::system::kernel_timer m_oLooper;
adtf::ucom::object_ptr<adtf::services::IReferenceClock> m_pClock;
adtf::streaming::ISampleWriter* m_pWriter;
adtf::ucom::object_ptr<adtf::devicetb::sdk::can::axle::ICANSupport> m_pSupport;
adtf::ucom::object_ptr<adtf::devicetb::sdk::can::axle::ICANDatabase> m_pDatabase;
adtf::ucom::object_ptr<adtf::devicetb::sdk::can::axle::ICANCoder> m_pCoder;
tResult SetDatabaseNameProperty(adtf::ucom::object_ptr<adtf::streaming::ant::IStreamType>& pStreamType);
tResult SetNetworkNameProperty(adtf::ucom::object_ptr<adtf::streaming::ant::IStreamType>& pStreamType);
tResult SetChannelNumberProperty(adtf::ucom::object_ptr<adtf::streaming::ant::IStreamType>& pStreamType);
tResult ProcessSample(const adtf::devicetb::sdk::can::axle::tSignalInfo& pInfo);
};
Copyright © CARIAD SE.
Implementation
#include "demo_can_coder_generator.h"
#include <sdk/can_types.h>
using namespace adtf;
ADTF_PLUGIN_VERSION("Can coder generator",
devicetb,
DEVICETB_VERSION_MAJOR,
DEVICETB_VERSION_MINOR,
DEVICETB_VERSION_PATCH,
cCanCoderGenerator)
cCanCoderGenerator::cCanCoderGenerator():
m_pWriter(CreateOutputPin("can_output", devicetb::sdk::can::stream_meta_type_can()))
{
SetDescription("can_output", "The outcoming CAN sample stream containing the configured signal and value.");
m_nTimer.SetDescription("Generation rate in [ms]");
RegisterPropertyVariable("Timer", m_nTimer);
m_strSignal.SetDescription("The name of the signal.");
RegisterPropertyVariable("Signal", m_strSignal);
m_nChannelID.SetDescription("The channel and database which should be used.");
RegisterPropertyVariable("ChannelID", m_nChannelID);
m_nSignalValue.SetDescription("The value for the signal.");
RegisterPropertyVariable("Signalvalue", m_nSignalValue);
SetDescription("Use this Streaming Source to generate CAN data for a specific signal from a CAN database.");
SetHelpLink("$(ADTF_DEVICE_TOOLBOX_DIR)/doc/adtf_device_toolbox_html/page_example_source_can_coder_generator.html");
}
tResult cCanCoderGenerator::Init()
{
RETURN_IF_FAILED(_runtime->GetObject(m_pSupport));
RETURN_IF_FAILED(m_pSupport->GetDatabase(static_cast<tChannelID>(m_nChannelID), m_pDatabase));
RETURN_IF_FAILED(m_pSupport->CreateCoder(m_pCoder));
ucom::object_ptr<streaming::IStreamType> pStreamType = ucom::make_object_ptr<streaming::cStreamType>(devicetb::sdk::can::stream_meta_type_can());
SetDatabaseNameProperty(pStreamType);
SetChannelNumberProperty(pStreamType);
SetNetworkNameProperty(pStreamType);
m_pWriter->ChangeType(pStreamType);
RETURN_IF_FAILED(_runtime->GetObject(m_pClock));
return cSampleStreamingSource::Init();
}
tResult cCanCoderGenerator::StartStreaming()
{
if (m_strSignal->IsEmpty())
{
RETURN_ERROR_DESC(ERR_INVALID_ARG, "Please set a signal name");
}
tSignalID nSgnlId;
const tSignalInfo* pInfo;
RETURN_IF_FAILED(m_pDatabase->GetSignalID(m_strSignal->GetPtr(), &nSgnlId));
m_pDatabase->GetSignalInfo(nSgnlId, &pInfo);
auto strMyName = adtf::streaming::get_named_graph_object_full_name(*this);
m_oLooper = system::kernel_timer(strMyName, m_nTimer * 1000, 0, &cCanCoderGenerator::ProcessSample,this, *pInfo);
return cSampleStreamingSource::StartStreaming();
}
tResult cCanCoderGenerator::StopStreaming()
{
m_oLooper.Stop();
return cSampleStreamingSource::StopStreaming();
}
tResult cCanCoderGenerator::ProcessSample(const tSignalInfo& pInfo)
{
streaming::output_sample_data<tCANData> pSample(m_pClock->GetStreamTime());
pSample->sData = {};
m_pCoder->Begin(pSample.GetDataPtr(), sizeof(tCANData));
m_pCoder->SetMessageInfo(static_cast<tChannelID>(m_nChannelID), pInfo.nMessageId, pInfo.bCANExtended);
tSignalValue oValue;
oValue.nTypeTag = tSignalValue::TAG_FLOAT64;
oValue.f64Value = m_nSignalValue;
m_pCoder->SetSignalValue(pInfo.nSignalID, &oValue);
{
// set the time
tSignalValue oTimeValue;
oTimeValue.nTypeTag = tSignalValue::TAG_UINT64;
oTimeValue.nui64Value = static_cast<tUInt64>(m_pClock->GetStreamTime());
m_pCoder->SetSignalValue(CAN_SIGNALID_HEADER_HWTIME, &oTimeValue);
}
m_pCoder->End();
m_pWriter->Write(pSample.Release());
return m_pWriter->ManualTrigger();
}
tResult cCanCoderGenerator::SetDatabaseNameProperty(ucom::object_ptr<streaming::ant::IStreamType>& pStreamType)
{
return streaming::set_property<util::cString>(*pStreamType.Get(), devicetb::sdk::can::stream_meta_type_can::CanDatabaseName,
"");
}
tResult cCanCoderGenerator::SetNetworkNameProperty(ucom::object_ptr<streaming::ant::IStreamType>& pStreamType)
{
ucom::object_ptr<ICANDatabase> pDatabase;
RETURN_IF_FAILED(m_pCoder->GetDatabase(static_cast<tChannelID>(m_nChannelID), pDatabase));
std::vector<const tChar*> strNames;
RETURN_IF_FAILED(pDatabase->GetNetworkNames(strNames));
return streaming::set_property<util::cString>(*pStreamType.Get(),
devicetb::sdk::can::stream_meta_type_can::CanNetworkName,
strNames[0]);
}
tResult cCanCoderGenerator::SetChannelNumberProperty(ucom::object_ptr<streaming::ant::IStreamType>& pStreamType)
{
return streaming::set_property<util::cString>(*pStreamType.Get(), devicetb::sdk::can::stream_meta_type_can::CanChannelNumber,
std::to_string(m_nChannelID).c_str());
}
Copyright 2024 CARIAD SE.
Namespace for functionality provided by V3.0.0.
axle::tSignalValue tSignalValue
Definition: can_types.h:301
axle::tSignalInfo tSignalInfo
Definition: can_types.h:300
axle::tCANData tCANData
The CAN data structure is used by CAN MediaSamples use following code within your filter:
Definition: can_types.h:294
axle::stream_meta_type_can stream_meta_type_can
Definition of Stream Meta Type CAN Messages.
ADTF - Namespace.
Copyright © CARIAD SE.