Skip to content
Permalink
Browse files
Get simple adas working, add doctstrings, add right hand rule code
  • Loading branch information
David committed Feb 8, 2023
1 parent aeebb97 commit 45d10e11100f8529fac1359603cddbf1e2249866
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 123 deletions.
@@ -134,24 +134,24 @@ target_link_libraries( example_frame_reader PRIVATE candata status_frames )
#target_link_libraries( example_control PRIVATE candata control_frames status_frames )

add_executable( example_straight_line src/example_straight_line.cpp )
target_link_libraries( example_straight_line PRIVATE simple_adas )
target_link_libraries( example_straight_line PUBLIC simple_adas )


add_executable( example_wiggle src/example_wiggle.cpp )
target_link_libraries( example_wiggle PRIVATE simple_adas )
target_link_libraries( example_wiggle PUBLIC simple_adas )

# demo
add_library( demo_ui src/demo_ui.cpp )
target_include_directories( demo_ui PUBLIC include )
add_library( demo_ui STATIC src/demo_ui.cpp )
target_include_directories( demo_ui PRIVATE include )
target_link_libraries( demo_ui
PRIVATE ftxui::screen
PRIVATE ftxui::dom
PRIVATE ftxui::component
PRIVATE candata adas )
PUBLIC ftxui::screen
PUBLIC ftxui::dom
PUBLIC ftxui::component
PUBLIC candata adas )

add_executable( demo src/demo.cpp )
target_link_libraries( demo
PRIVATE ftxui::screen
PRIVATE ftxui::dom
PRIVATE ftxui::component
PRIVATE candata adas demo_ui )
target_link_libraries( demo demo_ui )
#PRIVATE ftxui::screen
#PRIVATE ftxui::dom
#PRIVATE ftxui::component
#PRIVATE candata adas demo_ui )
@@ -55,9 +55,9 @@ BO_ 1312 VCU2AI_Status: 8 VCU
SG_ BRAKE_PLAUSIBILITY_FAULT : 50|1@1+ (1,0) [0|1] "__F__" Vector__XXX

BO_ 1315 VCU2AI_Steer: 6 VCU
SG_ ANGLE : 0|16@1- (-0.1,0) [-21|21] "deg" Vector__XXX
SG_ ANGLE_MAX : 16|16@1+ (-0.1,0) [0|21] "deg" Vector__XXX
SG_ ANGLE_REQUEST : 32|16@1- (-0.1,0) [-21|21] "deg" Vector__XXX
SG_ ANGLE : 0|16@1- (0.1,0) [-21|21] "deg" Vector__XXX
SG_ ANGLE_MAX : 16|16@1+ (0.1,0) [0|21] "deg" Vector__XXX
SG_ ANGLE_REQUEST : 32|16@1- (0.1,0) [-21|21] "deg" Vector__XXX

BO_ 1313 VCU2AI_Drive_F: 6 VCU
SG_ FRONT_AXLE_TRQ : 0|16@1- (0.1,0) [-195|195] "Nm" Vector__XXX
@@ -4,10 +4,19 @@
#include <control_frames.hpp>
#include <status_frames.hpp>

#include <fcntl.h>
#include <map>

namespace adas_api{
/**
* @brief Quickly define and setup all the status and control frames
* needed for the ADAS system.
*
* @details If you want to have direct access to the individual frames
* then this is the class to use. The individual frames will prevent
* sending dangerous values to the vehicle but you are responsible
* for managing the correct sequence etc to get the vehicle to
* respond correctly.
*/
class Adas
{
protected:
@@ -30,6 +39,12 @@ namespace adas_api{
StatusControlFrame statusControl { this->frontAxle, this->rearAxle, this->status };
SteeringControlFrame steeringControl { this->steering, this->status };

/**
* @brief A map of all the incoming frames.
*
* @details A map is used with the frame ID as the key to allow
* for fast lookup by the read() method.
*/
const std::map<const uint32_t, ProcessFrame* const> statusFrames
{
{ this->frontAxle.frameId, &this->frontAxle },
@@ -41,6 +56,9 @@ namespace adas_api{
{ this->wheelSpeeds.frameId, &this->wheelSpeeds }
};

/**
* @brief An array of all the outgoing frames.
*/
const std::array<ControlFrame* const, 5> controlFrames
{
&this->frontControl,
@@ -50,52 +68,38 @@ namespace adas_api{
&this->steeringControl
};

explicit Adas( const std::string& device ) : _canSocket( can::connect( device ) )
{
// set socket to non-blocking
const int existingFlags = fcntl( this->_canSocket, F_GETFL, 0 );
fcntl( this->_canSocket, F_SETFL, existingFlags | O_NONBLOCK );
}
/**
* @brief Attempt to read the specified number of frames from the
* CAN bus.
*
* @details The Adas class sets up the CAN socket to be non-blocking
* so this method will not block. If there are no frames to read
* then the method will return early.
*
* @param n Number of frames to attempt to read.
*/
virtual void read( const int n=50 );

Adas( const Adas& ) = delete;
virtual ~Adas()
{
try
{
can::close( this->_canSocket );
}
catch( const can::error& )
{}
}
/**
* @brief Write all the control frames to the CAN bus.
*/
virtual void write();

virtual void read( const int n=50 )
{
try
{
for( int i=0; i<n; ++i )
{
const can_frame rawFrame = can::read( this->_canSocket );
/**
* @brief Construct a new Adas object
*
* @param device The name of the CAN device to use.
*/
explicit Adas( const std::string& device );

if( auto statusFrame = this->statusFrames.find( rawFrame.can_id );
statusFrame != this->statusFrames.end() )
{
statusFrame->second->process( rawFrame );
}
}
}
catch( const can::error& )
{
// no more frames to read
}
}
Adas( const Adas& ) = delete;

virtual void write()
{
for( auto controlFrame : this->controlFrames )
{
can::write( this->_canSocket, controlFrame->process() );
}
}
/**
* @brief Destroy the Adas object
*
* @details The destructor will attempt to close the CAN socket.
*/
virtual ~Adas();
};
};

@@ -1,6 +1,6 @@
#ifndef STEERING_CONTROL_FRAME_HPP
#define STEERING_CONTROL_FRAME_HPP
#pragma once

#include "control/control_frame.hpp"
#include "status/steering_frame.hpp"
#include "status/status_frame.hpp"
@@ -9,13 +9,21 @@ namespace adas_api{
class SteeringControlFrame : public ControlFrame
{
public:
/**
* @brief Construct a new Steering Control Frame object
*
* @remarks Will use the same direction rules as the steering frame.
* I.e. if righthandrule is set for steering frame then the
* control frame will also use positive angles for CCW.
*
* @param steeringFrame
* @param statusFrame
*/
SteeringControlFrame( const SteeringFrame &steeringFrame, const StatusFrame &statusFrame );
virtual ~SteeringControlFrame() override = default;

void set_angle( const float angle );

static float steering_angle_limit( const float angle );

private:
/**
* @brief Generate can_frame for transmission.
@@ -11,10 +11,10 @@

#include <adas.hpp>

using namespace ftxui;

namespace demo_ui
{
using namespace ftxui;

struct MenuConfig
{
std::vector<std::string> states;
@@ -185,8 +185,7 @@ namespace demo_ui
return Container::Vertical(
{
Slider( "", &config.value, static_cast<int>( config.minimum*config.scaling ),
static_cast<int>( config.limit*config.scaling ),
static_cast<int>( config.scaling ) ),
static_cast<int>( config.limit*config.scaling ), 1 ),
Renderer( [&]()
{
return hbox(
@@ -5,47 +5,84 @@

#include <string>

namespace adas_api{
class SimpleAdas : protected Adas
{
public:
/** axle control */
inline void set_front_rpm( const float rpm ) { this->frontControl.set_speed( rpm ); };
inline void set_rear_rpm( const float rpm ) { this->rearControl.set_speed( rpm ); };
inline void set_rpm( const float rpm )
{
this->set_front_rpm( rpm );
this->set_rear_rpm( rpm );
}

/** brake control */
inline void set_front_brake( const float percentage ) { this->brakeControl.set_front( percentage ); }
inline void set_rear_brake( const float percentage ) { this->brakeControl.set_rear( percentage ); }
inline void set_brakes( const float percentage ) { this->brakeControl.set_brakes( percentage ); }

/** steering control */
inline void set_angle( const float degrees ) { this->steeringControl.set_angle( degrees ); }

/** status control */
inline bool go() const { return this->status().go(); }
inline void finish() { this->statusControl.set_mission_status( StatusControlFrame::MissionStatus::FINISHED ); }
inline void ebrake( bool b=true ) { this->statusControl.set_estop(b); }

/** status access */
const AxleTorqueFrame& front_axle() const { return this->frontAxle; }
const AxleTorqueFrame& rear_axle() const { return this->rearAxle; }
const BrakeFrame& brakes() const { return static_cast<const Adas*>(this)->brakes; }
const StatusFrame& status() const { return static_cast<const Adas*>(this)->status; }
const SteeringFrame& steering() const { return static_cast<const Adas*>(this)->steering; }
const WheelCountsFrame& wheel_counts() const { return this->wheelCounts; }
const WheelSpeedsFrame& wheel_speeds() const { return this->wheelSpeeds; }

explicit SimpleAdas( const std::string& device );
SimpleAdas( const SimpleAdas& ) = delete;

virtual void read( int n=50 ) override { return Adas::read( n ); }
virtual void write() override;
};
namespace adas_api
{
/**
* @brief A simplified interface to setup and control the ADAS system.
*
* @details This class is a wrapper around the Adas class that provides
* a simplified interface to the ADAS system. It gives complete access
* to all the incoming status information from the vehicle but only
* provides a subset of control commands.
* The class will automatically handle the correct sequence of
* commands to get the vehicle to start moving (e.g. state transitions).
*/
class SimpleAdas : protected Adas
{
public:
// === axle control ===
/**
* @brief Set the front axle target rpm.
*
* @details Torque is automatically set.
*
* @param rpm The target rpm.
*/
void set_front_rpm( const float rpm );

/**
* @brief Set the rear axle target rpm.
*
* @details Torque is automatically set.
*
* @param rpm The target rpm.
*/
void set_rear_rpm( const float rpm );

/**
* @brief Set the target rpm for both axles.
*
* @details Torque is automatically set.
*
* @param rpm The target rpm.
*/
void set_rpm( const float rpm );

/** brake control */
void set_front_brake( const float percentage );
void set_rear_brake( const float percentage );
void set_brakes( const float percentage );

// === steering control ===
/**
* @brief Set the target steering angle.
*
* @param degrees
*
* @warning Steering direction is following ROS convention.
*/
void set_angle( const float degrees );

/** status control */
bool go() const;
void finish();
void ebrake( bool b = true );

/** status access */
const AxleTorqueFrame &front_axle() const;
const AxleTorqueFrame &rear_axle() const;
const BrakeFrame &brakes() const;
const StatusFrame &status() const;
const SteeringFrame &steering() const;
const WheelCountsFrame &wheel_counts() const;
const WheelSpeedsFrame &wheel_speeds() const;

using Adas::Adas;
SimpleAdas( const SimpleAdas & ) = delete;

using Adas::read;
virtual void write() override;
};
}; // namespace adas_api

#endif // SIMPLE_ADAS_HPP
@@ -1,6 +1,6 @@
#ifndef AXLE_TORQUE_FRAME_HPP
#define AXLE_TORQUE_FRAME_HPP
#pragma once

#include "process_frame.hpp"

#include <candata.h>

0 comments on commit 45d10e1

Please sign in to comment.