Statistics
| Branch: | Revision:

root / scout_avr / src / main.cpp @ 958699af

History | View | Annotate | Download (4.7 KB)

1 6e7f0a98 Tom Mullins
#if 1 ///////////////////////////////////////////////
2 807483bf Tom Mullins
3 49090532 Tom Mullins
#include "ros.h"
4 6e7f0a98 Tom Mullins
#include "bom.h"
5 47e26dee Tom Mullins
#include "range.h"
6 6c9146d5 Tom Mullins
#include "stepper.h"
7
#include <std_msgs/Int16.h> // TODO remove
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 47e26dee Tom Mullins
#include <util/delay.h>
13 88fb3a79 Tom Mullins
14 6c9146d5 Tom Mullins
/* Period of main loop in ms */
15 2853d46b Tom Mullins
#define MAINLOOP_PERIOD 100//50
16 6c9146d5 Tom Mullins
17
char range_enabled = 0;
18 49090532 Tom Mullins
19 812788aa Tom Mullins
void debug(const char *str)
20
{
21 31f4a032 Tom Mullins
}
22
23 cf115e3d Tom Mullins
void callback(const std_msgs::Int16& msg)
24 49090532 Tom Mullins
{
25
}
26
27 6c9146d5 Tom Mullins
/* dammit, Priya, this capitalization just looks ridiculous */
28
void range_toggle_cb(const sonar::sonar_toggleRequest& req,
29
    sonar::sonar_toggleResponse& resp)
30
{
31
  range_enabled = req.set_on;
32
  resp.ack = true;
33
}
34
35
void range_set_scan_cb(const sonar::sonar_set_scanRequest& req,
36
    sonar::sonar_set_scanResponse& resp)
37
{
38
  step_sweep_bounds(req.stop_l, req.stop_r);
39
  resp.ack = true;
40
}
41
42 49090532 Tom Mullins
int main()
43 88fb3a79 Tom Mullins
{
44 6c9146d5 Tom Mullins
  unsigned long now, next;
45
  unsigned int ranges[2];
46 6e7f0a98 Tom Mullins
  char i, id;
47 6c9146d5 Tom Mullins
48 88fb3a79 Tom Mullins
  ros::NodeHandle nh;
49 6c9146d5 Tom Mullins
  nh.initNode();
50 49090532 Tom Mullins
51 6c9146d5 Tom Mullins
  /* To be removed later; just an example of a subscirber */
52
  ros::Subscriber<std_msgs::Int16> test_in("test_in", callback);
53
  nh.subscribe(test_in);
54 88fb3a79 Tom Mullins
55 2853d46b Tom Mullins
  /* Stepper */
56
  // TODO ROS messages to set bounds
57
  step_init();
58
  step_dir(1);
59
  step_set_size(STEP_WHOLE);
60
  step_sweep_bounds(-26, 26);
61
62 6c9146d5 Tom Mullins
  /* Range */
63 47e26dee Tom Mullins
  range_init();
64 6c9146d5 Tom Mullins
  sonar::sonar_distance range_msg;
65 86b48573 Tom Mullins
  ros::Publisher range_pub("sonar_distance", &range_msg);
66 6c9146d5 Tom Mullins
  ros::ServiceServer
67
    <sonar::sonar_toggleRequest, sonar::sonar_toggleResponse>
68
    range_toggle("sonar_toggle", range_toggle_cb);
69
  ros::ServiceServer
70
    <sonar::sonar_set_scanRequest, sonar::sonar_set_scanResponse>
71
    range_set_scan("sonar_set_scan", range_set_scan_cb);
72 86b48573 Tom Mullins
  nh.advertise(range_pub);
73 958699af Tom Mullins
  nh.advertiseService(range_toggle);
74
  nh.advertiseService(range_set_scan);
75 6c9146d5 Tom Mullins
76
  /* BOM */
77 47e26dee Tom Mullins
  bom_init();
78 6c9146d5 Tom Mullins
  bom::bom bom_msg;
79
  ros::Publisher bom_pub("bom", &bom_msg);
80 6e7f0a98 Tom Mullins
  nh.advertise(bom_pub);
81 88fb3a79 Tom Mullins
82 6e7f0a98 Tom Mullins
  id = 0;
83 6c9146d5 Tom Mullins
  next = 0;
84 88fb3a79 Tom Mullins
  while (1)
85
  {
86 6c9146d5 Tom Mullins
    nh.spinOnce();
87
88
    /* Skip loop if the loop period hasn't passed yet */
89
    /* TODO if we need more exact timing, we can enter a tight loop when now
90
     * gets close to next, and avoid the uncertainty of nh.spinOnce() */
91
    now = nh.getHardware()->time();
92
    if (now < next) {
93
      continue;
94
    }
95
    next = now + MAINLOOP_PERIOD;
96
97
    /* Temporary, for BOM testing */
98 6e7f0a98 Tom Mullins
    id++;
99
    if (id == 0x40) {
100
      id = 0;
101
    }
102
    set_robot_id(id);
103 47e26dee Tom Mullins
    bom_send(id & 1);
104 6c9146d5 Tom Mullins
105
    /* BOM */
106 6e7f0a98 Tom Mullins
    for (i = 0; i < 4; i++) {
107
      int msg = bom_get(i);
108
      if (msg != BOM_NO_DATA) {
109
        bom_msg.sender = bom_msg_get_robot_id(msg);
110
        bom_msg.send_dir = bom_msg_get_dir(msg);
111
        bom_msg.recv_dir = i;
112
        bom_pub.publish(&bom_msg);
113
      }
114
    }
115 47e26dee Tom Mullins
116 6c9146d5 Tom Mullins
    /* Stepper / range sensor */
117 2853d46b Tom Mullins
    if (range_enabled) {
118
      step_sweep();
119
      range_measure(ranges);
120
      range_msg.header.stamp = nh.now();
121
      range_msg.header.seq++;
122
      range_msg.pos = 0; // TODO fill this
123
      range_msg.distance0 = ranges[0];
124
      range_msg.distance1 = ranges[1];
125
      range_pub.publish(&range_msg);
126
    }
127 6c9146d5 Tom Mullins
128 88fb3a79 Tom Mullins
  }
129
130
  return 0;
131 49090532 Tom Mullins
}
132 88fb3a79 Tom Mullins
133 807483bf Tom Mullins
#else ///////////////////////////////////////////////
134
135
extern "C"
136 88fb3a79 Tom Mullins
{
137
#include <stdlib.h>
138
#include <string.h>
139 fd73d758 Tom Mullins
#include <avr/io.h>
140
#include <util/delay.h>
141 88fb3a79 Tom Mullins
}
142
143 49090532 Tom Mullins
#include "Atmega128rfa1.h"
144 1c3c96ce Tom Mullins
#include "range.h"
145 f115416e Tom Mullins
#include "bom.h"
146 6c9146d5 Tom Mullins
#include "stepper.h"
147 49090532 Tom Mullins
148 31f4a032 Tom Mullins
Atmega128rfa1 avr;
149
150 812788aa Tom Mullins
void debug(const char *str)
151
{
152 31f4a032 Tom Mullins
  avr.puts(str);
153
}
154
155 88fb3a79 Tom Mullins
int main()
156
{
157 1c3c96ce Tom Mullins
  char buf[20];
158 86b48573 Tom Mullins
  //int i;
159
  //char id = 0;
160 2853d46b Tom Mullins
  /*unsigned long now, next = 0;
161
  unsigned int ranges[2];*/
162 88fb3a79 Tom Mullins
  avr.init();
163 1c3c96ce Tom Mullins
  range_init();
164 f115416e Tom Mullins
  bom_init();
165 2853d46b Tom Mullins
  func step_sweep_cb = step_init(10);
166 6c9146d5 Tom Mullins
  step_sweep_bounds(-26, 26);
167
  step_dir(1);
168 2853d46b Tom Mullins
  step_sweep_speed(50);
169
  PORTF |= _BV(PF4);
170
  _delay_ms(500);
171
  PORTF = (PORTF & ~_BV(PF4)) | _BV(PF5);
172
  _delay_ms(500);
173
  PORTF &= ~_BV(PF5);
174 88fb3a79 Tom Mullins
  while (1)
175
  {
176 2853d46b Tom Mullins
    step_sweep_cb();
177
    _delay_ms(10);
178
    /*now = avr.time();
179 49090532 Tom Mullins
    if (now > next) {
180 2853d46b Tom Mullins
      next = now + 500;*/
181 fd73d758 Tom Mullins
      /*ultoa(now, buf, 10);
182 812788aa Tom Mullins
      avr.puts(buf);
183 fd73d758 Tom Mullins
      avr.puts("\r\n");*/
184 6c9146d5 Tom Mullins
      /*id++;
185 31f4a032 Tom Mullins
      if (id == 0x40) {
186
        id = 0;
187
      }
188 6c9146d5 Tom Mullins
      set_robot_id(id);*/
189 2853d46b Tom Mullins
      /*range_measure(ranges);
190 86b48573 Tom Mullins
      utoa(ranges[0], buf, 10);
191 f115416e Tom Mullins
      avr.puts("Range: ");
192
      avr.puts(buf);
193
      avr.puts(", ");
194 86b48573 Tom Mullins
      utoa(ranges[1], buf, 10);
195
      avr.puts(buf);
196 2853d46b Tom Mullins
      avr.puts("\r\n");*/
197 6c9146d5 Tom Mullins
      /*for (i = 0; i < 4; i++) {
198 f115416e Tom Mullins
        bom_send(i);
199
        int msg = bom_get(i);
200
        if (msg != BOM_NO_DATA) {
201
          avr.puts("BOM ");
202
          itoa(i, buf, 10);
203
          avr.puts(buf);
204
          avr.puts(": ");
205 31f4a032 Tom Mullins
          itoa(bom_msg_get_robot_id(msg), buf, 10);
206
          avr.puts(buf);
207
          avr.puts(" (");
208
          itoa(bom_msg_get_dir(msg), buf, 10);
209 f115416e Tom Mullins
          avr.puts(buf);
210 31f4a032 Tom Mullins
          avr.puts(")\r\n");
211 f115416e Tom Mullins
        }
212 6c9146d5 Tom Mullins
      }*/
213 2853d46b Tom Mullins
    //}
214 88fb3a79 Tom Mullins
  }
215
  return 0;
216 cf115e3d Tom Mullins
}
217 807483bf Tom Mullins
218
#endif //////////////////////////////////////////////