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_LEFT_ENC, 2, "enc_counts_left", V_3);
00037 ADD_PACKET(ID_RIGHT_ENC, 2, "enc_counts_right", V_3);
00038 ADD_PACKET(ID_LIGHT, 1, "light_bumper", V_3);
00039 ADD_PACKET(ID_LIGHT_LEFT, 2, "light_bumper_left", V_3);
00040 ADD_PACKET(ID_LIGHT_FRONT_LEFT, 2, "light_bumper_front_left", V_3);
00041 ADD_PACKET(ID_LIGHT_CENTER_LEFT, 2, "light_bumper_center_left", V_3);
00042 ADD_PACKET(ID_LIGHT_CENTER_RIGHT, 2, "light_bumper_center_right", V_3);
00043 ADD_PACKET(ID_LIGHT_FRONT_RIGHT, 2, "light_bumper_front_right", V_3);
00044 ADD_PACKET(ID_LIGHT_RIGHT, 2, "light_bumper_right", V_3);
00045 ADD_PACKET(ID_IR_LEFT, 1, "ir_opcode_left", V_3);
00046 ADD_PACKET(ID_IR_RIGHT, 1, "ir_opcode_right", V_3);
00047 ADD_PACKET(ID_STASIS, 1, "stasis", V_3);
00048
00049 totalDataBytes = 0;
00050 for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
00051 it != packets.end();
00052 ++it) {
00053 ids.push_back(it->first);
00054 totalDataBytes += it->second->nbytes;
00055 }
00056 }
00057
00058 Data::~Data() { }
00059
00060 bool Data::isValidPacketID(uint8_t id) const {
00061 if (packets.count(id)) {
00062 return true;
00063 }
00064 return false;
00065 }
00066
00067 boost::shared_ptr<Packet> Data::getPacket(uint8_t id) {
00068 if (isValidPacketID(id)) {
00069 return packets[id];
00070 }
00071 std::cout << "Invalid packet " << (int) id << " requested" << std::endl;
00072 return boost::shared_ptr<Packet>();
00073 }
00074
00075 void Data::validateAll() {
00076 for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
00077 it != packets.end();
00078 ++it) {
00079 it->second->validate();
00080 }
00081 }
00082
00083 unsigned int Data::getTotalDataBytes() const {
00084 return totalDataBytes;
00085 }
00086
00087 uint8_t Data::getNumPackets() const {
00088 return packets.size();
00089 }
00090
00091 std::vector<uint8_t> Data::getPacketIDs() {
00092 return ids;
00093 }
00094
00095 }