Statistics
| Branch: | Revision:

scoutos / scout_avr / src / main.cpp @ 066b08bb

History | View | Annotate | Download (4.95 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 <std_msgs/Int16.h> // TODO remove
9
#include <bom/bom.h>
10
#include <sonar/sonar_distance.h>
11
#include <sonar/sonar_toggle.h>
12
#include <sonar/sonar_set_scan.h>
13
#include <headlights/set_headlights.h>
14
#include <util/delay.h>
15

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

    
19
char range_enabled = 0;
20

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

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

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

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

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

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

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

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

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

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

    
92
  id = 0;
93
  next = 0;
94
  while (1)
95
  {
96
    nh.spinOnce();
97

    
98
    /* Skip loop if the loop period hasn't passed yet */
99
    /* TODO if we need more exact timing, we can enter a tight loop when now
100
     * gets close to next, and avoid the uncertainty of nh.spinOnce() */
101
    now = nh.getHardware()->time();
102
    if (now < next) {
103
      continue;
104
    }
105
    next = now + MAINLOOP_PERIOD;
106

    
107
    /* Temporary, for BOM testing */
108
    id++;
109
    if (id == 0x40) {
110
      id = 0;
111
    }
112
    set_robot_id(id);
113
    bom_send(0);
114

    
115
    /* BOM */
116
    for (i = 0; i < 4; i++) {
117
      int msg = bom_get(i);
118
      if (msg != BOM_NO_DATA) {
119
        bom_msg.sender = bom_msg_get_robot_id(msg);
120
        bom_msg.send_dir = bom_msg_get_dir(msg);
121
        bom_msg.recv_dir = i;
122
        bom_pub.publish(&bom_msg);
123
      }
124
    }
125

    
126
    /* Stepper / range sensor */
127
    if (range_enabled) {
128
      step_sweep();
129
      range_measure(ranges);
130
      range_msg.header.stamp = nh.now();
131
      range_msg.header.seq++;
132
      range_msg.pos = 0; // TODO fill this
133
      range_msg.distance0 = ranges[0];
134
      range_msg.distance1 = ranges[1];
135
      range_pub.publish(&range_msg);
136
    }
137

    
138
  }
139

    
140
  return 0;
141
}
142

    
143
#else ///////////////////////////////////////////////
144

    
145
extern "C"
146
{
147
#include <stdlib.h>
148
#include <string.h>
149
#include <avr/io.h>
150
#include <util/delay.h>
151
}
152

    
153
#include "Atmega128rfa1.h"
154
#include "range.h"
155
#include "bom.h"
156
#include "stepper.h"
157

    
158
Atmega128rfa1 avr;
159

    
160
void debug(const char *str)
161
{
162
  avr.puts(str);
163
}
164

    
165
int main()
166
{
167
  char buf[20];
168
  //int i;
169
  //char id = 0;
170
  /*unsigned long now, next = 0;
171
  unsigned int ranges[2];*/
172
  avr.init();
173
  range_init();
174
  bom_init();
175
  func step_sweep_cb = step_init(10);
176
  step_sweep_bounds(-26, 26);
177
  step_dir(1);
178
  step_sweep_speed(50);
179
  PORTF |= _BV(PF4);
180
  _delay_ms(500);
181
  PORTF = (PORTF & ~_BV(PF4)) | _BV(PF5);
182
  _delay_ms(500);
183
  PORTF &= ~_BV(PF5);
184
  while (1)
185
  {
186
    step_sweep_cb();
187
    _delay_ms(10);
188
    /*now = avr.time();
189
    if (now > next) {
190
      next = now + 500;*/
191
      /*ultoa(now, buf, 10);
192
      avr.puts(buf);
193
      avr.puts("\r\n");*/
194
      /*id++;
195
      if (id == 0x40) {
196
        id = 0;
197
      }
198
      set_robot_id(id);*/
199
      /*range_measure(ranges);
200
      utoa(ranges[0], buf, 10);
201
      avr.puts("Range: ");
202
      avr.puts(buf);
203
      avr.puts(", ");
204
      utoa(ranges[1], buf, 10);
205
      avr.puts(buf);
206
      avr.puts("\r\n");*/
207
      /*for (i = 0; i < 4; i++) {
208
        bom_send(i);
209
        int msg = bom_get(i);
210
        if (msg != BOM_NO_DATA) {
211
          avr.puts("BOM ");
212
          itoa(i, buf, 10);
213
          avr.puts(buf);
214
          avr.puts(": ");
215
          itoa(bom_msg_get_robot_id(msg), buf, 10);
216
          avr.puts(buf);
217
          avr.puts(" (");
218
          itoa(bom_msg_get_dir(msg), buf, 10);
219
          avr.puts(buf);
220
          avr.puts(")\r\n");
221
        }
222
      }*/
223
    //}
224
  }
225
  return 0;
226
}
227

    
228
#endif //////////////////////////////////////////////