Permalink
Browse files

Merge branch 'hub_module_with_binary' of github.com:SmirnoffYM/AI-sim…

…ulator into hub_module_with_binary
  • Loading branch information...
2 parents d033664 + ced264c commit 47217347dba779e87890e35403c9f9600e37c2b1 @Minoru Minoru committed May 10, 2012
Showing with 137 additions and 37 deletions.
  1. +0 −2 ai-simulator.pro
  2. +22 −10 commodule.cpp
  3. +0 −4 commodule.h
  4. +25 −6 docs/BinaryNetworkProtocol.markdown
  5. +72 −10 hubmodule.cpp
  6. +9 −5 messages.h
  7. +9 −0 robot.h
View
@@ -41,8 +41,6 @@ HEADERS += mainwindow.h \
object.h \
color.h
-LIBS += qjson0.dll
-
FORMS += mainwindow.ui \
robotwindow.ui
View
@@ -88,7 +88,7 @@ void ComModule::handleMessage()
* then put the message into the queue */
msg->num = seq_num;
msg->port = port;
- msg->type = (MessageType)msg_type;
+ msg->type = static_cast<MessageType>(msg_type);
// FIXME: is std::queue thread-safe? Should we use mutex here?
messageQueue->push(msg);
@@ -103,30 +103,42 @@ void ComModule::sendMessage(Message *msg)
QDataStream stream(&datagram, QIODevice::WriteOnly);
// version: 1
- stream << (quint8)1;
+ stream << static_cast<quint8>(1);
// other header infortmation
- stream << (quint32)msg->num << (quint16)msg->port << (quint8)msg->type;
+ stream << static_cast<quint32>(msg->num) << static_cast<quint16>(msg->port)
+ << static_cast<quint8>(msg->type);
switch(msg->type) {
case MsgAcknowledge:
// "acknowledge" doesn't have any payload
break;
+ case MsgStart:
+ break;
+ case MsgPause:
+ break;
+ case MsgStop:
+ break;
case MsgBump:
- stream << (quint32)((MessageBump *)msg)->coordX
- << (quint32)((MessageBump *)msg)->coordY;
+ {
+ MessageBump *m = static_cast<MessageBump *>(msg);
+ stream << static_cast<quint32>(m->coordX) << static_cast<quint32>(m->coordY);
+ };
break;
case MsgThereYouSee:
- MessageThereYouSee *m = (MessageThereYouSee *)msg;
- quint32 count = (quint32)m->objects.size();
+ {
+ MessageThereYouSee *m = static_cast<MessageThereYouSee *>(msg);
+ quint32 count = static_cast<quint32>(m->objects.size());
stream << count;
/* For each object, put its description into the stream */
for(quint32 i = 0; i < count; i++) {
MessageObject o = m->objects.front();
m->objects.pop_front();
- stream << (quint32)o.coordX << (quint32)o.coordY
- << (quint32)o.diameter << (quint32)o.seconds
- << (quint8)o.red << (quint8)o.green << (quint8)o.blue;
+ stream << static_cast<quint32>(o.coordX) << static_cast<quint32>(o.coordY)
+ << static_cast<quint32>(o.diameter) << static_cast<quint32>(o.degrees / 3600)
+ << static_cast<quint8>(o.red) << static_cast<quint8>(o.green)
+ << static_cast<quint8>(o.blue);
}
+ };
break;
}
View
@@ -7,10 +7,6 @@
#include <QtNetwork/QHostAddress>
#include <queue>
-#include <qjson/parser.h>
-#include <qjson/qobjecthelper.h>
-
-
#include "constants.h"
#include "messages.h"
#include "message-parsers.h"
@@ -23,7 +23,7 @@ be either some dull message (see "`acknowledge` message") or answer to
the question that agent asked (see "`there you see` message" and "`bump`
message").
-There are 9 types of messages:
+There are 12 types of messages:
* `move`
* `turn`
@@ -34,9 +34,12 @@ There are 9 types of messages:
* `acknowledge`
* `bump`
* `there you see`
+* `start`
+* `pause`
+* `stop`
-Out of those, only first 6 can be sent by agent, and the last three
-can be sent only by the simulator.
+Out of those, only first 6 can be sent by agent, and the last six can
+be sent only by the simulator.
Following are formal specification of how does header and each message
type look like.
@@ -68,6 +71,9 @@ Message types are mapped from names to numbers as follows:
* 6: `bump`
* 7: `there you see`
* 8: `parameter report`
+* 9: `start`
+* 10: `pause`
+* 11: `stop`
## `move` message
@@ -86,9 +92,9 @@ Message contains:
* seconds, *4 octets*, signed integer
-seconds are 1/60 of minute, and minute is in turn 1/60 of degree. That
-field specify number of seconds agent should turn clockwise. To turn
-counterclockwise, one should specify negative number of seconds.
+second is 1/60 of minute, and minute is, in turn, 1/60 of degree. That
+field specify new orientation of the agent. It is an absolute value
+counted from the north direction, which is at the top of the map.
## `change size` message
@@ -178,6 +184,16 @@ Seconds field indidates orientation of object.
List of objects is just a stream of objects descriptions.
+## `start`, `pause` and `stop` messages
+
+Those are used to control simulation flow. Agent program should start
+doing its work (i.e. moving agent around) upon receiving `start`
+message. If `pause` is sent, agent should pause and wait for the
+`start`. The only difference between `pause` and `stop` is that
+pausing doesn't erase current agent's state, while `stop` does.
+
+Those messages doesn't contain anything other than header.
+
## Example messages
### `move` message
@@ -232,20 +248,23 @@ List of objects is just a stream of objects descriptions.
0x00 0x00 0x00 0x10 -- x coordinate, 16
0x00 0x00 0x00 0x12 -- y coordinate, 18
0x00 0x00 0x00 0x15 -- diameter, 21
+0x00 0x00 0x01 0x68 -- seconds, 360 (one degree)
0x7f -- red component, 127
0xda -- green component, 218
0x3d -- blue component, 61
-- description of second object starts here
0x00 0x00 0x00 0x5d -- x coordinate, 93
0x00 0x00 0x09 0x2a -- y coordinate, 2346
0x00 0x00 0x00 0x03 -- diameter, 3
+0x00 0x01 0xa5 0xe0 -- seconds, 108000 (30 degrees)
0x05 -- red component, 5
0x07 -- green component, 7
0x0b -- blue component, 11
-- description of third object starts here
0x00 0x0d 0xa6 0x3b -- x coordinate, 894523
0x00 0x08 0xa7 0xfb -- y coordinate, 567291
0x00 0x00 0x08 0x6c -- diameter, 2165
+0x00 0x03 0x4b 0xc0 -- seconds, 216000 (60 degrees)
0x5c -- red component, 92
0x41 -- green component, 65
0x04 -- blue component, 4
View
@@ -1,7 +1,6 @@
#include "hubmodule.h"
#include "servant.h"
#include <cmath>
-#include <QDebug>
ModellingSystem * HubModule::modellingSystem = NULL;
double* HubModule::idleTime = NULL;
@@ -33,22 +32,70 @@ void HubModule::refresh()
switch (m->type) {
case MsgMove:
{
- qDebug() << "test";
MessageMove *messageMove = static_cast<MessageMove *>(m);
- HubModule::modellingSystem->getRobotByPort(messageMove->port)
- ->setCoords(messageMove->coordX, messageMove->coordY);
+
+ // check if there'll be no collisions
+ bool collision = false;
+
+ Robot* tmpClientRobot = HubModule::modellingSystem
+ ->getRobotByPort(messageMove->port);
+
+ // check for collisions with robots
+ for (int i = 0; i < ROBOTS; i++) {
+ Robot* tmpRobot = HubModule::modellingSystem->getRobot(i);
+ // if the current robot is the sender robot
+ // then make the next loop
+ if (tmpRobot->getPortNumber() == messageMove->port)
+ continue;
+
+ // check if distance between two points is
+ // bigger then robots size sum
+ if (sqrt(
+ pow(messageMove->coordX
+ - tmpRobot->getCoords().first, 2)
+ +
+ pow(messageMove->coordY
+ - tmpRobot->getCoords().second, 2)
+ ) < (tmpRobot->getSize() + tmpClientRobot->getSize())
+ )
+ collision = true;
+ }
+ // check for collisions with env objects
+ for (int i = 0; i < ROBOTS; i++) {
+ EnvObject* tmpEnvObject = HubModule::modellingSystem->getEnvObject(i);
+ // check if distance between two points is
+ // bigger then robots size sum
+ if (sqrt(
+ pow(messageMove->coordX
+ - tmpEnvObject->getCoords().first, 2)
+ +
+ pow(messageMove->coordY
+ - tmpEnvObject->getCoords().second, 2)
+ ) < (tmpEnvObject->getSize() + tmpClientRobot->getSize())
+ )
+ collision = true;
+ }
+
+ if (!collision)
+ HubModule::modellingSystem->getRobotByPort(messageMove->port)
+ ->setCoords(messageMove->coordX, messageMove->coordY);
+ else {
+ MessageBump *messageBump = new MessageBump();
+ messageBump->port = messageMove->port;
+ // set current robot coords
+ messageBump->coordX = tmpClientRobot->getCoords().first;
+ messageBump->coordY = tmpClientRobot->getCoords().second;
+ comModule->sendMessage(messageBump);
+ }
}
break;
case MsgTurn:
{
MessageTurn *messageTurn = static_cast<MessageTurn *>(m);
- double prevOrientation = HubModule::modellingSystem->getRobotByPort(messageTurn->port)
- ->getOrientation();
-
HubModule::modellingSystem->getRobotByPort(messageTurn->port)
- ->setOrientation(prevOrientation + messageTurn->degrees);
+ ->setOrientation(messageTurn->degrees);
}
break;
@@ -117,7 +164,7 @@ void HubModule::refresh()
// set diameter
messageObject.diameter = robot->getSize();
// set orientation
- messageObject.seconds = robot->getOrientation();
+ messageObject.degrees = robot->getOrientation();
objectsInRange.push_front(messageObject);
@@ -159,18 +206,33 @@ void HubModule::refresh()
// set diameter
messageObject.diameter = envObject->getSize();
// set orientation
- messageObject.seconds = envObject->getOrientation();
+ messageObject.degrees = envObject->getOrientation();
objectsInRange.push_front(messageObject);
// FIXME: messageObject destructor here
}
}
messageThereYouSee->objects = objectsInRange;
+ messageThereYouSee->port = messageWhoIsThere->port;
+ comModule->sendMessage(messageThereYouSee);
// send message to robot
}
break;
+ case MsgParameterReport:
+ {
+ MessageParameterReport *messageParameterReport
+ = static_cast<MessageParameterReport *>(m);
+
+ double paramValue = messageParameterReport->integral + messageParameterReport->real
+ / 1000000;
+
+ HubModule::modellingSystem->getRobotByPort(messageParameterReport->port)
+ ->setParametersByID(messageParameterReport->id, paramValue);
+ }
+ break;
+
}
}
View
@@ -17,7 +17,9 @@ enum MessageType {
MsgBump,
MsgThereYouSee,
MsgParameterReport,
- MsgUndefined
+ MsgStart,
+ MsgPause,
+ MsgStop
};
class Message
@@ -79,17 +81,19 @@ class MessageWhoIsThere : public Message
class MessageParameterReport : public Message
{
public:
- char id;
- unsigned int integral;
- int real;
+ MessageParameterReport() { type = MsgParameterReport; };
+
+ char id;
+ int integral;
+ unsigned int real;
};
/* Excuse me for awkward name - I really couldn't come up with anything better */
struct MessageObject {
char red, green, blue;
unsigned int diameter;
unsigned int coordX, coordY;
- int seconds;
+ double degrees;
};
class MessageThereYouSee : public Message
View
@@ -45,6 +45,15 @@ class Robot : public virtual Object
}
}
+ void setParametersByID(int id, double paramValue)
+ {
+ std::pair<std::string, double> params;
+ params.first = parameters[id].first;
+ params.second = paramValue;
+
+ parameters[id] = params;
+ }
+
unsigned int getPortNumber()
{
return portNumber;

0 comments on commit 4721734

Please sign in to comment.