123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 |
- /*
- * Copyright (C) 2018 dimercur
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
- #ifndef __MESSAGES_H__
- #define __MESSAGES_H__
-
- #include <string>
- #include "img.h"
-
- /**
- * Message ID defined for system communication
- *
- * @brief List of available message ID
- *
- */
- typedef enum {
- //Generic messages
- MESSAGE_EMPTY = 0,
- MESSAGE_LOG,
-
- // Message containing answer (after robot command, or for monitor)
- MESSAGE_ANSWER,
-
- // messages for serial communication with robot
- MESSAGE_OPEN_COM,
- MESSAGE_CLOSE_COM,
-
- // Messages specific to server
- MESSAGE_MONITOR_LOST,
-
- // Messages for camera
- MESSAGE_CAM_OPEN,
- MESSAGE_CAM_CLOSE,
- MESSAGE_ASK_ARENA,
- MESSAGE_ARENA_CONFIRM,
- MESSAGE_ARENA_INFIRM,
- MESSAGE_COMPUTE_POSITION,
- MESSAGE_STOP_COMPUTE_POSITION,
- MESSAGE_POSITION,
- MESSAGE_IMAGE,
-
- // Messages for robot
- MESSAGE_ROBOT_PING,
- MESSAGE_ROBOT_RESET,
- MESSAGE_ROBOT_START_WITH_WD,
- MESSAGE_ROBOT_START_WITHOUT_WD,
- MESSAGE_ROBOT_RELOAD_WD,
- MESSAGE_ROBOT_MOVE,
- MESSAGE_ROBOT_TURN,
- MESSAGE_ROBOT_GO_FORWARD,
- MESSAGE_ROBOT_GO_BACK,
- MESSAGE_ROBOT_GO_LEFT,
- MESSAGE_ROBOT_GO_RIGHT,
- MESSAGE_ROBOT_STOP,
- MESSAGE_ROBOT_POWEROFF,
- MESSAGE_ROBOT_GET_BATTERY,
- MESSAGE_ROBOT_BATTERY_LEVEL,
- MESSAGE_ROBOT_GET_STATE,
- MESSAGE_ROBOT_CURRENT_STATE
- } MessageID;
-
- typedef enum {
- ANSWER_ACK=0,
- ANSWER_NACK,
- ANSWER_LOST_ROBOT,
- ANSWER_ROBOT_TIMEOUT,
- ANSWER_ROBOT_UNKNOWN_COMMAND,
- ANSWER_ROBOT_ERROR,
- ANSWER_ROBOT_CHECKSUM
- } AnswerID;
-
- typedef enum {
- BATTERY_UNKNOWN=-1,
- BATTERY_EMPTY=0,
- BATTERY_LOW,
- BATTERY_FULL
- } BatteryLevel;
-
- typedef enum {
- ROBOT_NOT_BUSY=0,
- ROBOT_BUSY
- } RobotState;
-
- using namespace std;
-
- /**
- * Base class for messaging
- *
- * @brief Base class for messaging
- *
- */
- class Message {
- public:
- /**
- * Create a new, empty message
- */
- Message();
-
- /**
- * Create a new, empty message
- */
- Message(MessageID id);
-
- /**
- * Destroy message
- */
- virtual ~Message();
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- virtual string ToString();
-
- /**
- * Allocate a new mesage and copy contents of current message
- * @return A message, copy of current
- */
- virtual Message* Copy();
-
- /**
- * Get message ID
- * @return Current message ID
- */
- MessageID GetID() {
- return messageID;
- }
-
- /**
- * Set message ID
- * @param id Message ID
- */
- virtual void SetID(MessageID id);
-
- /**
- * Comparison operator
- * @param msg Message to be compared
- * @return true if message are equal, false otherwise
- */
- virtual bool operator==(const Message& msg) {
- return (messageID == msg.messageID);
- }
-
- /**
- * Difference operator
- * @param msg Message to be compared
- * @return true if message are different, false otherwise
- */
- virtual bool operator!=(const Message& msg) {
- return !(messageID == msg.messageID);
- }
-
- protected:
- /**
- * Message identifier (@see MessageID)
- */
- MessageID messageID;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- virtual bool CheckID(MessageID id);
- };
-
-
- /**
- * Message class for holding float value, based on Message class
- *
- * @brief Float message class
- *
- */
- class MessageInt : public Message {
- public:
- /**
- * Create a new, empty float message
- */
- MessageInt();
-
- /**
- * Create a new float message, with given ID and value
- * @param id Message ID
- * @param val Message value
- * @throw std::runtime_error if message ID is incompatible with float data
- */
- MessageInt(MessageID id, int val);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible with float data
- */
- void SetID(MessageID id);
-
- /**
- * Get message value (int)
- * @return int value
- */
- int GetValue() {
- return value;
- }
-
- /**
- * Set message value (int)
- * @param val int value to store in message
- */
- void SetValue(int val) {
- this->value = val;
- }
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new mesage and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- /**
- * Comparison operator
- * @param msg Message to be compared
- * @return true if message are equal, false otherwise
- */
- virtual bool operator==(const MessageInt& msg) {
- return ((messageID == msg.messageID) && (value == msg.value));
- }
-
- /**
- * Difference operator
- * @param msg Message to be compared
- * @return true if message are different, false otherwise
- */
- virtual bool operator!=(const MessageInt& msg) {
- return !((messageID == msg.messageID) && (value == msg.value));
- }
-
- protected:
- /**
- * Message integer value
- */
- int value;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding string value, based on Message class
- *
- * @brief String message class
- *
- */
- class MessageString : public Message {
- public:
- /**
- * Create a new, empty string message
- */
- MessageString();
-
- /**
- * Create a new string message, with given ID and string
- * @param id Message ID
- * @param s Message string
- * @throw std::runtime_error if message ID is incompatible with string data
- */
- MessageString(MessageID id, string s);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible with string data
- */
- void SetID(MessageID id);
-
- /**
- * Get message string
- * @return String
- */
- string GetString() {
- return s;
- }
-
- /**
- * Set message string
- * @param s String to store in message
- */
- void SetString(string s) {
- this->s = s;
- }
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- /**
- * Comparison operator
- * @param msg Message to be compared
- * @return true if message are equal, false otherwise
- */
- virtual bool operator==(const MessageString& msg) {
- return ((messageID == msg.messageID) && (s == msg.s));
- }
-
- /**
- * Difference operator
- * @param msg Message to be compared
- * @return true if message are different, false otherwise
- */
- virtual bool operator!=(const MessageString& msg) {
- return !((messageID == msg.messageID) && (s == msg.s));
- }
- protected:
- /**
- * Message content
- */
- string s;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding image, based on Message class
- *
- * @brief Image message class
- *
- */
- class MessageImg : public Message {
- public:
- /**
- * Create a new, empty image message
- */
- MessageImg();
-
- /**
- * Create a new image message, with given ID and boolean value
- * @param id Message ID
- * @param image Pointer to image
- * @throw std::runtime_error if message ID is incompatible with image message
- */
- MessageImg(MessageID id, Img* image);
-
- /**
- * Destroy Image message
- */
- virtual ~MessageImg();
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible withimage message
- */
- void SetID(MessageID id);
-
- /**
- * Get message image
- * @return Pointer to image
- */
- Img* GetImage() {
- return image;
- }
-
- /**
- * Set message image
- * @param image Pointer to image object
- */
- void SetImage(Img* image);
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- protected:
- /**
- * Message image
- */
- Img* image;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding position, based on Message class
- *
- * @brief Position message class
- *
- */
- class MessagePosition : public Message {
- public:
- /**
- * Create a new, empty image message
- */
- MessagePosition();
-
- /**
- * Create a new image message, with given ID and boolean value
- * @param id Message ID
- * @param pos Position
- * @throw std::runtime_error if message ID is incompatible with image message
- */
- MessagePosition(MessageID id, Position& pos);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible withimage message
- */
- void SetID(MessageID id);
-
- /**
- * Get message image
- * @return Pointer to image
- */
- Position GetPosition() {
- return pos;
- }
-
- /**
- * Set message image
- * @param image Pointer to image object
- */
- void SetPosition(Position& pos);
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- protected:
- /**
- * Message position
- */
- Position pos;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding battery level, based on Message class
- *
- * @brief Position message class
- *
- */
- class MessageBattery : public Message {
- public:
- /**
- * Create a new, empty image message
- */
- MessageBattery();
-
- /**
- * Create a new image message, with given ID and boolean value
- * @param id Message ID
- * @param image Pointer to image
- * @throw std::runtime_error if message ID is incompatible with image message
- */
- MessageBattery(MessageID id, BatteryLevel level);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible withimage message
- */
- void SetID(MessageID id);
-
- /**
- * Get message image
- * @return Pointer to image
- */
- BatteryLevel GetLevel() {
- return level;
- }
-
- /**
- * Set message image
- * @param image Pointer to image object
- */
- void SetLevel(BatteryLevel level);
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- protected:
- /**
- * Message position
- */
- BatteryLevel level;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding answer, based on Message class
- *
- * @brief Answer message class
- *
- */
- class MessageAnswer : public Message {
- public:
- /**
- * Create a new, empty image message
- */
- MessageAnswer();
-
- /**
- * Create a new image message, with given ID and boolean value
- * @param id Message ID
- * @param ans Answer ID
- * @throw std::runtime_error if message ID is incompatible with image message
- */
- MessageAnswer(MessageID id, AnswerID ans);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible withimage message
- */
- void SetID(MessageID id);
-
- /**
- * Get message image
- * @return Pointer to image
- */
- AnswerID GetAnswer() {
- return answer;
- }
-
- /**
- * Set message answer
- * @param ans Answer ID
- */
- void SetAnswer(AnswerID ans);
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- protected:
- /**
- * Message answer
- */
- AnswerID answer;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
-
- /**
- * Message class for holding robot state, based on Message class
- *
- * @brief Answer message class
- *
- */
- class MessageState: public Message {
- public:
- /**
- * Create a new, empty image message
- */
- MessageState();
-
- /**
- * Create a new image message, with given ID and boolean value
- * @param id Message ID
- * @param image Pointer to image
- * @throw std::runtime_error if message ID is incompatible with image message
- */
- MessageState(MessageID id, RobotState state);
-
- /**
- * Set message ID
- * @param id Message ID
- * @throw std::runtime_error if message ID is incompatible withimage message
- */
- void SetID(MessageID id);
-
- /**
- * Get message image
- * @return Pointer to image
- */
- RobotState GetState() {
- return state;
- }
-
- /**
- * Set message image
- * @param image Pointer to image object
- */
- void SetState(RobotState state);
-
- /**
- * Translate content of message into a string that can be displayed
- * @return A string describing message contents
- */
- string ToString();
-
- /**
- * Allocate a new message and copy contents of current message
- * @return A message, copy of current
- */
- Message* Copy();
-
- protected:
- /**
- * Robot state
- */
- RobotState state;
-
- /**
- * Verify if message ID is compatible with current message type
- * @param id Message ID
- * @return true, if message ID is acceptable, false otherwise
- */
- bool CheckID(MessageID id);
- };
- #endif /* __MESSAGES_H__ */
|