CARMA C++
Device.h
Go to the documentation of this file.
1 
10 #ifndef CARMA_CANBUS_DEVICE_H
11 #define CARMA_CANBUS_DEVICE_H
12 
13 // C++ STL includes
14 #include <vector>
15 #include <map>
16 #include <string>
17 
18 // Carma includes
19 #include "carma/canbus/CanOutput.h"
20 #include "carma/canbus/Message.h"
21 #include "carma/canbus/Types.h"
22 
23 namespace carma {
24 namespace canbus {
25 
30  typedef ::std::map< ::carma::canbus::msgType, ::std::string > MsgIdInfoMap;
31 
92  class Device {
93  public:
94 
105  Device(apiType api, nodeType node, CanOutput &io);
106 
117  virtual ~Device();
118 
123  apiType getApi() const;
124 
129  nodeType getNode() const;
130 
134  keyType getKey( ) const;
135 
136  static keyType createKey( apiType api, nodeType node );
137 
142  boardType getBoardType() const;
143 
149 
154  busIdType getBusId() const;
155 
160  deviceStateType getState() const;
161 
169  double getLastRxTime() const;
170 
185  unsigned int getNlatePackets() const;
186 
197  char getApiVersion() const;
198 
207  virtual MsgIdInfoMap getControls() const;
208 
223  virtual MsgIdInfoMap getHalfSecMonitors() const = 0;
224 
238  virtual MsgIdInfoMap getSlowMonitors() const = 0;
239 
249  virtual void setState(deviceStateType state);
250 
260  void setBusId(busIdType busId);
261 
266 
270  void setBoardType(boardType bt);
271 
281  virtual void setLastRxTime(double rxMjd);
282 
300  virtual void processMsg(msgType messageId,
301  std::vector<byteType>& data,
302  bool sim) = 0;
303 
328  virtual carma::canbus::Message simulateMsg(msgType messageId) = 0;
329 
340 
350 
359 
369 
370  // Standard Control messages.
380  void reset();
381 
389 
397 
409  void startChannelOneFastSampling(unsigned short fastItem);
410 
422  void startChannelTwoFastSampling(unsigned short fastItem);
423 
440  virtual void updateFrameData();
441 
442  protected:
443 
444  // This is shared data but it never changes. Let inherited classes
445  // use directly if they want.
446  const apiType api_;
447  const nodeType node_;
448  const keyType key_;
449 
450  // Standard controls.
451  // These controls are common message id's shared by all CANbus
452  // Devices. Note that not all devices will implement fast
453  // sampling. Also note that RESET refers to a software reset
454  // message, not a 'hardware reset' which is performed by the
455  // canbus::Dio class.
456 
481  static const msgType RESET = 0x0000;
482  static const msgType STOP_CHANNEL_1_FAST_SAMPLING = 0x0002;
483  static const msgType STOP_CHANNEL_2_FAST_SAMPLING = 0x0003;
484  static const msgType START_CHANNEL_1_FAST_SAMPLING = 0x0004;
485  static const msgType START_CHANNEL_2_FAST_SAMPLING = 0x0005;
486 
487  // Member objects
496 
497  // Helper functions for derived classes
502 
510  void resetLatePacketCount();
511 
527  bool isPacketLate(double window = 100.0);
528 
536  void setApiVersion(char api);
537 
538  private:
539 
540  // Structure to hold shared data and associated mutex.
541  struct {
543  serialNumberType serialNumber;
544  busIdType busId;
545  deviceStateType state;
546  unsigned int nLatePackets;
547  double lastRxMjd;
548  char apiVer;
549  mutable pthread_mutex_t mutex; // Mutable to allow const func access
550  } sharedData_;
551 
552  // Prevent assignment and copy construction
553  Device(const Device &);
554  Device &operator=(const Device &);
555 
556  };
557  } // End namespace canbus
558 } // namespace carma
559 #endif // CARMA_CANBUS_DEVICE_H
unsigned int keyType
Unique key identifier for each device.
Definition: Types.h:68
Declaration of carma::canbus::CanOutput interface.
unsigned int getNlatePackets() const
Get number of late packets.
void setSerialNumber(serialNumberType sn)
Set serial number of device.
unsigned short boardType
Carma Board Type id type.
Definition: Types.h:65
static const msgType START_CHANNEL_1_FAST_SAMPLING
Begin channel 1 fast sampling message id.
Definition: Device.h:484
void setApiVersion(char api)
Set API Version.
Declaration of carma::canbus::Message class.
::std::map< ::carma::canbus::msgType,::std::string > MsgIdInfoMap
Alias for an ::std::map containing a string description of a message id, keyed by the described carma...
Definition: Device.h:30
void setBoardType(boardType bt)
Set board type of device.
virtual MsgIdInfoMap getControls() const
Return a map of devices controls.
Declarations of carma::canbus types.
virtual void updateFrameData()
Update device or monitor data on frame timescale...
Device(apiType api, nodeType node, CanOutput &io)
Device constructor.
void stopChannelOneFastSampling()
Stop fast sampling on channel 1 This routine will stop fast sampling on channel 1 regardless of wheth...
Class to encapsulate a CAN message.
Definition: Message.h:21
virtual MsgIdInfoMap getSlowMonitors() const =0
Return a map of devices slow monitor points.
serialNumberType getSerialNumber() const
Return serial number of device.
char getApiVersion() const
Get API Version of this Device.
bool isPacketLate(double window=100.0)
Determine if the last packet sent from this device was late.
virtual carma::canbus::Message simulateMsg(msgType messageId)=0
Simulate a CAN message.
static const msgType START_CHANNEL_2_FAST_SAMPLING
Begin channel 2 fast sampling message id.
Definition: Device.h:485
virtual MsgIdInfoMap getHalfSecMonitors() const =0
Return a map of devices half second monitor points.
carma::canbus::Message createMsgToHost(msgType messageId) const
Create a CAN message addressed from this Device to the host.
unsigned short apiType
Carma API id type.
Definition: Types.h:64
deviceStateType getState() const
Status access routine.
unsigned short serialNumberType
Carma Serial Number type.
Definition: Types.h:67
busIdType getBusId() const
Return bus Id that node resides on.
boardType getBoardType() const
Return board type id of device.
unsigned short nodeType
Carma Node Type id type.
Definition: Types.h:66
virtual void setLastRxTime(double rxMjd)
Set latest rx time.
void resetLatePacketCount()
Reset the late packet count to 0.
virtual void processMsg(msgType messageId, std::vector< byteType > &data, bool sim)=0
Process a CAN message.
void setBusId(busIdType busId)
Set busId of device.
keyType getKey() const
Create a key unique to this api and node.
void startChannelOneFastSampling(unsigned short fastItem)
Start fast sampling the specified data item on channel 1.
void startChannelTwoFastSampling(unsigned short fastItem)
Start fast sampling the specified data item on channel 2.
carma::canbus::Message createDummyMsg() const
Create a dummy CAN message.
void reset()
Perform a software reset of module.
static const msgType STOP_CHANNEL_1_FAST_SAMPLING
Stop channel 1 fast sampling message id.
Definition: Device.h:482
carma::canbus::Message createMsgToAllNodes(msgType messageId) const
Create a CAN message addressed to all nodes of this type.
Abstract base class for all CAN Bus devices.
Definition: Device.h:92
carma::canbus::Message createMsgToNode(msgType messageId) const
Create a CAN message addressed to this device from the host.
unsigned short busIdType
Carma Bus Id type.
Definition: Types.h:70
nodeType getNode() const
Return node location code of device.
unsigned short msgType
Carma Message id type.
Definition: Types.h:69
double getLastRxTime() const
Get Last RX Time for device.
virtual void setState(deviceStateType state)
Set state of device.
CanOutput interface.
Definition: CanOutput.h:26
CanOutput & io_
Reference to CanOutput object.
Definition: Device.h:495
apiType getApi() const
Return api code of device.
static const msgType RESET
Software reset message id.
Definition: Device.h:481
void stopChannelTwoFastSampling()
Stop fast sampling on channel 2 This routine will stop fast sampling on channel 2 regardless of wheth...
virtual ~Device()
Device destructor.
void incrementLatePacketCount()
Increment the late packet count by 1.