Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / PositionMonitor.cpp @ 482

History | View | Annotate | Download (3.38 KB)

1 436 jknichel
/**
2
 * @file PositionMonitor.cpp
3
 *
4
 * @author Jason Knichel
5
 *
6
 * @date 2/4/08
7
 */
8
9
//TODO: make this file asynchronous
10
11 443 emarinel
#include <PositionMonitor.h>
12 436 jknichel
#include <stdlib.h>
13
#include <vision.h>
14
15 449 jknichel
#include <stdio.h>
16
17
#include <map>
18
using namespace std;
19
20 436 jknichel
PositionMonitor::PositionMonitor() {
21
  //TODO: don't hardcode this file name
22
  //TODO: check for error returned from init
23
  vision_init("/var/www/colonet.jpg");
24 466 emarinel
  newIdToAssign = -1;
25 436 jknichel
}
26
27
PositionMonitor::~PositionMonitor() {
28
}
29
30
int PositionMonitor::startMonitoring() {
31 449 jknichel
  //TODO: fill this in when this becomes asynchronous
32 436 jknichel
  return 0;
33
}
34
35
int PositionMonitor::stopMonitoring() {
36 449 jknichel
  //TODO: fill this in when this becomes asynchronous
37 436 jknichel
  return 0;
38
}
39
40 447 emarinel
int PositionMonitor::updatePositions() {
41 449 jknichel
  VisionPosition * positions = NULL;
42 436 jknichel
43
  //TODO: check for error returned
44 449 jknichel
  int numPositions = vision_get_robot_positions(&positions);
45 466 emarinel
  printf("numPositions is %d\n", numPositions);
46 467 emarinel
  for (int i = 0; i < numPositions; i++) {
47
    printf("{%d,%d} ", positions[i].x, positions[i].y);
48
  }
49
  printf("\n");
50 436 jknichel
51 468 jknichel
  map<int, VisionPosition> newPositionMap;
52 467 emarinel
53 458 jknichel
  //TODO: also remove robots that might have disappeared
54
  int i;
55
  for (i = 0; i < numPositions; i++) {
56
    VisionPosition newPos = positions[i];
57
    map<int, VisionPosition>::iterator iter;
58
    for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
59
      VisionPosition oldPos = iter->second;
60 449 jknichel
61 458 jknichel
      if (isProbablySameRobot(newPos, oldPos)) {
62 466 emarinel
        //TODO: is this the right use of an iterator?
63 468 jknichel
        newPositionMap.insert(make_pair(iter->first, newPos));
64 466 emarinel
        break;
65 458 jknichel
      }
66
    }
67 466 emarinel
68 458 jknichel
    if (iter == positionMap.end()) {
69 468 jknichel
      //a position was found that probably isn't a known
70
      //  robot so add it in case a new robot entered the field
71 466 emarinel
      printf("Inserting new robot: %d (%d,%d)", newIdToAssign, newPos.x, newPos.y);
72
73 458 jknichel
      //a position was found that probably isn't a known robot so add it in case a new robot entered the field
74 468 jknichel
      newPositionMap.insert(make_pair(newIdToAssign, newPos));
75 466 emarinel
      newIdToAssign--;
76 458 jknichel
    }
77
  }
78
79 468 jknichel
  positionMap = newPositionMap;
80
81 449 jknichel
  //TODO: remove this debug information
82
  map<int, VisionPosition>::iterator iter;
83
  for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
84
    printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
85
  }
86
87
  if (positions)
88
    free(positions);
89 458 jknichel
90 436 jknichel
  return 0;
91
}
92 443 emarinel
93 455 emarinel
int PositionMonitor::assignRealId(int old_id, int real_id) {
94
  printf("assigning real_id %d to old_id %d\n", real_id, old_id);
95
96
  map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
97
98
  if (iter == positionMap.end()) {
99
    fprintf(stderr, "assignRealId: old_id not found\n");
100
    return -1;
101
  }
102
103
  positionMap.insert(make_pair(real_id, iter->second));
104
  positionMap.erase(old_id);
105
106
  return 0;
107
}
108
109 447 emarinel
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
110 466 emarinel
  //TODO do this in another thread!
111
  updatePositions();
112
113 447 emarinel
  return positionMap;
114
}
115
116 443 emarinel
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) {
117 449 jknichel
  //TODO: figure out what a map returns if the element doesn't exist
118 451 jknichel
  if (positionMap.find(robot_id) == positionMap.end()){
119 449 jknichel
    return -1;
120
  }
121
122
  VisionPosition pos = positionMap[robot_id];
123 455 emarinel
124 449 jknichel
  *xbuf = pos.x;
125
  *ybuf = pos.y;
126 443 emarinel
127
  return 0;
128
}
129 458 jknichel
130
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
131
  int xDiff = p1.x - p2.x;
132
  int yDiff = p1.y - p2.y;
133
  return (xDiff*xDiff + yDiff*yDiff < MAX_DISTANCE*MAX_DISTANCE);
134
}