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
Definition: canio.h:45
Definition: canio.h:107