canio.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2003 John Sweeney & Brian Gerkey
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef _CANIO_H_
22 #define _CANIO_H_
23 
24 #define HAVE_STDINT_H 1
25 
26 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #if HAVE_STDINT_H
30 #include <stdint.h>
31 #endif
32 
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 
38 // Copied this from <canlib.h>. I assume that it's portable across CAN
39 // implementations.
40 #ifndef canMSG_STD
41 #define canMSG_STD 0x0002
42 #endif
43 
44 
45 class CanPacket
46 {
47 public:
48  long id;
49  uint8_t msg[8];
50  uint32_t dlc;
51  uint32_t flags;
52 
53  CanPacket()
54  {
55  memset(msg,0,sizeof(msg));
56 
57  flags = canMSG_STD;
58  dlc = 8;
59  }
60 
61  uint16_t GetSlot(int s) const
62  {
63  return (uint16_t) ((msg[s*2] << 8) | (msg[s*2+1]));
64  }
65 
66  void PutSlot(const int slot, const uint16_t val)
67  {
68  msg[slot*2] = (val >> 8) & 0xFF;
69  msg[slot*2+1] = val & 0xFF;
70  }
71 
72  void PutByte(const int byte, const uint16_t val)
73  {
74  msg[byte] = val & 0xFF;
75  }
76 
77  char* toString()
78  {
79  static char buf[256];
80  sprintf(buf, "id:%04lX %02X %02X %02X %02X %02X %02X %02X %02X",
81  id, msg[0], msg[1], msg[2], msg[3], msg[4], msg[5],
82  msg[6], msg[7]);
83 
84  return buf;
85  }
86 };
87 
88 #define DUALCAN_NR_CHANNELS 2
89 
90 /* this class encapsulates the low level CAN stuff.... so it deals
91  with reading and writing packets on the dual CAN channels.
92  We make the assumption that we only have to read off of one
93  channel though (looking at rmi_demo, it appears that this is
94  OK.)
95  A higher level entity will make sense of the packets, and call
96  the read/write methods with the required timing.
97 
98  It wouldn't take much to make this an abstract base class so that
99  the SegwayIO can use it, and then have different CAN hardwares
100  implement the virtual methods. So then we can just drop in
101  a new CAN hardware driver class and everything would still work.
102  Would also be able to split up the files, so we could keep
103  canio.[cc,h] in player, and the CAN hardware specific files
104  can be local.
105  */
106 
108 {
109 public:
110  DualCANIO() {}
111  virtual int Init(long channel_freq) = 0;
112  virtual int ReadPacket(CanPacket *pkt, int channel) = 0;
113  virtual int WritePacket(CanPacket &pkt) = 0;
114  virtual int Shutdown() = 0;
115 };
116 
117 #endif
#define PLAYER_WARN1(msg, a)
Error message macros.
Definition: error.h:90
Request/reply: Get/set current source.
Definition: player_interfaces.h:2979
static bool MatchMessage(player_msghdr_t *hdr, int type, int subtype, player_devaddr_t addr)
Helper for message processing.
Definition: message.h:159
#define PLAYER_CAMERA_FORMAT_RGB565
Image format : 16-bit color (5 bits R, 6 bits G, 5 bits B).
Definition: player_interfaces.h:2940
double ReadFloat(int section, const char *name, double value)
Read a floating point (double) value.
Definition: canio.h:45
int AddInterface(player_devaddr_t addr)
Add an interface.
#define PLAYER_CAMERA_REQ_GET_SOURCE
Request/reply subtype: get current source.
Definition: player_interfaces.h:2925
Generic message header.
Definition: player.h:161
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:658
uint32_t compression
Image compression; PLAYER_CAMERA_COMPRESS_RAW indicates no compression.
Definition: player_interfaces.h:2966
#define PLAYER_CAMERA_REQ_GET_IMAGE
Request/reply subtype: get current image frame.
Definition: player_interfaces.h:2931
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:664
uint32_t width
Image dimensions [pixels].
Definition: player_interfaces.h:2953
#define PLAYER_CAMERA_FORMAT_RGB888
Image format : 24-bit color (8 bits R, 8 bits G, 8 bits B).
Definition: player_interfaces.h:2942
Encapsulates a device (i.e., a driver bound to an interface)
Definition: device.h:74
const char * ReadString(int section, const char *name, const char *value)
Read a string value.
uint32_t fdiv
Some images (such as disparity maps) use scaled pixel values; for these images, fdiv specifies the sc...
Definition: player_interfaces.h:2963
virtual void Main(void)=0
Main method for driver thread.
uint32_t image_count
Size of image data as stored in image buffer (bytes)
Definition: player_interfaces.h:2968
int ReadInt(int section, const char *name, int value)
Read an integer value.
#define PLAYER_MSGTYPE_DATA
A data message.
Definition: player.h:95
#define PLAYER_ERROR2(msg, a, b)
Error message macros.
Definition: error.h:83
#define PLAYER_MSGTYPE_RESP_ACK
A positive response message.
Definition: player.h:112
#define PLAYER_CAMERA_DATA_STATE
Data subtype: state.
Definition: player_interfaces.h:2922
uint8_t * image
Compressed image data (byte-aligned, row major order).
Definition: player_interfaces.h:2972
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr *hdr, void *data)
Message handler.
static bool MatchDeviceAddress(player_devaddr_t addr1, player_devaddr_t addr2)
Compare two addresses.
Definition: device.h:201
#define PLAYER_MSGTYPE_REQ
A request message.
Definition: player.h:106
#define PLAYER_MSGTYPE_RESP_NACK
A negative response message.
Definition: player.h:125
int ReadDeviceAddr(player_devaddr_t *addr, int section, const char *name, int code, int index, const char *key)
Read a device id.
Data: state (PLAYER_CAMERA_DATA_STATE)
Definition: player_interfaces.h:2950
int GetTupleCount(int section, const char *name)
Get the number of values in a tuple.
Class for loading configuration file information.
Definition: configfile.h:196
int ReadTupleInt(int section, const char *name, int index, int value)
Read an integer from a tuple field.
A device address.
Definition: player.h:145
An autopointer for the message queue.
Definition: message.h:73
uint32_t format
Image format (must be compatible with depth).
Definition: player_interfaces.h:2959
Definition: v4lcapture.h:99
Definition: v4lframe.h:63
uint32_t norm_count
Norm (one of "NTSC", "PAL", "SECAM", "UNKNOWN").
Definition: player_interfaces.h:2982
void SetError(int code)
Set/reset error code.
Definition: driver.h:145
#define PLAYER_ERROR1(msg, a)
Error message macros.
Definition: error.h:82
#define PLAYER_CAMERA_COMPRESS_RAW
Compression method: raw.
Definition: player_interfaces.h:2945
#define PLAYER_ERROR(msg)
Error message macros.
Definition: error.h:81
Base class for drivers which oeprate with a thread.
Definition: driver.h:552
#define PLAYER_CAMERA_FORMAT_MONO8
Image format : 8-bit monochrome.
Definition: player_interfaces.h:2936
double timestamp
Time associated with message contents (seconds since epoch)
Definition: player.h:170
#define PLAYER_WARN(msg)
Warning message macros.
Definition: error.h:89
Definition: canio.h:107
Base class for all drivers.
Definition: driver.h:108
uint32_t height
Image dimensions [pixels].
Definition: player_interfaces.h:2955
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:76
T max(T a, T b)
Return the maximum of a, b.
Definition: utility.h:126
#define PLAYER_CAMERA_REQ_SET_SOURCE
Request/reply subtype: set current source.
Definition: player_interfaces.h:2928
#define PLAYER_CAMERA_COMPRESS_JPEG
Compression method: jpeg.
Definition: player_interfaces.h:2947
uint32_t bpp
Image bits-per-pixel (8, 16, 24, 32).
Definition: player_interfaces.h:2957