root / quad2 / arduino / src / ros_lib / examples / ADC / ADC.pde @ c1426757
History | View | Annotate | Download (911 Bytes)
1 | c1426757 | Tom Mullins | /* |
---|---|---|---|
2 | * rosserial ADC Example |
||
3 | * |
||
4 | * This is a poor man's Oscilloscope. It does not have the sampling |
||
5 | * rate or accuracy of a commerical scope, but it is great to get |
||
6 | * an analog value into ROS in a pinch. |
||
7 | */ |
||
8 | |||
9 | #include <WProgram.h> |
||
10 | #include <ros.h> |
||
11 | #include <rosserial_arduino/Adc.h> |
||
12 | |||
13 | ros::NodeHandle nh; |
||
14 | |||
15 | rosserial_arduino::Adc adc_msg; |
||
16 | ros::Publisher p("adc", &adc_msg); |
||
17 | |||
18 | void setup() |
||
19 | { |
||
20 | pinMode(13, OUTPUT); |
||
21 | nh.initNode(); |
||
22 | |||
23 | nh.advertise(p); |
||
24 | } |
||
25 | |||
26 | //We average the analog reading to elminate some of the noise |
||
27 | int averageAnalog(int pin){ |
||
28 | int v=0; |
||
29 | for(int i=0; i<4; i++) v+= analogRead(pin); |
||
30 | return v/4; |
||
31 | } |
||
32 | |||
33 | long adc_timer; |
||
34 | |||
35 | void loop() |
||
36 | { |
||
37 | adc_msg.adc0 = averageAnalog(0); |
||
38 | adc_msg.adc1 = averageAnalog(1); |
||
39 | adc_msg.adc2 = averageAnalog(2); |
||
40 | adc_msg.adc3 = averageAnalog(3); |
||
41 | adc_msg.adc4 = averageAnalog(4); |
||
42 | adc_msg.adc5 = averageAnalog(5); |
||
43 | |||
44 | p.publish(&adc_msg); |
||
45 | |||
46 | nh.spinOnce(); |
||
47 | } |