Statistics
| Branch: | Revision:

scoutos / scout_avr / src / main.cpp @ daf6a575

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

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

    
18
char range_enabled = 0;
19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
137
  }
138

    
139
  return 0;
140
}
141

    
142
#else ///////////////////////////////////////////////
143

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

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

    
157
Atmega128rfa1 avr;
158

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

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

    
227
#endif //////////////////////////////////////////////