-
Notifications
You must be signed in to change notification settings - Fork 526
/
TcpHelloWorld.ino
84 lines (70 loc) · 1.84 KB
/
TcpHelloWorld.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* rosserial Publisher Example
* Prints "hello world!"
* This intend to connect to an Arduino Ethernet Shield
* and a rosserial socket server.
* You can launch the rosserial socket server with
* roslaunch rosserial_server socket.launch
* The default port is 11411
*
*/
#include <SPI.h>
#include <Ethernet.h>
// To use the TCP version of rosserial_arduino
#define ROSSERIAL_ARDUINO_TCP
#include <ros.h>
#include <std_msgs/String.h>
// Set the shield settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 177);
// Set the rosserial socket server IP address
IPAddress server(192,168,0,11);
// Set the rosserial socket server port
const uint16_t serverPort = 11411;
ros::NodeHandle nh;
// Make a chatter publisher
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
// Be polite and say hello
char hello[13] = "hello world!";
uint16_t period = 1000;
uint32_t last_time = 0;
void setup()
{
// Use serial to monitor the process
Serial.begin(115200);
// Connect the Ethernet
Ethernet.begin(mac, ip);
// Let some time for the Ethernet Shield to be initialized
delay(1000);
Serial.println("");
Serial.println("Ethernet connected");
Serial.println("IP address: ");
Serial.println(Ethernet.localIP());
// Set the connection to rosserial socket server
nh.getHardware()->setConnection(server, serverPort);
nh.initNode();
// Another way to get IP
Serial.print("IP = ");
Serial.println(nh.getHardware()->getLocalIP());
// Start to be polite
nh.advertise(chatter);
}
void loop()
{
if(millis() - last_time >= period)
{
last_time = millis();
if (nh.connected())
{
Serial.println("Connected");
// Say hello
str_msg.data = hello;
chatter.publish( &str_msg );
} else {
Serial.println("Not Connected");
}
}
nh.spinOnce();
delay(1);
}