Hi I am trying to construct a "navsat" msg and send it to ROS on a Raspberry pi 4 using the rosserial package. I have been unsuccessful, and I am starting to think maybe I am fundamentally expecting sth immpossible. Basically, I am wondering if it is at all possible that both the SIM808 and rosserial package are trying to use the serial connection as below:
#include <Arduino.h>
#include <ros.h>
#include <ros/time.h>
#include <geometry_msgs/Vector3.h>
#include <sensor_msgs/NavSatFix.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
#include <string.h>
#include <Time.h>
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
ros::NodeHandle nh;
sensor_msgs::NavSatFix gpsMsg;
ros::Publisher gps("gps", &gpsMsg);
// Publisher object for filtered IMU
double GPS_la = 0.1;
double GPS_lo = 0.1;
double GPS_ws = 0.1;
double GPS_alt = 0.1;
double GPS_heading = 0.1;
uint16_t GPS_year = 1;
uint8_t GPS_month = 1;
uint8_t GPS_day = 1;
uint8_t GPS_hour = 1;
uint8_t GPS_minute = 1;
uint8_t GPS_second = 1;
uint8_t GPS_centisecond = 1;
#define GPS_Sampling_Time_ms 100
unsigned long currentMillis_GPS = 0;
unsigned long previousMillis_GPS = 0;
bool state = LOW;
#define LED 8
SoftwareSerial mySerial(PIN_TX,PIN_RX);
//The content of messages sent
#define GSM_Initial_MESSAGE "Hello Master, This is AttBot at your service. Tell me what to do!"
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,
void getGPS();
// ================================================================
// === INITIAL SETUP ===
// ================================================================
void setup() {
pinMode(LED, OUTPUT); // Declare the LED as an output
// initialize serial communication
mySerial.begin(9600);
//Serial.begin(57600); //open serial and set the baudrate
// ******** Initialize sim808 module *************
while(!sim808.init())
{
//Serial.print("Sim808 init error\r\n");
delay(1000);
}
delay(3000);
if(sim808.attachGPS()){
//Serial.println("Open the GPS power success");
}else{
//Serial.println("Open the GPS power failure");
}
//Connect to ROS
nh.initNode();
//advertise GPS data
nh.advertise(gps);
//while(!nh.connected()) {nh.spinOnce();}
}
void loop() {
//check the sampling time of acquising GPS data
currentMillis_GPS = millis();
if (currentMillis_GPS - previousMillis_GPS > GPS_Sampling_Time_ms) {
getGPS();
//************* Turn off the GPS power ************
sim808.detachGPS();
previousMillis_GPS = currentMillis_GPS;
//publish GPS data
gpsMsg.header.stamp = nh.now();
gpsMsg.header.frame_id = "map";
gpsMsg.latitude = GPS_la;
gpsMsg.longitude = GPS_lo;
gpsMsg.altitude = GPS_alt;
gps.publish(&gpsMsg);
}
nh.spinOnce();
delay(20);
}
void getGPS(){
while(!sim808.attachGPS())
{
//Serial.println("Open the GPS power failure");
}
delay(80);
//Serial.println("Open the GPS power success");
digitalWrite(LED, HIGH);
if(!sim808.getGPS())
{
//Serial.println("not getting anything");
stateChange();
}
GPS_la = sim808.GPSdata.lat;
GPS_lo = sim808.GPSdata.lon;
GPS_ws = sim808.GPSdata.speed_kph;
GPS_alt = sim808.GPSdata.altitude;
GPS_heading = sim808.GPSdata.heading;
GPS_year = sim808.GPSdata.year;
GPS_month = sim808.GPSdata.month;
GPS_day = sim808.GPSdata.day;
GPS_hour = sim808.GPSdata.hour;
GPS_minute = sim808.GPSdata.minute;
GPS_second = sim808.GPSdata.second;
GPS_centisecond = sim808.GPSdata.centisecond;
}
I am almost sure that this is the problem with SIM808 serial connection because, I am successfully sending IMU data and motor encoder data with Arduino boards (DUE and UNO) to the Raspberry. Can you recommend me how should I fix this?
PS: I am using mega2560, and:
- no the RAM usage is under 80 %
- yes I have checked the baudrate on "ArduinoHardware.h" to be 9600 and also on my rosnode.
- I have intentially set "iostream=&Serial1;" to make it sure it is different than
mySerial.begin(9600);
- I have checked the cable.
- The sketch works fine without the rosserial
just to save some time.
I guess I am also asking if the SoftwareSerial is using my hardware serial because it doesn't seem so.
I can a cross this feather M0 (Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0 : ID 2772 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits) which extends the serial connections of the mega but I prefer to avoid going in that direction if possible.