Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / PositionMonitor.cpp @ 467

History | View | Annotate | Download (3.53 KB)

1
/**
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
#include <PositionMonitor.h>
12
#include <stdlib.h>
13
#include <vision.h>
14

    
15
#include <stdio.h>
16

    
17
#include <map>
18
using namespace std;
19

    
20
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
  newIdToAssign = -1;
25
}
26

    
27
PositionMonitor::~PositionMonitor() {
28
}
29

    
30
int PositionMonitor::startMonitoring() {
31
  //TODO: fill this in when this becomes asynchronous
32
  return 0;
33
}
34

    
35
int PositionMonitor::stopMonitoring() {
36
  //TODO: fill this in when this becomes asynchronous
37
  return 0;
38
}
39

    
40
int PositionMonitor::updatePositions() {
41
  VisionPosition * positions = NULL;
42

    
43
  //TODO: check for error returned
44
  int numPositions = vision_get_robot_positions(&positions);
45
  printf("numPositions is %d\n", numPositions);
46
  for (int i = 0; i < numPositions; i++) {
47
    printf("{%d,%d} ", positions[i].x, positions[i].y);
48
  }
49
  printf("\n");
50

    
51

    
52
  /*
53
  //TODO: compare current set of positions to previous set of
54
  // positions to match up who is who
55
  positionMap.clear();
56

57
  int i;
58
  for (i = 0; i < numPositions; i++) {
59
    //TODO: would this insert by copy or just point to the memory in positions[i]?
60
    //      If this just points to the memory in positions[i] then we have a bug
61
    positionMap[i] = positions[i];
62
  }
63
  */
64

    
65
  //TODO: also remove robots that might have disappeared
66
  int i;
67
  for (i = 0; i < numPositions; i++) {
68
    VisionPosition newPos = positions[i];
69
    map<int, VisionPosition>::iterator iter;
70
    for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
71
      VisionPosition oldPos = iter->second;
72

    
73
      if (isProbablySameRobot(newPos, oldPos)) {
74
        //TODO: is this the right use of an iterator?
75
        iter->second = newPos;
76
        break;
77
      }
78
    }
79

    
80
    if (iter == positionMap.end()) {
81
      printf("Inserting new robot: %d (%d,%d)", newIdToAssign, newPos.x, newPos.y);
82
      
83
      //a position was found that probably isn't a known robot so add it in case a new robot entered the field
84
      positionMap.insert(make_pair(newIdToAssign, newPos));
85
      newIdToAssign--;
86
    }
87
  }
88

    
89
  //TODO: remove this debug information
90
  map<int, VisionPosition>::iterator iter;
91
  for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
92
    printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
93
  }
94
  
95
  if (positions)
96
    free(positions);
97

    
98
  return 0;
99
}
100

    
101
int PositionMonitor::assignRealId(int old_id, int real_id) {
102
  printf("assigning real_id %d to old_id %d\n", real_id, old_id);
103

    
104
  map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
105

    
106
  if (iter == positionMap.end()) {
107
    fprintf(stderr, "assignRealId: old_id not found\n");
108
    return -1;
109
  }
110

    
111
  positionMap.insert(make_pair(real_id, iter->second));
112
  positionMap.erase(old_id);
113

    
114
  return 0;
115
}
116

    
117
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
118
  //TODO do this in another thread!
119
  updatePositions();
120

    
121
  return positionMap;
122
}
123

    
124
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) {
125
  //TODO: figure out what a map returns if the element doesn't exist
126
  if (positionMap.find(robot_id) == positionMap.end()){
127
    return -1;
128
  }
129

    
130
  VisionPosition pos = positionMap[robot_id];
131

    
132
  *xbuf = pos.x;
133
  *ybuf = pos.y;
134

    
135
  return 0;
136
}
137

    
138
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
139
  int xDiff = p1.x - p2.x;
140
  int yDiff = p1.y - p2.y;
141
  return (xDiff*xDiff + yDiff*yDiff < MAX_DISTANCE*MAX_DISTANCE);
142
}