1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include <cstdlib>
#include <memory>
#include <thread>
#include <deque>
#include <iostream>
#include <vector>
#include <atomic>
#include <unistd.h>
#include <ctime>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
class Robot;
class l_package {
public:
l_package(){};
l_package(int r) : r_flag(r){};
int l_flag=1;
int r_flag=0;
virtual int& get(){}; //virtual getter returns nothing
};
class l_package_t :public l_package {
public:
l_package_t();
l_package_t(int &i_trigger): l_package(99), trigger(i_trigger) {};
virtual int& get() {
return trigger;
};
int &trigger;
};
typedef int(Robot::*m_behavior)(int);
typedef int(Robot::*m_listener)(l_package&);
class thread_item;
class Behavior {
public:
Behavior(Robot &o, m_behavior b_have, std::string i_id);
void run_listeners();
int exec(int flag);
l_package k_package;
l_package s_package;
l_package_t s_proposed;
m_behavior main_b;
std::string id;
std::vector<std::shared_ptr<thread_item> >thread_list;
Robot &r;
};
struct State {
std::shared_ptr<Behavior> current_behavior;
int current_param;
};
class Stack {
public:
Stack() {};
void empty(){
state.clear();
};
State pop_out(){
State last=state.front();
state.pop_front();
return last;
};
void push_b(std::shared_ptr<Behavior> i_behavior, int iparam) {
State temp;
temp.current_behavior=i_behavior;
temp.current_param=iparam;
state.push_back(temp);
};
void push_f(std::shared_ptr<Behavior> i_behavior, int iparam) {
State temp;
temp.current_behavior=i_behavior;
temp.current_param=iparam;
state.push_front(temp);
};
std::deque<State> state;
};
class Robot {
public:
static std::atomic<bool> behavior_status;
Robot() {
std::shared_ptr<Behavior> move=std::make_shared<Behavior>(*this,&Robot::move_and_scan,"M");
run_stack->push_b(move,20);
};
int keyboard_interrupt(l_package &l_input){
std::cout << "Keyboard running with R flag " <<l_input.r_flag <<std::endl;
int c;
for (;;) {
if (l_input.l_flag !=1)
break;
if (kbhit()) {
if (getchar() == 'c') {
std::cout << "Key c pressed, stopping move_and_scan" << std::endl;
behavior_status=false;
}
}
}
std::cout << "Keyboard thread ending " << l_input.l_flag << std::endl;
}
int sonar_interrupt( l_package &l_input ){
std::cout << "Sonar running with R flag " << l_input.r_flag <<std::endl;
std::cout << "s_proposed in sonar should give the trigger value of 50 =>" << l_input.get() << std::endl;
int c;
l_input.r_flag=true;
for (;;) {
if (l_input.l_flag!=1)
break;
else {
usleep(1000000);
}
}
l_input.r_flag=false;
std::cout << "Sonar thread ending " << l_input.l_flag << std::endl;
}
int move_and_scan(int x){
bool status=true;
std::cout << "Move starting " << std::endl;
behavior_status=true;
int start_time=clock();
int elapsed_time=0;
do {
if (!behavior_status) {
std::cout << "Moved was stopped " << std::endl;
status=false;
break;
}
elapsed_time=clock() - start_time;
} while (elapsed_time < (x*1000000));
std::cout << "Move stopping with result "<< status << std::endl;
}
void run_state() {
if (run_stack->state.size() > 0) {
std::cout << "Run stack size is " << run_stack->state.size() << std::endl;
s=run_stack->pop_out();
int outcome=s.current_behavior->exec(s.current_param);
}
}
std::shared_ptr<Stack> run_stack=std::make_shared<Stack>();
State s;
static int speed;
};
int Robot::speed=50;
l_package_t::l_package_t():l_package(66),trigger(Robot::speed){}; //default l_package_t constructor requires initialization of trigger with some variable
std::atomic<bool> Robot::behavior_status{0};
class thread_item {
public:
thread_item(Robot &o, m_listener m_list, std::string i_id, l_package &i_pack) : r(o), id(i_id), l_pack(i_pack) {
thr=std::make_shared<std::thread>(m_list,r,std::ref(l_pack));
}
std::string id;
l_package &l_pack;
std::shared_ptr<std::thread> thr;
Robot &r;
};
Behavior::Behavior(Robot &o, m_behavior b_have, std::string i_id): r(o), main_b(b_have),id(i_id) {
k_package=l_package(5);
s_package=l_package(6);
};
void Behavior::run_listeners() {
std::shared_ptr<thread_item>m=std::make_shared<thread_item>(r,&Robot::keyboard_interrupt,"KL",std::ref(k_package));
thread_list.push_back(m);
//std::shared_ptr<thread_item>x=std::make_shared<thread_item>(r,&Robot::sonar_interrupt,"SL",std::ref(s_package)); //OLD package
std::shared_ptr<thread_item>x=std::make_shared<thread_item>(r,&Robot::sonar_interrupt,"SL",std::ref(s_proposed)); //NEW package with trigger
thread_list.push_back(x);
}
int Behavior::exec(int flag){
run_listeners();
std::cout << "Executing behavior " << this->id << std::endl;
bool result = (r.*(this->main_b))(flag);
for ( auto &l: thread_list ) {
l->l_pack.l_flag=0;
}
for ( auto &l: thread_list ) {
l->thr->join();
}
}
int main(int argc, char** argv) {
Robot create;
//for (;;) {
create.run_state();
//}
return 0;
}
|