Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / libscout / src / behaviors / WH_Robot.cpp @ 3f72678f

History | View | Annotate | Download (3.04 KB)

1 bebd9bcb Leon
#include "WH_Robot.h"
2 6761a531 Priya
#include "../helper_classes/Order.h"
3 bebd9bcb Leon
4
/** @Brief: warehouse robot constructor **/
5 3f72678f Priya
WH_Robot::WH_Robot(std::string scoutname):Behavior(scoutname, "WH_Robot")
6 bebd9bcb Leon
{
7 3f72678f Priya
    ROS_INFO("WH_ROBOT: Creating a WH_Robot...");
8 6761a531 Priya
    nav_map = new navigationMap(scoutname);
9 9b4328d7 Priya
    curr_task = DEFAULT_TASK;
10 3f72678f Priya
    name = scoutname;
11
    reg_failed = 1;
12
13
    robot_to_sched = n.advertise<std_msgs::String>(name + "_topic", 1000);
14
    sched_to_robot = n.subscribe("sched_to_robot", 1000, msg_callback);
15
16
    while(reg_failed)
17
    {
18
      std_msgs::String msg;
19
      std::stringstream ss;
20
      ss << "REGISTER "<<name;
21
      msg.data = ss.str();
22
      robot_to_sched.publish(msg);
23
    }
24
25
    ROS_INFO("WH_Robot: I am a created WH robot");
26 bebd9bcb Leon
}
27
28 6761a531 Priya
WH_Robot::~WH_Robot()
29
{
30
    delete(nav_map);
31
}
32
33
Duration WH_Robot::get_worst_case_time(State start_state, State target_state)
34 bebd9bcb Leon
{
35 6761a531 Priya
  return nav_map->get_worst_case_time(start_state, target_state);
36 bebd9bcb Leon
}
37
38 9b4328d7 Priya
int WH_Robot::exec_task()
39 bebd9bcb Leon
{
40 3f72678f Priya
    ROS_INFO("WH_robot: Executing a task");
41 9b4328d7 Priya
    assert(curr_task != DEFAULT_TASK);
42
    //TODO: do task
43
    srand(0xDEADBEEF);
44
    int error = rand() % 12;
45
    if(error < 9) //Fail with 1/4 probability
46 bebd9bcb Leon
    {
47 3f72678f Priya
      ROS_INFO("WH_robot: TASK COMPLETE!");
48 9b4328d7 Priya
      return TASK_COMPLETED;
49 bebd9bcb Leon
    }
50 9b4328d7 Priya
    else
51
    {
52 3f72678f Priya
      ROS_INFO("WH_robot: TASK FAILED!");
53 9b4328d7 Priya
      return TASK_FAILED;
54 bebd9bcb Leon
    }
55
}
56
57 3f72678f Priya
void WH_Robot::msg_callback(const std_msgs::String::ConstPtr& msg)
58
{
59
  if(msg->data.compare(0, 11, "REG_SUCCESS") == 0)
60
  {
61
    reg_failed = 0;
62
  }
63
  else if(msg->data.compare(0, 8, "SET_TASK") == 0)
64
  {
65
    char* string = msg->data.c_str();
66
    char* data;
67
    data = strtok(string, " ");
68
    int id = atoi(data);
69
    data = strtok(string, " ");
70
    Address source = atoi(data);
71
    data = strtok(string, " ");
72
    Address dest = atoi(data);
73
    Time start_time = ros::Time::now();
74
    Path path = NULL;
75
    Duration est_time(0);
76
77
    curr_task = new Order(id, source, dest, start_time, path, est_time);
78
  }
79
  else
80
  {
81
    ROS_INFO("I got a bad message: %s", msg->data);
82
  }
83
}
84
85
void WH_Robot::run ()
86
{
87
88
    while(ok())
89 bebd9bcb Leon
    {
90 3f72678f Priya
        ROS_INFO("WH_ROBOT: Running main loop");
91
92
93
        std_msgs::String msg;
94
        std::stringstream ss;
95
        ss << "GET_TASK "<<id;
96
        msg.data = ss.str();
97
        robot_to_sched.publish(msg);
98
99
100
        while(curr_task == DEFAULT_TASK)
101
          continue;
102
        int error = exec_task();
103
        if(error == TASK_COMPLETED)
104
        {
105
106
107
        ss << "SUCCESS "<<curr_task->id;
108
        msg.data = ss.str();
109
        robot_to_sched.publish(msg);
110
111
112
        }
113
        else //error == TASK_FAILED
114
        {
115
 
116
117
        ss << "FAILED "<<curr_task->id;
118
        msg.data = ss.str();
119
        robot_to_sched.publish(msg);
120
121
122
       }
123
        delete curr_task;
124
        curr_task = DEFAULT_TASK;
125 bebd9bcb Leon
    }
126
}
127
128 9b4328d7 Priya
void WH_Robot::set_task(Order order)
129
{
130 ca164875 Leon
    curr_task = new Order(order.getid(), order.get_source(), order.get_dest(), 
131
        order.get_start_time(), order.get_path(), order.get_est_time()); 
132 9b4328d7 Priya
    return;
133 bebd9bcb Leon
}
134 9b4328d7 Priya