Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout_avr / src / main.cpp @ 4bdd00ba

History | View | Annotate | Download (5.64 KB)

1
#if 1 ///////////////////////////////////////////////
2

    
3
#include "ros.h"
4
#include "bom.h"
5
#include "range.h"
6
#include "stepper.h"
7
#include "orb.h"
8
#include "cliffSensor.h"
9
#include <messages/bom.h>
10
#include <messages/sonar_distance.h>
11
#include <messages/sonar_toggle.h>
12
#include <messages/sonar_set_scan.h>
13
#include <messages/set_headlights.h>
14
#include <messages/cliff_status_changed.h>
15
#include <util/delay.h>
16

    
17
/* Period of main loop in ms */
18
#define MAINLOOP_PERIOD 100//50
19

    
20
char range_enabled = 0;
21

    
22
void debug(const char *str)
23
{
24
}
25

    
26
void orb_callback(const messages::set_headlights& msg)
27
{
28
  orb_set0(msg.left_red, msg.left_green, msg.left_blue);
29
  orb_set1(msg.right_red, msg.right_green, msg.right_blue);
30
}
31

    
32
/* dammit, Priya, this capitalization just looks ridiculous */
33
void range_toggle_cb(const messages::sonar_toggleRequest& req,
34
    messages::sonar_toggleResponse& resp)
35
{
36
  range_enabled = req.set_on;
37
  if (range_enabled)
38
    step_enable();
39
  else
40
    step_disable();
41
  resp.ack = true;
42
}
43

    
44
void range_set_scan_cb(const messages::sonar_set_scanRequest& req,
45
    messages::sonar_set_scanResponse& resp)
46
{
47
  step_sweep_bounds(req.stop_l, req.stop_r);
48
  resp.ack = true;
49
}
50

    
51
int main()
52
{
53
  unsigned long now, next;
54
  unsigned int ranges[2];
55
  char i, id, cliff_status;
56

    
57
  ros::NodeHandle nh;
58
  nh.initNode();
59

    
60
  /* Stepper */
61
  // TODO ROS messages to set bounds
62
  step_init();
63
  step_dir(1);
64
  step_set_size(STEP_WHOLE);
65
  step_sweep_bounds(-26, 26);
66

    
67
  /* Range */
68
  range_init();
69
  messages::sonar_distance range_msg;
70
  ros::Publisher range_pub("sonar_distance", &range_msg);
71
  ros::ServiceServer
72
    <messages::sonar_toggleRequest, messages::sonar_toggleResponse>
73
    range_toggle("sonar_toggle", range_toggle_cb);
74
  ros::ServiceServer
75
    <messages::sonar_set_scanRequest, messages::sonar_set_scanResponse>
76
    range_set_scan("sonar_set_scan", range_set_scan_cb);
77
  nh.advertise(range_pub);
78
  nh.advertiseService(range_toggle);
79
  nh.advertiseService(range_set_scan);
80

    
81
  /* BOM */
82
  bom_init();
83
  messages::bom bom_msg;
84
  ros::Publisher bom_pub("bom", &bom_msg);
85
  nh.advertise(bom_pub);
86

    
87
  /* Headlights (aka Orbs) */
88
  //orb_init();
89
  ros::Subscriber<messages::set_headlights> orb_sub("set_headlights",
90
      orb_callback);
91
  nh.subscribe(orb_sub);
92

    
93
  /* Cliff sensors */
94
  cliffSensor_init();
95
  messages::cliff_status_changed cliff_msg;
96
  ros::Publisher cliff_pub("cliff", &cliff_msg);
97
  nh.advertise(cliff_pub);
98

    
99
  id = 0;
100
  next = 0;
101
  while (1)
102
  {
103
    nh.spinOnce();
104

    
105
    /* Skip loop if the loop period hasn't passed yet */
106
    /* TODO if we need more exact timing, we can enter a tight loop when now
107
     * gets close to next, and avoid the uncertainty of nh.spinOnce() */
108
    now = nh.getHardware()->time();
109
    if (now < next) {
110
      continue;
111
    }
112
    next = now + MAINLOOP_PERIOD;
113

    
114
    /* Temporary, for BOM testing */
115
    id++;
116
    if (id == 0x40) {
117
      id = 0;
118
    }
119
    set_robot_id(id);
120
    bom_send(0);
121

    
122
    /* BOM */
123
    for (i = 0; i < 4; i++) {
124
      int msg = bom_get(i);
125
      if (msg != BOM_NO_DATA) {
126
        bom_msg.sender = bom_msg_get_robot_id(msg);
127
        bom_msg.send_dir = bom_msg_get_dir(msg);
128
        bom_msg.recv_dir = i;
129
        bom_pub.publish(&bom_msg);
130
      }
131
    }
132

    
133
    /* Stepper / range sensor */
134
    if (range_enabled) {
135
      step_sweep();
136
      range_measure(ranges);
137
      range_msg.stamp = nh.now();
138
      range_msg.pos = step_get_pos();
139
      range_msg.distance0 = ranges[0];
140
      range_msg.distance1 = ranges[1];
141
      range_msg.stamp = nh.now();
142
      range_pub.publish(&range_msg);
143
    }
144

    
145
    cliff_status = 0;
146
    if (read_cliffSensor_front())
147
      cliff_status |= messages::cliff_status_changed::CLIFF_FRONT;
148
    if (read_cliffSensor_left())
149
      cliff_status |= messages::cliff_status_changed::CLIFF_LEFT;
150
    if (read_cliffSensor_right())
151
      cliff_status |= messages::cliff_status_changed::CLIFF_RIGHT;
152
    if (cliff_status != cliff_msg.cliff_status) {
153
      cliff_msg.cliff_status = cliff_status;
154
      cliff_pub.publish(&cliff_msg);
155
    }
156

    
157
  }
158

    
159
  return 0;
160
}
161

    
162
#else ///////////////////////////////////////////////
163

    
164
extern "C"
165
{
166
#include <stdlib.h>
167
#include <string.h>
168
#include <avr/io.h>
169
#include <util/delay.h>
170
}
171

    
172
#include "Atmega128rfa1.h"
173
#include "range.h"
174
#include "bom.h"
175
#include "stepper.h"
176

    
177
Atmega128rfa1 avr;
178

    
179
void debug(const char *str)
180
{
181
  avr.puts(str);
182
}
183

    
184
int main()
185
{
186
  char buf[20];
187
  //int i;
188
  //char id = 0;
189
  /*unsigned long now, next = 0;
190
  unsigned int ranges[2];*/
191
  avr.init();
192
  range_init();
193
  bom_init();
194
  func step_sweep_cb = step_init(10);
195
  step_sweep_bounds(-26, 26);
196
  step_dir(1);
197
  step_sweep_speed(50);
198
  PORTF |= _BV(PF4);
199
  _delay_ms(500);
200
  PORTF = (PORTF & ~_BV(PF4)) | _BV(PF5);
201
  _delay_ms(500);
202
  PORTF &= ~_BV(PF5);
203
  while (1)
204
  {
205
    step_sweep_cb();
206
    _delay_ms(10);
207
    /*now = avr.time();
208
    if (now > next) {
209
      next = now + 500;*/
210
      /*ultoa(now, buf, 10);
211
      avr.puts(buf);
212
      avr.puts("\r\n");*/
213
      /*id++;
214
      if (id == 0x40) {
215
        id = 0;
216
      }
217
      set_robot_id(id);*/
218
      /*range_measure(ranges);
219
      utoa(ranges[0], buf, 10);
220
      avr.puts("Range: ");
221
      avr.puts(buf);
222
      avr.puts(", ");
223
      utoa(ranges[1], buf, 10);
224
      avr.puts(buf);
225
      avr.puts("\r\n");*/
226
      /*for (i = 0; i < 4; i++) {
227
        bom_send(i);
228
        int msg = bom_get(i);
229
        if (msg != BOM_NO_DATA) {
230
          avr.puts("BOM ");
231
          itoa(i, buf, 10);
232
          avr.puts(buf);
233
          avr.puts(": ");
234
          itoa(bom_msg_get_robot_id(msg), buf, 10);
235
          avr.puts(buf);
236
          avr.puts(" (");
237
          itoa(bom_msg_get_dir(msg), buf, 10);
238
          avr.puts(buf);
239
          avr.puts(")\r\n");
240
        }
241
      }*/
242
    //}
243
  }
244
  return 0;
245
}
246

    
247
#endif //////////////////////////////////////////////