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

added template file for nodes #17

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Binary file modified .DS_Store
Binary file not shown.
214 changes: 214 additions & 0 deletions CANReader/CANReaderNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Copyright (c) 2016 PolySync
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* \example CANReaderNode.cpp
*
* PolySync CAN API reader Example.
*
* Shows how to use the CAN API to read CAN frames.
*
* The example uses the standard PolySync node template and state machine.
* Send the SIGINT (control-C on the keyboard) signal to the node/process to do a graceful shutdown.
*
*/

#include <iostream>
#include <memory>

#include <PolySyncNode.hpp>
#include <PolySyncCAN.hpp>
#include <PolySyncDTCException.hpp>
#include <PolySyncDataModel.hpp>


class CANReaderNode : public polysync::Node
{

public:

CANReaderNode( uint channelID )
:
_channel( channelID, _flags )
{
// empty
}

virtual ~CANReaderNode() = default;

protected:

/**
* This function is triggered once when the node has initialized in the
* PolySync context.
*/
virtual void initStateEvent()
{
std::cout << "CANReaderNode::initStateEvent()" << std::endl;

try
{
_channel.setBitRate( _bitRate );

_channel.goOnBus();
}
catch( polysync::DTCException & exception )
{
// If interaction with the channel fails, print why and trigger
// errorStateEvent()
std::cout << exception.what() << std::endl;

activateFault( exception.getDtc(), NODE_STATE_ERROR );
}
}

/**
* Called repeatedly while node is in an operational state. For this
* example, we will read CAN data and print useful information.
*/
virtual void okStateEvent()
{
try
{
// Read data from the device, timeout after one second.
_channel.read( 1000000 );

// Output CAN frame data.
std::cout << "CAN frame - ID: 0x"
<< _channel.getInputFrameId()
<< std::endl;

std::cout << "DLC: "
<< _channel.getInputFramePayloadSize()
<< std::endl << std::endl;
}
catch( polysync::DTCException & exception )
{
auto errorCode = exception.getDtc();

if( exception.getDtc() != DTC_UNAVAILABLE )
{
if( errorCode == DTC_INTR )
{
std::cout << "CAN read was interrupted, application most "
<< "likely received SIGINT (ctrl-c)."
<< std::endl;

disconnectPolySync();

return;
}

std::cout << exception.what() << std::endl;

// Activate a fault state for this node. The NODE_STATE_ERROR
// will trigger call to errorStateEvent.
activateFault( exception.getDtc(), NODE_STATE_ERROR );
}
else
{
std::cout << "Device unavailable. " << std::endl;
}
}

// Sleep for one millisecond
polysync::sleepMicro( 1000 );
}

/**
* If exceptions occurred in @ref okStateEvent or @ref initStateEvent, we
* disconnect, which triggers @ref releaseStateEvent and allows for graceful
* exit.
*/
virtual void errorStateEvent()
{
std::cout << "CANReaderNode::errorStateEvent()" << std::endl;

disconnectPolySync();
}


private:

polysync::CANChannel _channel;

uint _flags{ PSYNC_CAN_OPEN_ALLOW_VIRTUAL };

ps_datarate_kind _bitRate{ DATARATE_500K };

};

/**
* Entry point for this example application
* The "connectPolySync" function begins the node's PolySync execution loop.
*
* @param argc - int, the number of parameters on the command-line
* @param argv - char* [], the parsed command-line arguments
*
* @return int - exit code
*/
int main( int argc, char *argv[] )
{
// Check for shared memory key argument
if( argc > 1 )
{
try
{
if( polysync::getChannelCount() > 0 )
{
CANReaderNode canReader( std::stoul( argv[ 1 ] ) );

canReader.setNodeName( "polysync-can-reader-cpp" );

canReader.connectPolySync();
}
else
{
std::cout << "No available CAN channels." << std::endl;
}
}
catch( std::exception & e )
{
std::cout << "Invalid argument. This example requires valid "
"integer input representing a CAN channel."
<< std::endl
<< "For example: "
"polysync-can-reader-cpp 1"
<< std::endl;

return 1;
}
}
else
{
std::cout << "Must pass CAN channel argument." << std::endl
<< "For example: "
"polysync-can-reader-cpp 1" << std::endl;

return 1;
}

return 0;
}
26 changes: 26 additions & 0 deletions CANReader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required( VERSION 2.8 )

project( polysync-can-reader-cpp )

if( NOT PSYNC_HOME )
if( $ENV{PSYNC_HOME} )
set( PSYNC_HOME $ENV{PSYNC_HOME} )
else()
set( PSYNC_HOME /usr/local/polysync )
endif()
endif()

include( ${PSYNC_HOME}/BuildResources.cmake )

include_directories(
${PSYNC_INCLUDE_DIRS}
)

add_executable(
${PROJECT_NAME}
CANReaderNode.cpp
)

target_link_libraries( ${PROJECT_NAME}
${PSYNC_LIBS}
)
32 changes: 32 additions & 0 deletions CANReader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### CANReader

Using the PolySync state machine, this example does a simple CAN read in the OK state.
This example also acts as a guide for when you need a node to listen to a CAN channel.

### Hardware requirements

- linuxcan-compatible hardware is connected, i.e. a Kvaser Leaf Light

### Dependencies

You need to have a CAN channel to run this example, see: [Connecting To A CAN Sensor](https://help.polysync.io/articles/configuration/runtime-node-configuration/connecting-to-a-can-radar-sensor/)

Packages: libglib2.0-dev

To install on Ubuntu

```bash
sudo apt-get install libglib2.0-dev
```

### Building and running the node

```bash
$ cd CANReader
$ mkdir build && cd build
$ cmake ..
$ make
$ ./polysync-can-reader-cpp 1
```

For more API examples, visit the "Tutorials" and "Development" sections in the PolySync Help Center [here](https://help.polysync.io/articles/).
27 changes: 27 additions & 0 deletions DataGenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required( VERSION 2.8 )

project( polysync-data-generator-cpp )

if( NOT PSYNC_HOME )
if( $ENV{PSYNC_HOME} )
set( PSYNC_HOME $ENV{PSYNC_HOME} )
else()
set( PSYNC_HOME /usr/local/polysync )
endif()
endif()

include( ${PSYNC_HOME}/BuildResources.cmake )

include_directories(
${PSYNC_INCLUDE_DIRS}
)

aux_source_directory( . SRC )

add_executable( ${PROJECT_NAME}
${SRC}
)

target_link_libraries( ${PROJECT_NAME}
${PSYNC_LIBS}
)
Loading