Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / WH_Robot.cpp @ afa9104d

History | View | Annotate | Download (5.06 KB)

1
#include "WH_Robot.h"
2
#include "../helper_classes/Order.h"
3

    
4
/** @Brief: warehouse robot constructor **/
5
WH_Robot::WH_Robot(std::string scoutname):line_follow(scoutname) 
6
{
7
  nav_map = new navigationMap(scoutname);
8
  curr_task = DEFAULT_TASK;
9
  name = scoutname;
10
  reg_failed = 1;
11
}
12

    
13
WH_Robot::~WH_Robot()
14
{
15
  delete(nav_map);
16
}
17

    
18
Duration WH_Robot::get_worst_case_time(State start_state, State target_state)
19
{
20
  return nav_map->get_worst_case_time(start_state, target_state);
21
}
22

    
23
/** @brief Based on the given path, make
24
 *         the series of turns to follow this path,
25
 *         updating the current state in the navigation map as we do so   
26
 *  @param the Path to follow
27
 */
28
void WH_Robot::follow_path(Path path_to_follow){
29
  for(int i=0; i<path_to_follow.len; i++)
30
  {
31
    Turn t = path_to_follow.path[i];
32
    ROS_INFO("making turn %d", t);
33
    nav_map->update_state(t);
34
    switch(t)
35
    {
36
      case ISTRAIGHT:
37
        turn_straight();
38
        follow_line();
39
        break;
40
      case ILEFT:
41
        turn_left();
42
        follow_line();
43
        break;
44
      case IRIGHT:
45
        turn_right();
46
        follow_line();
47
        break;
48
      case IUTURN:
49
        u_turn();
50
        follow_line();
51
        break;
52
    }
53
  }  
54
}
55

    
56
int WH_Robot::exec_task()
57
{
58
  ROS_INFO("WH_robot: Executing a task");
59
  assert(curr_task != DEFAULT_TASK);
60
  
61
  // remember where you started doing this task so we know where to return
62
  State home_state = nav_map->get_state();
63
  ROS_INFO("WH_robot: starting from home state %d", (int)home_state);
64
  ROS_INFO("WH_robot: aiming for state %d", (int)curr_task->get_dest());
65
  Path new_path = nav_map->shortest_path(curr_task->get_dest());
66
  curr_task->set_path(new_path);
67

    
68
  follow_path(new_path);
69
   
70
  //TODO: forklift yaaaay
71

    
72
  int did_task_complete;
73

    
74
  srand(0xDEADBEEF);
75
  int error = rand() % 12;
76
  if(error < 6) //Fail with 1/2 probability
77
  {
78
    ROS_INFO("WH_robot: TASK COMPLETE!");
79
    did_task_complete = TASK_COMPLETED;
80
  }
81
  else
82
  {
83
    ROS_INFO("WH_robot: TASK FAILED!");
84
    did_task_complete = TASK_FAILED;
85
  }
86

    
87
  ROS_INFO("WH_robot: Now I am attempting to go home from state %d to state %d", 
88
        (int)nav_map->get_state(), (int)home_state);
89

    
90
  Path return_path = nav_map->shortest_path(home_state);
91
  curr_task->set_path(return_path);
92
  
93
  follow_path(return_path);
94

    
95
  return did_task_complete;
96
}
97

    
98
void WH_Robot::robot_callback(const std_msgs::String::ConstPtr& msg)
99
{
100
  std::cout << "robot callback called." << std::endl;
101
  if(msg->data.compare(0, 11, "REG_SUCCESS") == 0)
102
  {
103
    id = atoi(msg->data.substr(12).c_str());
104
    reg_failed = 0;
105
  }
106
  else if(msg->data.compare(0, 8, "SET_TASK") == 0)
107
  {
108
    ROS_INFO("WH_ROBOT: Setting robot task");
109
    char* string = (char*)msg->data.c_str();
110
    ROS_INFO("Set task string is: %s", string);
111
    char* data;
112
    data = strtok(string, " ");
113
    data = strtok(NULL, " ");
114
    int order_id = atoi(data);
115
    ROS_INFO("order Id: %d with string %s", order_id, data);
116

    
117
    data = strtok(NULL, " ");
118
    Address source = atoi(data);
119
    ROS_INFO("source: %d with string %s", source, data);
120

    
121
    data = strtok(NULL, " ");
122
    Address dest = atoi(data);
123
    ROS_INFO("dest: %d with string %s", dest, data);
124

    
125
    Time start_time = ros::Time::now();
126
    Path path;
127
    path.len = 0;
128
    path.path = NULL;
129
    Duration est_time(0);
130

    
131
    curr_task = new Order(order_id, source, dest, start_time, path, est_time);
132
  }
133
  else
134
  {
135
    ROS_INFO("I got a bad message: %s", msg->data.c_str());
136
  }
137
}
138

    
139
void WH_Robot::run ()
140
{
141
  ROS_INFO("A WH_Robot has been created");
142

    
143
  robot_to_sched = node.advertise<std_msgs::String>("robot_to_sched", 1000);
144
  sched_to_robot = node.subscribe(name + "_topic", 1000, &WH_Robot::robot_callback, this);
145

    
146
  ROS_INFO("I am a robot. Registering with scheduler...");
147
  while(reg_failed && ok())
148
  {
149
    std_msgs::String msg;
150
    std::stringstream ss;
151
    ss << "REGISTER "<<name;
152
    msg.data = ss.str();
153
    robot_to_sched.publish(msg);
154

    
155
    spinOnce();
156
  }
157

    
158
  follow_line();
159

    
160
  while(ok())
161
  {
162
    ROS_INFO("WH_ROBOT: Running main loop");
163

    
164

    
165
      std_msgs::String msg;
166
      std::stringstream ss;
167
      ss << "GET_TASK "<<id;
168
      msg.data = ss.str();
169
      robot_to_sched.publish(msg);
170

    
171
    while(curr_task == DEFAULT_TASK && ok()){
172
      spinOnce();
173
    }
174

    
175
    int error = exec_task();
176
    if(error == TASK_COMPLETED)
177
    {
178
      std_msgs::String msg;
179
      std::stringstream ss;
180
      ss << "SUCCESS "<<curr_task->getid();
181
      msg.data = ss.str();
182
      robot_to_sched.publish(msg);
183
    }
184
    else //error == TASK_FAILED
185
    {
186
      std_msgs::String msg;
187
      std::stringstream ss;
188
      ss << "FAILED "<<curr_task->getid();
189
      msg.data = ss.str();
190
      robot_to_sched.publish(msg);
191
    }
192
    delete curr_task;
193
    curr_task = DEFAULT_TASK;
194

    
195
  }
196
}
197

    
198
void WH_Robot::set_task(Order order)
199
{
200
  curr_task = new Order(order.getid(), order.get_source(), order.get_dest(), 
201
      order.get_start_time(), order.get_path(), order.get_est_time()); 
202
  return;
203
}
204