Commit 0cf0fce9 authored by Nicolas Federico Furquez Morena's avatar Nicolas Federico Furquez Morena
Browse files

Merge branch 'ordenando_codigo' into 'develop'

Ordenando codigo

See merge request !1
parents 447e7a66 c644c6ca
**/env
.directory
**/data/**
**/__pycache__/**
**/**/.vscode
**/**/*.pyc
**/env/**
{
"python.formatting.provider": "black"
}
\ No newline at end of file
#ifndef ROS_LOCALINO_HARDWARE_H_
#define ROS_LOCALINO_HARDWARE_H_
#if ARDUINO>=100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <SPI.h>
#include <USBSerial.h>
#define WIFI Serial1
#define SERIAL_CLASS USBSerial
class LocalinoHardware {
public:
// connection pins
static const uint8_t PIN_RST = PA0; // reset pin
static const uint8_t PIN_IRQ = PA1; // irq pin
static const uint8_t PIN_SS = PA4; // spi select pin
static const uint8_t LED1 = PB0; // led1
static const uint8_t LED2 = PB1; // led2
LocalinoHardware() {
iostream = &Serial;
baud_ = 115200;
}
void setBaud(long baud) {
this->baud_= baud;
}
long getBaud() {
return baud_;
}
void init() {
iostream->begin(baud_);
}
int read() {
return iostream->read();
}
void write(uint8_t* data, int length) {
for(int i=0; i<length; i++) {
iostream->write(data[i]);
}
}
unsigned long time() {
return millis();
}
protected:
SERIAL_CLASS* iostream;
long baud_;
};
#endif
/**
*
* @todo
* - move strings to flash (less RAM consumption)
* - fix deprecated convertation form string to char* startAsAnchor
* - give example description
*
*
*
*
* PROYECTO:
* VERSION:
* -20191103-2109: se deja funcionando con los parametros para que funcione en NLOS
*
*/
#include <ros.h>
#include <std_msgs/UInt32.h>
#include <std_msgs/String.h>
#include <SPI.h>
#include "DW1000Ranging.h"
#include "DW1000Time.h"
#include "DW1000.h"
#include <localization_localino/Distance.h>
#include "LocalinoHardware.h"
// ======================= ROS ==========================
ros::NodeHandle_<LocalinoHardware> _nh;
// Callback declaration
void ROS_newAntennaDelayValue(const std_msgs::UInt32& value);
const unsigned int BUFFER_SIZE = 100;
std_msgs::String str_msg;
localization_localino::Distance distancia;
ros::Publisher localization("localization", &distancia);
ros::Subscriber<std_msgs::UInt32> subscriber("antenna_delay_param", &ROS_newAntennaDelayValue);
char _bufferSerial[BUFFER_SIZE];
// ======================================================
char anchorid[90] = "02:00:5B:D5:A9:9A:E2:9C"; // defines the ID of the Anchor, MUST be unique
//String hostip = "192.168.4.123";
String hostip = "255.255.255.255";
uint16_t _currentShortAddress;
uint32_t _antennaDelay = 16469;
void setup() {
//Serial.begin(115200);
Serial1.begin(115200);
// indicate if we are an anchor
pinMode(LocalinoHardware::LED2, OUTPUT);
pinMode(LocalinoHardware::LED1, OUTPUT);
delay(1000);
DW1000Ranging_setup();
// finished
digitalWrite(LocalinoHardware::LED2, HIGH);
digitalWrite(LocalinoHardware::LED1, HIGH);
}
void DW1000Ranging_setup() {
//init the configuration
DW1000Ranging.initCommunication(LocalinoHardware::PIN_RST, LocalinoHardware::PIN_SS, LocalinoHardware::PIN_IRQ); //Reset, CS, IRQ pin
//define the sketch as anchor. It will be great to dynamically change the type of module
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachBlinkDevice(newBlink);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
DW1000Ranging.attachNewLog(newLog);
//Enable the filter to smooth the distance
//DW1000Ranging.useRangeFilter(true);
// DW1000Time delay = new DW1000Time((float)16418.744);
DW1000.setAntennaDelay(_antennaDelay);
//we start the module as an anchor
//DW1000Ranging.startAsAnchor(anchorid, DW1000.MODE_LONGDATA_RANGE_ACCURACY, false);
DW1000Ranging.startAsAnchor(anchorid, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
byte* shortAddress = DW1000Ranging.getCurrentShortAddress();
_currentShortAddress = shortAddress[1] * 256 + shortAddress[0];
ROS_setup();
}
void ROS_setup() {
_nh.initNode();
_nh.subscribe(subscriber);
_nh.advertise(localization);
}
void loop() {
DW1000Ranging.loop();
_nh.spinOnce();
delay(1);
}
void newRange() {
// indicate TX
digitalWrite(LocalinoHardware::LED2, HIGH);
//Mando al serial como si fuera desde un anchor
unsigned long timestamp = millis();
float tagRange = DW1000Ranging.getDistantDevice()->getRange();
float tagPower = DW1000Ranging.getDistantDevice()->getRXPower();
String msg = "R," + String(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX) + "," + String(2) + "," + tagRange + "," + timestamp;
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
enviarSerialRos(DW1000Ranging.getDistantDevice()->getShortAddress(), tagRange,tagPower, 0, msg);
// indicate TX off
digitalWrite(LocalinoHardware::LED2, LOW);
}
void newDevice(DW1000Device* device) {
String msg = "TAG [" + String(_currentShortAddress, HEX) + "] -> N,ranging init; 1 device added! -> short: " + String(device->getShortAddress(), HEX);
enviarSerialRos(1, 0.0 ,0.0,1,msg);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void newBlink(DW1000Device* device) {
String msg = "ANCHOR [" + String(_currentShortAddress, HEX) + "] -> B,blink; 1 device added! -> short: " + String(device->getShortAddress(), HEX);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void inactiveDevice(DW1000Device* device) {
String msg = "ANCHOR [" + String(_currentShortAddress, HEX) + "] -> D,delete inactive device: " + String(device->getShortAddress(), HEX);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void newLog(const char* log) {
String msg = "ANCHOR [" + String(_currentShortAddress, HEX) + "] -> " + log;
//Serial.println(msg);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void enviarSerialRos(int anchor, float distance, float power, int code, String mensaje) {
mensaje.toCharArray(_bufferSerial, BUFFER_SIZE);
distancia.msg = _bufferSerial;
distancia.anchor =anchor ;
distancia.code = code;
distancia.distance = distance;
distancia.power = power;
localization.publish( &distancia );
//_nh.spinOnce();
}
void ROS_newAntennaDelayValue(const std_msgs::UInt32& value){
digitalWrite(LocalinoHardware::LED1, LOW);
_antennaDelay = value.data;
DW1000.setAntennaDelay(_antennaDelay);
DW1000.commitConfiguration();
//String msg = "Se setea AD" + String(_antennaDelay);
//enviarSerialRos(1, 0.0 ,0.0,1,msg);
delay(500);
digitalWrite(LocalinoHardware::LED1, HIGH);
}
#ifndef ROS_LOCALINO_HARDWARE_H_
#define ROS_LOCALINO_HARDWARE_H_
#if ARDUINO>=100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <SPI.h>
#include <USBSerial.h>
#define SERIAL_CLASS USBSerial
class LocalinoHardware
{
public:
LocalinoHardware()
{
iostream = &Serial;
baud_ = 115200;
}
void setBaud(long baud)
{
this->baud_= baud;
}
int getBaud()
{
return baud_;
}
void init()
{
iostream->begin(baud_);
}
int read()
{
return iostream->read();
};
void write(uint8_t* data, int length)
{
for(int i=0; i<length; i++)
{
iostream->write(data[i]);
}
}
unsigned long time()
{
return millis();
}
protected:
SERIAL_CLASS* iostream;
long baud_;
};
#endif
/**
@todo
- move strings to flash (less RAM consumption)
- fix deprecated convertation form string to char* startAsTag
- give example description
PROYECTO:
VERSION:
-20191103-2109: se deja funcionando con los parametros para que funcione en NLOS
*/
#include <ros.h>
#include <localization_localino/Distance.h>
#include "LocalinoHardware.h"
#include <SPI.h>
#include "DW1000Ranging.h"
ros::NodeHandle_<LocalinoHardware> nh;
const unsigned int BUFFER_SIZE = 100;
localization_localino::Distance distancia;
ros::Publisher localization("localization", &distancia);
char _bufferSerial[BUFFER_SIZE];
// connection pins
const uint8_t PIN_RST = PA0; // reset pin
const uint8_t PIN_IRQ = PA1; // irq pin
const uint8_t PIN_SS = PA4; // spi select pin
const uint8_t LED1 = PB0; // led1
const uint8_t LED2 = PB1; // led2
char tagid[90] = "00:01:5B:D5:A9:9A:E2:9C"; // defines the ID of the Anchor, MUST be unique
String hostip = "255.255.255.255";
uint16_t _currentShortAddress;
void setup() {
Serial1.begin(115200);
// indicate if we are an anchor
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
delay(1000);
//init the configuration
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
//define the sketch as anchor. It will be great to dynamically change the type of module
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
//DW1000Ranging.attachNewLog(newLog);
//Enable the filter to smooth the distance
DW1000Ranging.useRangeFilter(true);
DW1000.setAntennaDelay(16418.744);
//we start the module as a tag
//DW1000Ranging.startAsTag(tagid, DW1000.MODE_LONGDATA_RANGE_ACCURACY, false);
DW1000Ranging.startAsTag(tagid, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
byte* shortAddress = DW1000Ranging.getCurrentShortAddress();
_currentShortAddress = shortAddress[1] * 256 + shortAddress[0];
nh.initNode();
nh.advertise(localization); //TODO cambiar si hacemos mensaje nuestro
// finished
digitalWrite(LED1, HIGH);
}
void loop() {
digitalWrite(LED2, HIGH);
DW1000Ranging.loop();
digitalWrite(LED2, LOW);
}
void newRange() {
// indicate TX
digitalWrite(LED1, HIGH);
byte* _ownAddress = DW1000Ranging.getDistantDevice()->getByteAddress();
//Mando al serial como si fuera desde un anchor
unsigned long timestamp = millis();
float tagRange = DW1000Ranging.getDistantDevice()->getRange();
float tagPower = DW1000Ranging.getDistantDevice()->getRXPower();
String msg = "R," + String(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX) + "," + String(2) + "," + tagRange + "," + timestamp;
Serial1.println("SendToUDP " + String(hostip) + ",10001," + msg);
enviarSerialRos(DW1000Ranging.getDistantDevice()->getShortAddress(), tagRange,tagPower, 0, msg);
// indicate TX off
digitalWrite(LED1, LOW);
}
void newDevice(DW1000Device* device) {
String msg = "TAG [" + String(_currentShortAddress, HEX) + "] -> N,ranging init; 1 device added! -> short: " + String(device->getShortAddress(), HEX);
enviarSerialRos(1, 0.0 ,0.0,1,msg);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void inactiveDevice(DW1000Device* device) {
String msg = "TAG [" + String(_currentShortAddress, HEX) + "] -> D,delete inactive device: " + String(device->getShortAddress(), HEX);
enviarSerialRos(1, 0.0 ,0.0,1,msg);
Serial1.println("SendToUDP " + String(hostip) + ",10000," + msg);
}
void enviarSerialRos(int anchor, float distance, float power, int code, String mensaje) {
mensaje.toCharArray(_bufferSerial, BUFFER_SIZE);
distancia.msg = _bufferSerial;
distancia.anchor =anchor ;
distancia.code = code;
distancia.distance = distance;
distancia.power = power;
localization.publish( &distancia );
nh.spinOnce();
}
void newLog(const char* log) {
}
cmake_minimum_required(VERSION 3.0.2)
project(localization_localino)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
add_message_files(
FILES
Distance.msg
)
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES localization_localino
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/localization_localino.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/localization_localino_node.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above