ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I will soon open another question to solve other problems I encoutered
2 | No.2 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I will soon open another question to solve other problems succesffully created a ros wifi node that reads accellerations of the embeddded IMU and publish them. I encoutered put here the code, maybe it can be helpful.
3 | No.3 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I succesffully created a ros wifi node that reads accellerations of the embeddded embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,168,73,121);
IPAddress server(192,168,73,125);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "VISINTINI-VARESE";
char pass[] = "royalmango418";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================
enter code here
4 | No.4 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I succesffully created a ros wifi node that reads accellerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,168,73,121);
IPAddress server(192,168,73,125);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "VISINTINI-VARESE";
char pass[] = "royalmango418";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================
enter code here
5 | No.5 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I succesffully created a ros wifi node that reads accellerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,168,73,121);
IPAddress server(192,168,73,125);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "VISINTINI-VARESE";
char pass[] = "royalmango418";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================
6 | No.6 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I succesffully successffully created a ros wifi node that reads accellerations accelerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,168,73,121);
IPAddress server(192,168,73,125);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "VISINTINI-VARESE";
char pass[] = "royalmango418";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================
7 | No.7 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I successffully created a ros wifi node that reads accelerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,168,73,121);
server(192,178,82,123);
IPAddress server(192,168,73,125);
server(192,178,82,126);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "VISINTINI-VARESE";
"network_name;
char pass[] = "royalmango418";
"network_password";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================
8 | No.8 Revision |
Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I successffully created a ros wifi node that reads accelerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.
from the command line run:
roscore
and then, in another terminal.
rosrun rosserial_python serial_node.py tcp
If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.
Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.
I really hope I am helping somebody here!! let me know if it works!!
code:
#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni
// ====================== MASTER DATA =========================
// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,178,82,123);
IPAddress server(192,178,82,126);
WiFiClient client;
//======================= WIFI CLASS DEFINITION ========================
class WiFiHardware {
public:
WiFiHardware() {};
void init() {
// do your initialization here. this probably includes TCP server/client setup
client.connect(server, 11411);//11411
}
// read a byte from the serial port. -1 = failure
int read() {
// implement this method so that it reads a byte from the TCP connection and returns it
// you may return -1 is there is an error; for example if the TCP connection is not open
return client.read(); //will return -1 when it will works
}
// write data to the connection to ROS
void write(uint8_t* data, int length) {
// implement this so that it takes the arguments and writes or prints them to the TCP connection
for(int i=0; i<length; i++)
client.write(data[i]);
}
// returns milliseconds since start of program
unsigned long time() {
return millis(); // easy; did this one for you
}
};
//======================= VARIABLE DEFINITION ========================
char ssid[] = "network_name;
"network_name";
char pass[] = "network_password";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros
std_msgs::Float32 acc_x_msg; // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg; // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg; // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg; // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg; // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg; // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);
//============================ SETUP =====================================
void setup() {
Serial.begin(9600);
// wifi setup:
Serial.println("\nStarting connection to server...");
WiFi.begin(ssid, pass);
Serial.println("\nconnected to server");
delay(10000);
// accellerometer setup:
IMU.begin();
delay(10000);
// ros setup:
nh.initNode();
nh.advertise(pub_acc_x);
nh.advertise(pub_acc_y);
nh.advertise(pub_acc_z);
nh.advertise(pub_om_x);
nh.advertise(pub_om_y);
nh.advertise(pub_om_z);
delay(10000);
}
//============================ LOOP =====================================
void loop() {
// accellerometer managment:
char buffer[8]; // string buffer for use with dtostrf() function
float ax, ay, az; // accelerometer values
float omx,omy, omz; // gyroscope values
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
&& IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(omx, omy, omz)) {
acc_x_msg.data = ax;
pub_acc_x.publish(&acc_x_msg);
acc_y_msg.data = ay;
pub_acc_y.publish(&acc_y_msg);
acc_z_msg.data = az;
pub_acc_z.publish(&acc_z_msg);
om_x_msg.data = omx;
pub_om_x.publish(&om_x_msg);
om_y_msg.data = omy;
pub_om_y.publish(&om_y_msg);
om_z_msg.data = omz;
pub_om_z.publish(&om_z_msg);
}
nh.spinOnce();
delay(200);
}
//========================================================================