canio.h
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2003  John Sweeney & Brian Gerkey
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  */
00020 
00021 #ifndef _CANIO_H_
00022 #define _CANIO_H_
00023 
00024 #define HAVE_STDINT_H 1
00025 
00026 #if HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029 #if HAVE_STDINT_H
00030 #include <stdint.h>
00031 #endif
00032 
00033 #include <sys/types.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <unistd.h>
00037 
00038 // Copied this from <canlib.h>. I assume that it's portable across CAN
00039 // implementations.
00040 #ifndef canMSG_STD
00041 #define canMSG_STD              0x0002
00042 #endif
00043 
00044 
00045 class CanPacket
00046 {
00047 public:
00048         long id;
00049         uint8_t msg[8];
00050         uint32_t dlc;
00051         uint32_t flags;
00052 
00053         CanPacket()
00054         {
00055                 memset(msg,0,sizeof(msg));
00056 
00057                 flags = canMSG_STD;
00058                 dlc = 8;
00059         }
00060 
00061         uint16_t GetSlot(int s)  const
00062         {
00063                 return (uint16_t) ((msg[s*2] << 8) | (msg[s*2+1]));
00064         }
00065 
00066         void PutSlot(const int slot, const uint16_t val)
00067         {
00068                 msg[slot*2] = (val >> 8) & 0xFF;
00069                 msg[slot*2+1] = val & 0xFF;
00070         }
00071 
00072         void PutByte(const int byte, const uint16_t val)
00073         {
00074                 msg[byte] = val & 0xFF;
00075         }
00076 
00077         char* toString()
00078         {
00079                 static char buf[256];
00080                 sprintf(buf, "id:%04lX %02X %02X %02X %02X %02X %02X %02X %02X",
00081                                 id, msg[0], msg[1], msg[2], msg[3], msg[4], msg[5],
00082                                 msg[6], msg[7]);
00083 
00084                 return buf;
00085         }
00086 };
00087 
00088 #define DUALCAN_NR_CHANNELS 2
00089 
00090 /* this class encapsulates the low level CAN stuff.... so it deals
00091    with reading and writing packets on the dual CAN channels.
00092    We make the assumption that we only have to read off of one
00093    channel though (looking at rmi_demo, it appears that this is
00094    OK.)
00095    A higher level entity will make sense of the packets, and call
00096    the read/write methods with the required timing.
00097 
00098    It wouldn't take much to make this an abstract base class so that
00099    the SegwayIO can use it, and then have different CAN hardwares
00100    implement the virtual methods.  So then we can just drop in
00101    a new CAN hardware driver class and everything would still work.
00102    Would also be able to split up the files, so we could keep
00103    canio.[cc,h] in player, and the CAN hardware specific files
00104    can be local.
00105  */
00106 
00107 class DualCANIO
00108 {
00109 public:
00110         DualCANIO() {}
00111         virtual int Init(long channel_freq) = 0;
00112         virtual int ReadPacket(CanPacket *pkt, int channel) = 0;
00113         virtual int WritePacket(CanPacket &pkt) = 0;
00114         virtual int Shutdown() = 0;
00115 };
00116 
00117 #endif

Last updated 25 May 2011 21:17:00