class mrpt::hwdrivers::CCANBusReader

Overview

This “software driver” reads the frames of a J1939 CAN bus through a serial CAN converter using an ASCII protocol (Lawicel-style commands such as “S5”, “O” and “V”, and frames starting with ‘T’), attached to a standard RS232 serial port (or a USB2SERIAL converter).

Each frame is returned as a mrpt::obs::CObservationCANBusJ1939.

The serial port is opened upon the first call to “doProcess” or “initialize”, so you must call “loadConfig” before this, or manually call “setSerialPort”.

See also the example configuration file for rawlog-grabber in “share/mrpt/config_files/rawlog-grabber”.

 PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS:
-------------------------------------------------------
  [supplied_section_name]
  COM_port_WIN = COM1   // Serial port to connect to
  COM_port_LIN = ttyS0

  COM_baudRate = 57600  // Possible values: 9600, 38400, 57600 (default), 500000
  CANBusSpeed  = 250000 // Speed of the CAN bus, in bits per second: 10000,
                        // 20000, 50000, 100000, 125000, 250000 (default),
                        // 500000, 800000 or 1000000
  useCANReaderTimestamp = false  // (Reserved for future use)
  nTries_connect = 1    // Times to try to detect the baud rate of the
                        // converter (default = 1)
  nTries_command = 4    // Times each setup command is retried before giving
                        // up (default = 4)

See also:

mrpt::obs::CObservationCANBusJ1939

#include <mrpt/hwdrivers/CCANBusReader.h>

class CCANBusReader:
    public mrpt::system::COutputLogger,
    public mrpt::hwdrivers::CGenericSensor
{
public:
    // construction

    CCANBusReader();

    // methods

    void setSerialPort(const std::string& port);
    std::string getSerialPort() const;
    void setBaudRate(int baud);
    int getBaudRate() const;
    void setCANReaderTimeStamping(bool setTimestamp = false);
    bool getCANReaderTimeStamping();
    void setCANReaderSpeed(const unsigned int speed);
    unsigned int getCANReaderSpeed();
    unsigned int getCurrentConnectTry() const;
    void doProcessSimple(bool& outThereIsObservation, mrpt::obs::CObservationCANBusJ1939& outObservation, bool& hardwareError);
    virtual void initialize();
    virtual void doProcess();
};

Inherited Members

public:
    // typedefs

    typedef std::shared_ptr<CGenericSensor> Ptr;
    typedef std::multimap<mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr> TListObservations;
    typedef std::pair<mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr> TListObsPair;

    // enums

    enum TSensorState;

    // structs

    struct TMsg;

    // fields

    bool logging_enable_console_output {true};
    bool logging_enable_keep_record {false};

    // methods

    void logStr(const VerbosityLevel level, std::string_view msg_str) const;
    void logFmt(const VerbosityLevel level, const char* fmt, ...) const;
    void void logCond(const VerbosityLevel level, bool cond, const std::string& msg_str) const;
    void setLoggerName(const std::string& name);
    std::string getLoggerName() const;
    void setVerbosityLevel(const VerbosityLevel level);
    void setVerbosityLevelForCallbacks(const VerbosityLevel level);
    void setMinLoggingLevel(const VerbosityLevel level);
    VerbosityLevel getMinLoggingLevel() const;
    VerbosityLevel getMinLoggingLevelForCallbacks() const;
    bool isLoggingLevelVisible(VerbosityLevel level) const;
    void getLogAsString(std::string& log_contents) const;
    std::string getLogAsString() const;
    void writeLogToFile(const std::optional<std::string>& fname_in = std::nullopt) const;
    void dumpLogToConsole() const;
    std::string getLoggerLastMsg() const;
    void getLoggerLastMsg(std::string& msg_str) const;
    void loggerReset();
    void logRegisterCallback(output_logger_callback_t userFunc);
    bool logDeregisterCallback(output_logger_callback_t userFunc);
    COutputLogger& operator = (const COutputLogger&);
    COutputLogger& operator = (COutputLogger&&);
    static std::array<mrpt::system::ConsoleForegroundColor, NUMBER_OF_VERBOSITY_LEVELS>& logging_levels_to_colors();
    static const std::array<const char*, NUMBER_OF_VERBOSITY_LEVELS>& logging_levels_to_names();
    virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const = 0;
    TSensorState getState() const;
    double getProcessRate() const;
    std::string getSensorLabel() const;
    void setSensorLabel(const std::string& sensorLabel);
    void enableVerbose(bool enabled = true);
    bool isVerboseEnabled() const;
    CGenericSensor& operator = (const CGenericSensor&);
    virtual void loadConfig(const mrpt::config::CConfigFileBase& configSource, const std::string& section);
    virtual void initialize();
    virtual void doProcess() = 0;
    virtual TListObservations getObservations();
    virtual void setPathForExternalImages(] const std::string& directory);
    virtual void setExternalImageFormat(const std::string& ext);
    virtual void setExternalImageJPEGQuality(const unsigned int quality);
    virtual unsigned int getExternalImageJPEGQuality() const;
    static void registerClass(const TSensorClassId* pNewClass);
    static CGenericSensor* createSensor(const std::string& className);
    static Ptr createSensorPtr(const std::string& className);

Construction

CCANBusReader()

Constructor

Methods

void setSerialPort(const std::string& port)

Changes the serial port to connect to (call prior to ‘doProcess’), for example “COM1” or “ttyS0”.

This is not needed if the configuration is loaded with “loadConfig”.

std::string getSerialPort() const

See also:

setSerialPort

void setBaudRate(int baud)

Changes the serial port baud rate (call prior to ‘doProcess’); valid values are 9600,38400 and 500000.

This is not needed if the configuration is loaded with “loadConfig”.

See also:

getBaudRate

int getBaudRate() const

See also:

setBaudRate

void setCANReaderTimeStamping(bool setTimestamp = false)

Enables/Disables the addition of a timestamp according to the arrival time to the converter (default=false) (call prior to ‘doProcess’) This is not needed if the configuration is loaded with “loadConfig”.

void setCANReaderSpeed(const unsigned int speed)

Sets the CAN reader speed when connecting to the CAN Bus.

unsigned int getCurrentConnectTry() const

If performing several tries in initialize(), this is the current try loop number.

void doProcessSimple(
    bool& outThereIsObservation,
    mrpt::obs::CObservationCANBusJ1939& outObservation,
    bool& hardwareError
    )

Specific laser scanner “software drivers” must process here new data from the I/O stream, and, if a whole scan has arrived, return it.

This method will be typically called in a different thread than other methods, and will be called in a timely fashion.

virtual void initialize()

Set-up communication with the laser.

Called automatically by rawlog-grabber. If used manually, call after “loadConfig” and before “doProcess”.

In this class this method does nothing, since the communications are setup at the first try from “doProcess” or “doProcessSimple”.

virtual void doProcess()

This method will be invoked at a minimum rate of “process_rate” (Hz)

Parameters:

This

method must throw an exception with a descriptive message if some critical error is found.