00001 #include "create/data.h"
00002
00003 #define ADD_PACKET(id,nbytes,info,enabledVersion) if ((enabledVersion) & version) packets[id]=boost::make_shared<Packet>(nbytes,info)
00004
00005 namespace create {
00006
00007 Data::Data(ProtocolVersion version) {
00008
00009
00010
00011
00012
00013 ADD_PACKET(ID_BUMP_WHEELDROP, 1, "bumps_wheeldrops", V_ALL);
00014 ADD_PACKET(ID_WALL, 1, "wall", V_ALL);
00015 ADD_PACKET(ID_CLIFF_LEFT, 1, "cliff_left", V_ALL);
00016 ADD_PACKET(ID_CLIFF_FRONT_LEFT, 1, "cliff_front_left", V_ALL);
00017 ADD_PACKET(ID_CLIFF_FRONT_RIGHT, 1, "cliff_front_right", V_ALL);
00018 ADD_PACKET(ID_CLIFF_RIGHT, 1, "cliff_right", V_ALL);
00019 ADD_PACKET(ID_VIRTUAL_WALL, 1, "virtual_wall", V_ALL);
00020 ADD_PACKET(ID_OVERCURRENTS, 1, "overcurrents", V_1);
00021 ADD_PACKET(ID_DIRT_DETECT_LEFT, 1, "dirt_detect_left", V_ALL);
00022 ADD_PACKET(ID_DIRT_DETECT_RIGHT, 1, "dirt_detect_right", V_1);
00023 ADD_PACKET(ID_IR_OMNI, 1, "ir_opcode", V_ALL);
00024 ADD_PACKET(ID_BUTTONS, 1, "buttons", V_ALL);
00025 ADD_PACKET(ID_DISTANCE, 2, "distance", V_1 | V_2);
00026 ADD_PACKET(ID_ANGLE, 2, "angle", V_1 | V_2);
00027 ADD_PACKET(ID_CHARGE_STATE, 1, "charging_state", V_ALL);
00028 ADD_PACKET(ID_VOLTAGE, 2, "voltage", V_ALL);
00029 ADD_PACKET(ID_CURRENT, 2, "current", V_ALL);
00030 ADD_PACKET(ID_TEMP, 1, "temperature", V_ALL);
00031 ADD_PACKET(ID_CHARGE , 2, "battery_charge", V_ALL);
00032 ADD_PACKET(ID_CAPACITY, 2, "battery_capacity", V_ALL);
00033 ADD_PACKET(ID_OI_MODE, 1, "oi_mode", V_2 | V_3);
00034 ADD_PACKET(ID_LEFT_ENC, 2, "enc_counts_left", V_3);
00035 ADD_PACKET(ID_RIGHT_ENC, 2, "enc_counts_right", V_3);
00036 ADD_PACKET(ID_LIGHT, 1, "light_bumper", V_3);
00037 ADD_PACKET(ID_LIGHT_LEFT, 2, "light_bumper_left", V_3);
00038 ADD_PACKET(ID_LIGHT_FRONT_LEFT, 2, "light_bumper_front_left", V_3);
00039 ADD_PACKET(ID_LIGHT_CENTER_LEFT, 2, "light_bumper_center_left", V_3);
00040 ADD_PACKET(ID_LIGHT_CENTER_RIGHT, 2, "light_bumper_center_right", V_3);
00041 ADD_PACKET(ID_LIGHT_FRONT_RIGHT, 2, "light_bumper_front_right", V_3);
00042 ADD_PACKET(ID_LIGHT_RIGHT, 2, "light_bumper_right", V_3);
00043 ADD_PACKET(ID_IR_LEFT, 1, "ir_opcode_left", V_3);
00044 ADD_PACKET(ID_IR_RIGHT, 1, "ir_opcode_right", V_3);
00045 ADD_PACKET(ID_STASIS, 1, "stasis", V_3);
00046
00047 totalDataBytes = 0;
00048 for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
00049 it != packets.end();
00050 ++it) {
00051 ids.push_back(it->first);
00052 totalDataBytes += it->second->nbytes;
00053 }
00054 }
00055
00056 Data::~Data() { }
00057
00058 bool Data::isValidPacketID(uint8_t id) const {
00059 if (packets.count(id)) {
00060 return true;
00061 }
00062 return false;
00063 }
00064
00065 boost::shared_ptr<Packet> Data::getPacket(uint8_t id) {
00066 if (isValidPacketID(id)) {
00067 return packets[id];
00068 }
00069 return boost::shared_ptr<Packet>();
00070 }
00071
00072 void Data::validateAll() {
00073 for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
00074 it != packets.end();
00075 ++it) {
00076 it->second->validate();
00077 }
00078 }
00079
00080 unsigned int Data::getTotalDataBytes() const {
00081 return totalDataBytes;
00082 }
00083
00084 uint8_t Data::getNumPackets() const {
00085 return packets.size();
00086 }
00087
00088 std::vector<uint8_t> Data::getPacketIDs() {
00089 return ids;
00090 }
00091
00092 }