Army ant simulation
Robot.h
Go to the documentation of this file.
1 
9 #ifndef ROBOT_H_
10 #define ROBOT_H_
11 
12 #include "Box2D/Box2D.h"
13 #include "SFML/Graphics.hpp"
14 #include "SFML/Window.hpp"
15 #include <math.h>
16 
17 #include "constants.h"
18 #include "Config.h"
19 /*----------------------TODOO
20  * create a function to assign a robot to a controller after the robot has been created
21  * */
22 
26 enum e_state {WALK, BRIDGE};
30 enum side {RIGHT, LEFT};
31 
32 
40 class Robot {
41 
42 public:
43  Robot();
44  Robot(b2World* world, config::sRobot robotParameters, double posX, double posY, double angle = 0);
45  virtual ~Robot();
46 
53  void createBody(b2World* world, config::sRobot m_robotParameters, double posX, double posY, double angle = 0);
54 
59  void drawBody(sf::RenderWindow& window, double m_to_pix);
64  void drawJoint(sf::RenderWindow& window, double m_to_px);
69  void drawGripJoint(sf::RenderWindow& window, double m_to_px);
70 
76  void moveBodyWithMotor();
82  void moveBodyWithForce();
88  void moveBodyWithImpulse();
89 
94  void turnOffMotor(side s);
99  void blockMotorRotation(side s);
104  void allowMotorRotation(side s);
110  void limitMotorRotation(side s, double limit_angle_rad);
111 
115  void setState(e_state state);
119  e_state getState();
120 
124  b2Body* getBody();
129  b2Body* getWheel(side s);
130 
135  b2PrismaticJointDef getJointDef(side s);
136 
146  void grip(b2Contact* contact, b2Body* otherBody, double m_to_px);
156  void gripGroundFromPos();
166  bool gripSide(b2Contact* contact, b2Body* otherBody, double m_to_px);
167 
168 
169  bool contactOnGripSide(b2Contact* contact);
170 
175  void setContact(bool contact);
178  bool isContact();
179 
183  void setDelay(int delay);
187  int getDelay();
188 
193  bool checkGripp(side s);
194 
198  bool isGrabbed();
199 
200  //void releaseGrip(b2World* world, b2Joint* gripJointDef);
201 
205  int nbJoint(side s);
209  void incrementNbJoint(side s);
213  void resetNbJoint(side s);
214 
219  b2RevoluteJoint* getMotor(side s);
220 
224  void destroyBody();
225 
229  double getBodyLength();
230 
234  void setSpeed(double speed);
235 
239  bool isReady();
240 
244  bool isMoving();
245 
252  void setId(int id);
253 
257  int getId();
258 
262  b2Vec2 getPosition();
263 
268  double getAngle();
269 
270  side m_movingSide = RIGHT; //Side of the foot that is moving (active motor joint)
271  bool m_ready = false; //Flag to indicate when the robot is ready to move
272  bool m_moving = false; //Flag to indicate that the robot is moving (active motor)
273  //Rq: could be replaced by a test on the value of the robot angular speed
274  bool m_start = true; //Flag to indicate that the robot has just been created (necessary to create the first gripper)
275  bool m_pushing = false; //Flag to indicate that the robot is pushing
276  bool m_isGrabbed = false; //Flag to indicate that the robot is grabbed by another robot
277  double m_referenceAngle = -PI ; //Reference angle of the robot (rad) taken at the previous grip creation
278  double m_referenceAngleJoint = 0 ; //Reference angle of the joint involved in the previous grip creation at the time of the creation
279  //Currently not used but planned as a backup method
280  int m_bridgeAge = 0; //Age of the stable bridge in number of iterations
281  int m_age = 0; //Age of the robot in the bridge state. should be reseted when the robot leave the bridge state
282  int m_pushing_delay = 0; //Pushing delay before which the robot is not able to grip if it didn't run through the limit angle (in number of iterations)
283 
287  b2PrismaticJoint* m_previousGripJoint = nullptr;
288  b2PrismaticJoint* m_currentGripJoint = nullptr;
289 
290 
291 private:
292 
294  double m_angularSpeed = 2*PI; //Angular speed of the robot's motors
295  config::sRobot m_robotParameters; //Robot configuration parameters as defined in Config.h
296 
297  b2Body* m_robotBody; //Pointer on the Box2D body corresponding to the attach between the 2 feet of the robot
298  b2Body* m_leftWheel; //Pointer on the Box2D body corresponding to the left foot of the robot
299  b2Body* m_rightWheel; //Pointer on the Box2D body corresponding to the right foot of the robot
300 
304  b2RevoluteJoint* m_rightWheelMotor;
305  b2RevoluteJoint* m_leftWheelMotor;
306 
307  e_state m_state; //State of the robot. Can be either WALK or BRIDGE
308  bool m_contact = false; //Flag to indicate if the robot is involved in a contact. Reset to false once the contact has been handled
309 
315  b2PrismaticJointDef m_leftGripJointDef; //Box2D definition of the left grip joint
316  b2PrismaticJointDef m_rightGripJointDef;
317 
318  int m_nbJointLeft = 0; //Number of gripper belonging to the left foot
319  int m_nbJointRight = 0; //Number of gripper belonging to the right foot
320 
321  int m_delay = 60; // Delay before the robot can be ready to move (in number of iteration). 60 fps => 60 = 1s de delay
322 
323  int m_id; //Unique id of the robot
324 
325  sf::CircleShape m_shapeRight; //Box2D shape of the right foot
326  sf::CircleShape m_shapeLeft; //Box2D shape of the left foot
327  sf::CircleShape m_shape3; //Backup shape used to draw the joints
328  sf::Vertex m_normal[2]; //Box2D shape used to draw the gripper belonging (black line)
329 
330  double m_M_TO_PX; //Copy of the scale to do the conversion from real dimensions (m) to simulated ones (pixels)
331 };
332 
333 #endif /* ROBOT_H_ */
void drawGripJoint(sf::RenderWindow &window, double m_to_px)
Definition: Robot.cpp:233
side
Definition: Robot.h:30
bool isReady()
Definition: Robot.cpp:1088
bool isGrabbed()
Definition: Robot.cpp:938
bool checkGripp(side s)
Definition: Robot.cpp:902
void drawJoint(sf::RenderWindow &window, double m_to_px)
Definition: Robot.cpp:303
double getBodyLength()
Definition: Robot.cpp:1044
void setDelay(int delay)
Definition: Robot.cpp:1023
int getId()
Definition: Robot.cpp:1053
e_state
Definition: Robot.h:26
double getAngle()
Definition: Robot.cpp:1037
void allowMotorRotation(side s)
Definition: Robot.cpp:564
void setSpeed(double speed)
Definition: Robot.cpp:1030
void blockMotorRotation(side s)
Definition: Robot.cpp:522
bool isMoving()
Definition: Robot.cpp:1085
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...
b2PrismaticJointDef getJointDef(side s)
Definition: Robot.cpp:1056
Definition: Robot.h:40
e_state getState()
Definition: Robot.cpp:1069
b2Body * getWheel(side s)
Definition: Robot.cpp:1072
void moveBodyWithImpulse()
Definition: Robot.cpp:495
void limitMotorRotation(side s, double limit_angle_rad)
Definition: Robot.cpp:543
void createBody(b2World *world, config::sRobot m_robotParameters, double posX, double posY, double angle=0)
Definition: Robot.cpp:55
void resetNbJoint(side s)
Definition: Robot.cpp:1007
void destroyBody()
Definition: Robot.cpp:1093
void grip(b2Contact *contact, b2Body *otherBody, double m_to_px)
Definition: Robot.cpp:575
void incrementNbJoint(side s)
Definition: Robot.cpp:999
void setContact(bool contact)
Definition: Robot.cpp:1020
void setId(int id)
Definition: Robot.cpp:1027
bool isContact()
Definition: Robot.cpp:1082
b2RevoluteJoint * getMotor(side s)
Definition: Robot.cpp:982
bool gripSide(b2Contact *contact, b2Body *otherBody, double m_to_px)
Definition: Robot.cpp:641
void gripGroundFromPos()
Definition: Robot.cpp:729
int getDelay()
Definition: Robot.cpp:1048
void drawBody(sf::RenderWindow &window, double m_to_pix)
Definition: Robot.cpp:142
b2Body * getBody()
Definition: Robot.cpp:1041
int nbJoint(side s)
Definition: Robot.cpp:991
void setState(e_state state)
Definition: Robot.cpp:1033
b2PrismaticJoint * m_previousGripJoint
Definition: Robot.h:287
void turnOffMotor(side s)
Definition: Robot.cpp:513
void moveBodyWithMotor()
Definition: Robot.cpp:442
void moveBodyWithForce()
Definition: Robot.cpp:470
b2Vec2 getPosition()
Definition: Robot.cpp:1065