urg_beginner_task_07.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <mURGCtrl.h>
00010 #include <mRunCtrl.h>
00011 #include <vutils.h>
00012 #include <stdio.h>
00013
00014 using namespace VXV;
00015
00016 enum {
00017 BodyWidth = 330,
00018 };
00019
00020
00021
00022 static int getFrontLength(std::vector<VXV::Grid3D>& points) {
00023
00024 int min_length = -1;
00025 for (std::vector<Grid3D>::iterator it = points.begin();
00026 it != points.end(); ++it) {
00027 if ((abs(it->y) < (BodyWidth + 100)/2) && (it->x > 0)) {
00028 if ((min_length < 0) || (min_length > it->x)) {
00029 min_length = it->x;
00030 }
00031 }
00032 }
00033 return min_length;
00034 }
00035
00036
00037 int main(int argc, char *argv[]) {
00038 try {
00039 mRunCtrl run;
00040 mURGCtrl urg;
00041 if ((initConnection(&run, argc, argv) < 0) ||
00042 (initConnection(&urg, argc, argv) < 0)) {
00043 exit(1);
00044 }
00045
00046 vmonitor::show();
00047
00048 run.followLine(VXV::Position(0, 0, deg(0)));
00049
00050
00051 unsigned long beginTicks = VXV::GetTicks();
00052 bool moving = true;
00053 while (VXV::GetTicks() - beginTicks < 30000) {
00054 urg.capture();
00055 urg.convert();
00056
00057 int length = getFrontLength(urg.crd_points);
00058 if ((length > 0) && (length < 600)) {
00059 if (moving) {
00060
00061 run.push_runState();
00062 run.stop();
00063 run.pop_runState();
00064 moving = false;
00065 }
00066 } else if (!moving) {
00067
00068 run.lastMoveCommand();
00069 moving = true;
00070 }
00071 }
00072
00073 VXV::Delay(1000);
00074 } catch (std::exception& e) {
00075 printf("exception: %s\n", e.what());
00076 }
00077 return 0;
00078 }
00079