Go to the documentation of this file.00001 #include <Wire.h>
00002 #include <Servo.h>
00003 #include <CapSense.h>
00004
00005
00006 #include <ros.h>
00007 #include <std_msgs/UInt16.h>
00008 #include <std_msgs/Bool.h>
00009 #include <std_msgs/Float32.h>
00010
00011 #include <rosserial_adk_demo_msgs/Led.h>
00012 #include <sensor_msgs/Joy.h>
00013
00014 #include "Button.h"
00015 #include "adk_shield.h"
00016
00017
00018
00019
00020
00021 ros::NodeHandle nh;
00022
00023 template<Servo* servo, int i> void servo_cb(const std_msgs::UInt16& cmd ){
00024 if (cmd.data >= 0 && cmd.data <= 180 ) servo[i].write(cmd.data);
00025 }
00026
00027 ros::Subscriber<std_msgs::UInt16> sub_servo0("servo/0", servo_cb<servos,0>);
00028 ros::Subscriber<std_msgs::UInt16> sub_servo1("servo/1", servo_cb<servos,1>);
00029 ros::Subscriber<std_msgs::UInt16> sub_servo2("servo/2", servo_cb<servos,2>);
00030
00031 std_msgs::Bool button_msg;
00032 Button button1(BUTTON1);
00033 ros::Publisher pub_button1 ("button1", &button_msg);
00034
00035 Button button2(BUTTON2);
00036 ros::Publisher pub_button2 ("button2", &button_msg);
00037
00038 Button button3(BUTTON3);
00039 ros::Publisher pub_button3 ("button3", &button_msg);
00040
00041 Button button_joy(JOY_SWITCH);
00042 ros::Publisher pub_button_joy ("button_joy", &button_msg);
00043
00044 Button * buttons[4];
00045 ros::Publisher* pub_buttons[4];
00046
00047
00048 template<int ledr, int ledg, int ledb>
00049 void led_cb(const rosserial_adk_demo_msgs::Led& led){
00050 analogWrite(ledr, 255 - (unsigned char) led.r);
00051 analogWrite(ledg, 255 - (unsigned char) led.g);
00052 analogWrite(ledb, 255 - (unsigned char) led.b);
00053
00054 }
00055
00056 ros::Subscriber<rosserial_adk_demo_msgs::Led>
00057 sub_led_1 ("led1", led_cb<LED1_RED, LED1_GREEN, LED1_BLUE>),
00058 sub_led_2 ("led2", led_cb<LED2_RED, LED2_GREEN, LED2_BLUE>),
00059 sub_led_3 ("led3", led_cb<LED3_RED, LED3_GREEN, LED3_BLUE>);
00060
00061 template <int relay_pin>
00062 void relay_cb(const std_msgs::Bool& cmd){
00063 digitalWrite(relay_pin, cmd.data ? HIGH : LOW);
00064 }
00065
00066 ros::Subscriber<std_msgs::Bool> sub_relay1 ("relay1", relay_cb<RELAY1>);
00067 ros::Subscriber<std_msgs::Bool> sub_relay2 ("relay2", relay_cb<RELAY2>);
00068
00069 std_msgs::Float32 temp_msg;
00070 ros::Publisher pub_temp ("temperature", &temp_msg);
00071
00072 std_msgs::UInt16 light_msg;
00073 ros::Publisher pub_light ("light", &light_msg);
00074
00075 sensor_msgs::Joy joy_msg;
00076 float axes[2];
00077 char frame_id[] = "joy_stick";
00078 ros::Publisher pub_joy ("joy", &joy_msg);
00079
00080 std_msgs::Bool touch_msg;
00081 ros::Publisher pub_touch ("touch", &touch_msg);
00082
00083 void setup()
00084 {
00085
00086 setup_shield();
00087
00088
00089
00090 buttons[0] = &button1; buttons[1]=&button2; buttons[2]=&button3;
00091 buttons[3] = &button_joy;
00092
00093 pub_buttons[0] = &pub_button1; pub_buttons[1] = &pub_button2;
00094 pub_buttons[2] = &pub_button3; pub_buttons[3] = &pub_button_joy;
00095
00096
00097
00098 nh.initNode();
00099
00100 nh.subscribe(sub_servo0);
00101 nh.subscribe(sub_servo1);
00102 nh.subscribe(sub_servo2);
00103
00104 nh.subscribe(sub_led_1);
00105 nh.subscribe(sub_led_2);
00106 nh.subscribe(sub_led_3);
00107
00108 nh.subscribe(sub_relay1);
00109 nh.subscribe(sub_relay2);
00110
00111 nh.advertise(pub_button1);
00112 nh.advertise(pub_button2);
00113 nh.advertise(pub_button3);
00114 nh.advertise(pub_button_joy);
00115
00116 nh.advertise(pub_temp);
00117 nh.advertise(pub_light);
00118 nh.advertise(pub_touch);
00119 nh.advertise(pub_joy);
00120
00121
00122 joy_msg.axes = axes;
00123 joy_msg.axes_length =2;
00124 joy_msg.header.frame_id = frame_id;
00125 }
00126
00127
00128 unsigned long temp_pub_time;
00129 unsigned long light_pub_time;
00130 unsigned long joy_pub_time;
00131 unsigned long touch_robot_time;
00132
00133
00134 void loop()
00135 {
00136 nh.spinOnce();
00137
00138
00139 for (int i =0; i<4; ++i){
00140 if (buttons[i]->changed()){
00141 button_msg.data = buttons[i]->pressed();
00142 pub_buttons[i]->publish(&button_msg);
00143 }
00144 }
00145
00146
00147 if (millis() > temp_pub_time){
00148 temp_msg.data = (analogRead(TEMP_SENSOR)*4.9 -400)/19.5;
00149 pub_temp.publish(&temp_msg);
00150 temp_pub_time = millis() + 1000;
00151 }
00152
00153
00154
00155 if (millis() > light_pub_time){
00156 light_msg.data = analogRead(LIGHT_SENSOR);
00157 pub_light.publish(&light_msg);
00158 light_pub_time = millis() + 100;
00159 }
00160
00161
00162 if (millis() > joy_pub_time){
00163 int x, y;
00164 read_joystick(&x, &y);
00165 joy_msg.header.stamp = nh.now();
00166 joy_msg.axes[0] = y; joy_msg.axes[0] /=100;
00167 joy_msg.axes[1] = x; joy_msg.axes[1] /=100;
00168 pub_joy.publish(&joy_msg);
00169 }
00170
00171 if (millis() > touch_robot_time){
00172 bool c0 = touch_robot.capSense(5) > 750;
00173 if (c0 != prior_robot_state) {
00174 touch_msg.data = c0;
00175 pub_touch.publish(&touch_msg);
00176 prior_robot_state = c0;
00177 }
00178 }
00179 delay(10);
00180 }