Bratmobile
configurator.h
1 #ifndef CONFIGURATOR_H
2 #define CONFIGURATOR_H
3 #include <dirent.h>
4 #include <thread>
5 #include <filesystem>
6 #include <ncurses.h>
7 #include <fstream>
8 #include <algorithm>
9 #include <random>
10 #include <sys/stat.h>
11 #include "debug.h"
12 #include "planner.h"
13 #include "control_interface.h"
14 #include "task_controller.h"
15 #include "tracker.h"
16 
18 protected:
19  int iteration=0; //represents that hasn't started yet, robot isn't moving and there are no map data
20  Task currentTask; //need to make thread safe?
21  Controller * task_controller=NULL;
22  Tracker * tracker=NULL;
23  LIDAR_In * ci=NULL;
24  Motor_Out * control=NULL;
25  Planner *planner=NULL;
26  Logger * logger=NULL;
27  bool running =0;
28  std::thread * LIDAR_thread=NULL;
29  float simulationStep=2*std::max(ROBOT_HALFLENGTH, ROBOT_HALFWIDTH);
30  std::chrono::high_resolution_clock::time_point previousTimeScan;
31  GoalChanger * goal_changer=NULL;
32  std::vector<vertexDescriptor>m_plan, current_vertices;
33  int bodies=0;
34  Task controlGoal;
35  CoordinateContainer data2fp;
36  TransitionSystem transitionSystem=TransitionSystem(1);
37  WorldBuilder worldBuilder;
38  vertexDescriptor currentVertex=MOVING_VERTEX;
39  edgeDescriptor movingEdge=edgeDescriptor(), currentEdge=edgeDescriptor();
40 
41  public:
42 
43 
44 Configurator(){
45  init();
46 }
47 
48 Configurator(Task _task){
49  init(_task);
50 }
51 
57 void init(Task _task=Task());
58 
65 bool Spawner();
66 
67 int getIteration(){
68  return iteration;
69 }
70 
71 void addIteration(int i=1){
72  iteration+=i;
73 }
74 
75 
76 void dummy_vertex(vertexDescriptor src);
77 
78 simResult simulate(Task, b2World &);
79 
85 
86 
87 void printPlan(std::vector <vertexDescriptor>* p=NULL);
88 
89 
90 
99 std::pair<edgeDescriptor, bool> addVertex(const vertexDescriptor & src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0);
100 
101 //search the TS for a plan
102 //std::vector <vertexDescriptor> planner(TransitionSystem&, vertexDescriptor, vertexDescriptor goal=TransitionSystem::null_vertex(), bool been=0, const Task* custom_ctrl_goal=NULL, bool * finished =NULL) ;
103 
108 virtual void explore_plan(b2World&)=0;
109 
110 //std::vector <vertexDescriptor> back_planner(TransitionSystem&, vertexDescriptor, vertexDescriptor root=0);
111 
112 //starts thread
113 void start();
114 
115 //stops thread
116 void stop();
117 
118 void registerInterface(LIDAR_In *, Motor_Out *);
119 
126 static void run(Configurator *);
127 
128 // //uses LIDAR data to calculate an affine transform of disturbance Di if present
129 // void track_task_execution();
130 
134 void change_task();
135 
136 //updates environment representation with time
141 void update_graph(TransitionSystem& g, const b2Transform & _deltaPose);
142 
143 //round angle to a divisor of PI/2
144 float approximate_angle(const float &, const Direction &, const simResult::resultType &);
145 
146 
147 void adjust_goal_expectation();
148 
149 
150 void register_controller(Controller * controller){
151  task_controller=controller;
152 }
153 
154 Controller* get_controller(){
155  return task_controller;
156 }
157 
158 void register_tracker(Tracker * _tracker){
159  if (!_tracker){return;}
160  tracker=_tracker;
161  tracker->init(&controlGoal);
162 }
163 
164 Tracker * get_tracker()const {
165  return tracker;
166 }
167 
168 Motor_Out * get_motor_interface(){
169  return control;
170 }
171 
172 LIDAR_In * get_lidar_interface(){
173  return ci;
174 }
175 
176 
177 void register_planner(Planner * _p){
178  planner=_p;
179 }
180 
181 void setSimulationStep(float f){
182  simulationStep=f;
183 }
184 
185 void register_logger(Logger * l){
186  logger=l;
187 }
188 
193 static void MulT(const b2Transform& B, Task & task);
194 
199 static void Mul(const b2Transform& B, Task &task);
200 
201 
202 protected:
203 
204 
205 };
206 
213  protected:
214  StateMatcher matcher;
222 ExecutionInfo package_info(vertexDescriptor gv=TransitionSystem::null_vertex(), bool been=false){
223  return ExecutionInfo(currentVertex, gv, currentTask, controlGoal, been, m_plan);
224 }
225 
239 Disturbance getDisturbance(TransitionSystem&g, vertexDescriptor v, b2World & world, const Direction & dir, const b2Transform& start);
240 
252 void backtrack(std::vector <vertexDescriptor>& evaluation_q, std::vector <vertexDescriptor>&priority_q, std::set<vertexDescriptor>& closed, std::vector <vertexDescriptor>& plan_prov, vertexDescriptor module_src=MOVING_VERTEX, vertexDescriptor startRecycle=MOVING_VERTEX);
253 
262 std::vector <vertexDescriptor> splitTask(vertexDescriptor v, Direction d, vertexDescriptor src=TransitionSystem::null_vertex());
263 
272 void propagateD(vertexDescriptor v1, vertexDescriptor v0, std::set<vertexDescriptor>*closed=NULL, StateMatcher::MATCH_TYPE match=StateMatcher::_FALSE);
273 
274 //if in plan the vertex gets priority
275 void planPriority(TransitionSystem&, vertexDescriptor);
276 
277 void adjust_simulated_task(const vertexDescriptor&, TransitionSystem &, Task*);
278 
279 //adjust real-world task
280 void adjust_rw_task(const vertexDescriptor&, TransitionSystem &, Task*, const b2Transform &);
281 
288 std::pair <edgeDescriptor, bool> maxProbability(std::vector<edgeDescriptor> ev, TransitionSystem&);
289 
301 VertexMatch findMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference * _sd=NULL, std::vector <VertexMatch>*other_matches=NULL); //matches to most likely
302 
312 std::vector<vertexDescriptor> explorer(vertexDescriptor v, TransitionSystem&g, b2World &w); //evaluates only after DEFAULT, internal one step lookahead
313 
317 std::pair <bool, Direction> getOppositeDirection(Direction);
318 
322 void resetPhi();
323 
333 std::pair <edgeDescriptor, bool> add_vertex_now(const vertexDescriptor & src, vertexDescriptor & v1, Disturbance obs,Edge edge=Edge(), bool topDown=0);
334 
343 std::pair <edgeDescriptor, bool> add_vertex_retro(vertexDescriptor &src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0);
344 
346 std::vector <Direction> getExploredDirections(vertexDescriptor v, const std::vector<Direction>& directions);
347 
349 void removeExploredTransitions(vertexDescriptor v);
350 
357 void transitionMatrix(vertexDescriptor v, Direction d, vertexDescriptor src);
358 
368 void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector<vertexDescriptor>& plan_prov);
369 
378 void addToPriorityQueue(vertexDescriptor v, std::vector<vertexDescriptor>& queue, const std::set <vertexDescriptor>& closed);
379 
380 
381 //removes singleton vertices and self-edges
382 void ts_cleanup(TransitionSystem &, std::vector <vertexDescriptor>&);
383 
384 //apply affine transformation to states (e.g. if the same situation encountered in the past is reencountered)
385 void shift_states(TransitionSystem &, const std::vector<vertexDescriptor>&, const b2Transform &); //shifts a sequence of states by a certain transform
386 
387 //return vertex from which exploration of the environment starts (explorer)
388 vertexDescriptor get_explore_start(TransitionSystem &);
389 
395 void pre_explore();
396 
397 
398 //std::vector <State> output_plan(const std::vector<vertexDescriptor> &, const TransitionSystem &);
399 
400 void explore_plan(b2World&)override;
401 
412 std::vector<Direction>::iterator get_next_option(vertexDescriptor v, vertexDescriptor src, std::vector<vertexDescriptor> full_plan);
413 
429 bool recycle_plan(vertexDescriptor v, vertexDescriptor &v0, vertexDescriptor & task_start, StateMatcher::MATCH_TYPE &matchType,
430  b2Transform & shift_start, b2Transform& sk_first_start, std::pair<edgeDescriptor, bool>&edge,
431  std::vector<vertexDescriptor> &plan_prov, Direction t_get_direction);
432 
433 
445 std::pair<State, Edge> simulation_setup(b2World& w, Task & t, vertexDescriptor v0, b2Transform shift, b2Transform &start, std::vector<Direction>v0_options);
446 
453 void reassign_direction(vertexDescriptor bestNext, Direction& direction);
454 
462 bool matchToSafe(VertexMatch &match,const std::vector<VertexMatch> &other_matches=std::vector<VertexMatch>());
463 
476 std::pair<edgeDescriptor, bool> setup_match_edge(VertexMatch &match, vertexDescriptor &v0, vertexDescriptor & v1,const Edge& k, Direction direction, bool changedMatch);
477 
484 std::vector <vertexDescriptor> task_vertices(vertexDescriptor v, std::pair<bool, edgeDescriptor>* ep=NULL);
485 
494 std::vector <vertexDescriptor> visitedOrVisitingEdge(const std::vector <edgeDescriptor>& es, TransitionSystem& g, vertexDescriptor cv=TransitionSystem::null_vertex());
495 
504 vertexDescriptor getRecyclingStart(vertexDescriptor v, vertexDescriptor v1, vertexDescriptor taskStart);
505 
511 std::vector <edgeDescriptor> inEdges(vertexDescriptor v, Direction d = UNDEFINED); //returns a vector containing all the in-edges of a vertex which have the specified direction
512 
513 bool closeVertex(std::set<vertexDescriptor> & closed, vertexDescriptor v);
514 
525 std::pair<edgeDescriptor, bool> addEdgeRetrospectively(vertexDescriptor v, vertexDescriptor &v1, const State & s_tmp,std::pair<edgeDescriptor, bool> first_edge, Direction d, float linearSpeed);
526 
537 void correctQueue(std::vector<vertexDescriptor>& queue, vertexDescriptor v, vertexDescriptor startRecycle, int planProvSize);
538 
544 void adjustProbability(const edgeDescriptor &e);
545 
546 public:
547 
549 
551  init(_task);
552 }
553 
554 
555 };
556 
557 
563  protected:
564  void explore_plan(b2World&)override;
565  public:
566 
568 
569  ReactiveConfigurator(Task _task){
570  init(_task);
571 }
572 
573 
574 };
575 
576  #endif
Configurator with long-range planning. It explores transitions out of a state until a DEFAULT Task is...
Definition: configurator.h:212
VertexMatch findMatch(State s, Direction dir=Direction::UNDEFINED, StateMatcher::MATCH_TYPE match_type=StateMatcher::_TRUE, StateDifference *_sd=NULL, std::vector< VertexMatch > *other_matches=NULL)
Searches transition System for a match to a state provided. Continuous states are matched,...
Definition: configurator.cpp:681
std::vector< vertexDescriptor > visitedOrVisitingEdge(const std::vector< edgeDescriptor > &es, TransitionSystem &g, vertexDescriptor cv=TransitionSystem::null_vertex())
Returns a visited edge if present, or if the current.
void reassign_direction(vertexDescriptor bestNext, Direction &direction)
Reassigns direction as the direction of the bext task to expand next in explorer.
Definition: configurator.cpp:983
std::pair< edgeDescriptor, bool > setup_match_edge(VertexMatch &match, vertexDescriptor &v0, vertexDescriptor &v1, const Edge &k, Direction direction, bool changedMatch)
Given a valid match, sets up the edge with the previous vertex Creates new edge if it doesn't exist,...
Definition: configurator.cpp:1012
void propagateD(vertexDescriptor v1, vertexDescriptor v0, std::set< vertexDescriptor > *closed=NULL, StateMatcher::MATCH_TYPE match=StateMatcher::_FALSE)
Propagate a disturbance backwards to all states representing the same task.
Definition: configurator.cpp:336
void pre_explore()
Prepares transitionsystem for exploration: clears all edges of q0 with states that aren't the current...
Definition: configurator.cpp:798
void explore_plan(b2World &) override
Explores and plan.
Definition: configurator.cpp:891
std::pair< edgeDescriptor, bool > add_vertex_now(const vertexDescriptor &src, vertexDescriptor &v1, Disturbance obs, Edge edge=Edge(), bool topDown=0)
Adds state after discovering it in exploration.
Definition: configurator.cpp:45
bool recycle_plan(vertexDescriptor v, vertexDescriptor &v0, vertexDescriptor &task_start, StateMatcher::MATCH_TYPE &matchType, b2Transform &shift_start, b2Transform &sk_first_start, std::pair< edgeDescriptor, bool > &edge, std::vector< vertexDescriptor > &plan_prov, Direction t_get_direction)
Assesses whether a previous plan can be recycled.
Definition: configurator.cpp:941
std::pair< edgeDescriptor, bool > addEdgeRetrospectively(vertexDescriptor v, vertexDescriptor &v1, const State &s_tmp, std::pair< edgeDescriptor, bool > first_edge, Direction d, float linearSpeed)
Adds edge retrospectively (used in split task)
Definition: configurator.cpp:1116
std::pair< bool, Direction > getOppositeDirection(Direction)
Definition: configurator.cpp:103
void backtrack(std::vector< vertexDescriptor > &evaluation_q, std::vector< vertexDescriptor > &priority_q, std::set< vertexDescriptor > &closed, std::vector< vertexDescriptor > &plan_prov, vertexDescriptor module_src=MOVING_VERTEX, vertexDescriptor startRecycle=MOVING_VERTEX)
Iterates through vertices, if they result in crash, it splits the tasks and recalculates evaluation f...
Definition: configurator.cpp:302
std::pair< State, Edge > simulation_setup(b2World &w, Task &t, vertexDescriptor v0, b2Transform shift, b2Transform &start, std::vector< Direction >v0_options)
Sets up for simulation.
Definition: configurator.cpp:973
Disturbance getDisturbance(TransitionSystem &g, vertexDescriptor v, b2World &world, const Direction &dir, const b2Transform &start)
Use attention window to find if any previously avoided obstacle is in the way of the goal,...
Definition: configurator.cpp:113
std::vector< edgeDescriptor > inEdges(vertexDescriptor v, Direction d=UNDEFINED)
Returns a vector of all the in-edges of vertex.
Definition: configurator.cpp:1069
ExecutionInfo package_info(vertexDescriptor gv=TransitionSystem::null_vertex(), bool been=false)
Package task/goal execution details into an Execution Info instance.
Definition: configurator.h:222
void correctQueue(std::vector< vertexDescriptor > &queue, vertexDescriptor v, vertexDescriptor startRecycle, int planProvSize)
Edits.
Definition: configurator.cpp:1130
std::vector< vertexDescriptor > splitTask(vertexDescriptor v, Direction d, vertexDescriptor src=TransitionSystem::null_vertex())
Split tasks into sub-states of fixed length.
Definition: configurator.cpp:250
void transitionMatrix(vertexDescriptor v, Direction d, vertexDescriptor src)
Definition: configurator.cpp:491
std::pair< edgeDescriptor, bool > maxProbability(std::vector< edgeDescriptor > ev, TransitionSystem &)
Return edge with maximum probablitit in a vector.
Definition: configurator.cpp:618
std::pair< edgeDescriptor, bool > add_vertex_retro(vertexDescriptor &src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0)
Adds vertices retroactively (e.g. after a state is split)
Definition: configurator.cpp:53
void removeExploredTransitions(vertexDescriptor v)
Definition: configurator.cpp:484
std::vector< Direction > getExploredDirections(vertexDescriptor v, const std::vector< Direction > &directions)
Definition: configurator.cpp:470
std::vector< Direction >::iterator get_next_option(vertexDescriptor v, vertexDescriptor src, std::vector< vertexDescriptor > full_plan)
Returns an iterator to the next option representing a discrete Task transition. If v is in the plan,...
bool matchToSafe(VertexMatch &match, const std::vector< VertexMatch > &other_matches=std::vector< VertexMatch >())
if the match is a crashed task
Definition: configurator.cpp:995
void addToPriorityQueue(vertexDescriptor v, std::vector< vertexDescriptor > &queue, const std::set< vertexDescriptor > &closed)
Adds vertexDescriptor to priority queue according to a custom heuristic.
Definition: configurator.cpp:600
std::vector< vertexDescriptor > task_vertices(vertexDescriptor v, std::pair< bool, edgeDescriptor > *ep=NULL)
Rerturns all the vertices making up a task.
Definition: configurator.cpp:1021
void adjustProbability(const edgeDescriptor &e)
Adjusts the probability that a continuous state (edge target) will occur after taking a discrete stat...
Definition: configurator.cpp:1139
vertexDescriptor getRecyclingStart(vertexDescriptor v, vertexDescriptor v1, vertexDescriptor taskStart)
Returns the vertex from which to start recycling plan.
Definition: configurator.cpp:1088
void resetPhi()
Resets all vertices evaluation function phi to a default unitialised value of 10.
Definition: configurator.cpp:93
void applyTransitionMatrix(vertexDescriptor v0, Direction d, bool ended, vertexDescriptor src, std::vector< vertexDescriptor > &plan_prov)
Sets permitted transitions out of a state.
Definition: configurator.cpp:551
std::vector< vertexDescriptor > explorer(vertexDescriptor v, TransitionSystem &g, b2World &w)
Constructs transition system using a Box2D simulation combined with an A* graph expansion algorithm.
Definition: configurator.cpp:170
Definition: configurator.h:17
void update_graph(TransitionSystem &g, const b2Transform &_deltaPose)
Definition: configurator.cpp:865
static void run(Configurator *)
Spawns tasks, creates plans, tracks task and controls real-world task switching option to run in thre...
Definition: configurator.cpp:419
std::pair< edgeDescriptor, bool > addVertex(const vertexDescriptor &src, vertexDescriptor &v1, Edge edge=Edge(), bool topDown=0)
Add state to the cognitive map and set next state Task direction and options.
Definition: configurator.cpp:351
bool Spawner()
Calls functions to explore the state space and extract a plan.
Definition: configurator.cpp:63
void estimate_current_vertex()
Definition: configurator.cpp:814
static void MulT(const b2Transform &B, Task &task)
Matrix multiply by transpose.
Definition: configurator.cpp:4
virtual void explore_plan(b2World &)=0
Explores and plan.
static void Mul(const b2Transform &B, Task &task)
Matrix multiplication.
Definition: configurator.cpp:9
void init(Task _task=Task())
Initialises configurator.
Definition: configurator.cpp:16
void change_task()
changes tasks to execute on
Definition: configurator.cpp:849
Callback interface class for implementing custom strategies to switch between tasks to be executed.
Definition: task_controller.h:11
Information about what the configurator is doing (current Task, current vertex), what it wants to do ...
Definition: planner.h:41
Definition: control_interface.h:31
Class used to load data from the configurator.
Definition: debug.h:18
Definition: control_interface.h:42
Searches the transition system and extracts a plan.
Definition: planner.h:88
Implements a Braitenberg vehicle: only simulates one Task at a time.
Definition: configurator.h:562
void explore_plan(b2World &) override
Explores and plan.
Definition: configurator.cpp:917
Definition: graphTools.h:453
Definition: task.h:19
Tracking interface: Bridge between the real world and the simulation. Is used for tracking execution ...
Definition: tracker.h:8
Definition: worldbuilder.h:5
Definition: disturbance.h:112
Edges connecting states in the transition system.
Definition: graphTools.h:40
Definition: control_interface.h:119
Contains the differences between the contiuous components of two hybrid states.
Definition: graphTools.h:142
Hybrid states in the transition system.
Definition: graphTools.h:69
Definition: disturbance.h:268