Pullrequest bluegiga stdma dongle (#2261)

This commit is contained in:
Mario Coppola
2019-04-09 17:54:03 +02:00
committed by Gautier Hattenberger
parent 0a6d735804
commit f328eb14fa
11 changed files with 4953 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
*.d
*.o
*.elf
bluetooth-proximity
+76
View File
@@ -0,0 +1,76 @@
TARGET = bluetooth-proximity
SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o)
DEPS := $(SRCS:.c=.d)
EMBED ?=
ifeq ($(EMBED),)
CC = gcc
MACHINE = $(shell $(CC) -dumpmachine)
# Windows
ifneq (,$(or $(findstring mingw, $(MACHINE)), $(findstring cygwin, $(MACHINE))))
PLATFORM = WIN
LIBS = -lm -lsetupapi
RM = del
# POSIX
else
PLATFORM = POSIX
LIBS = -lm
endif
all: $(TARGET)
%.o: %.c
$(CC) -O3 -Wall -c -fmessage-length=0 -DPLATFORM_$(PLATFORM) -MMD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" -DPRINT
$(TARGET): $(OBJS)
@echo 'Building target: $@'
$(CC) -o"$(TARGET)" $(OBJS) $(LIBS) $(CFLAG)
@echo 'Finished building target: $@'
clean:
-$(RM) $(OBJS) $(DEPS) $(TARGET)
.PHONY: all clean
else
CROSS_COMPILE ?= arm-linux-gnueabi-
CC := $(CROSS_COMPILE)gcc
CFLAGS := -O3 -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes
LDFLAGS := -Wl,--no-undefined -Wl,--as-needed
all: $(TARGET)
%.o: %.c
$(CC) -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
$(TARGET): $(OBJS)
@echo 'Building target: $@'
$(CC) -o"$(TARGET).elf" $(OBJS) $(LDFLAGS)
@echo 'Finished building target: $@'
clean:
-$(RM) $(OBJS) $(DEPS) $(TARGET).elf
#include $(PAPARAZZI_SRC)/conf/Makefile.linux
DRONE ?= $(PAPARAZZI_HOME)/sw/tools/parrot/ardrone2.py
SUB_DIR = paparazzi
HOST ?= 192.168.1.1
# Program the device and start it.
upload_program: $(TARGET).elf
#$(Q)$(DRONE) --host=$(HOST) upload_file $(TARGET).elf $(SUB_DIR)
$(Q)$(DRONE) --host=$(HOST) insmod cdc-acm.ko
$(Q)$(DRONE) --host=$(HOST) upload_file_and_run $(TARGET).elf $(SUB_DIR)
$(Q)$(DRONE) --host=$(HOST) status
# --host=$(HOST)
endif
.PHONY: all clean upload_program debug
+37
View File
@@ -0,0 +1,37 @@
STDMA_dongle.md
### Description
This tool allows Bluegiga dongle to communicate using STDMA (Self-organized Time-Division Multiple Access).
Using this, drones with a Bluegiga USB Bluetooth dongle can communicate with eachother and exchange their RSSI (i.e., signal strenth, which is a measure of distance). This has been tested with up to 3 ARDrones.
It was used in: Coppola, M., McGuire, K.N., Scheper, K.Y.W. et al. Auton Robot (2018). https://doi.org/10.1007/s10514-018-9760-3
### Commands functions
make clean && make --> makes it for the computer
EMBED=1 --> this builds it for the drone, rather than for your computer
Once connected to the drone --> make upload_program EMBED=1 HOST=192..........
### Setting up the communication between laptops/drones
Turn on the drones and go to:
cd $PAPARAZZI_HOME/sw/tools/STDMA_dongle
The command
make clean && Make
will build `bluetooth_proximity`. Which is an application that can run on your computer. This is very useful for broadcasting from your computer if you want your computer to simulate the presence of a drone, for instance.
You can run it with
./bluetooth_proximity
If you want the program to actually run on the ARDrones, then you have to build their embedded version and upload it. Note that this must be done every time the drone stops being active (e.g. when you change the battery). It starts running as soon as it is uploaded.
To build and run for the drone:
make clean && make EMBED=1
To upload to the drones, select your **DRONE** type in the Makefile or add the option **DRONE=...** to the command (tested with ARDrone):
make upload_program EMBED=1 HOST=192.168.40.200
Note the last numbers may change depending on the IP address of your drones.
Make sure to check that you are connected to the drones when doing this step!
+41
View File
@@ -0,0 +1,41 @@
#ifndef APITYPES_H_
#define APITYPES_H_
#ifdef __GNUC__
#define PACKSTRUCT( decl ) decl __attribute__((__packed__))
#define ALIGNED __attribute__((aligned(0x4)))
#else //msvc
#define PACKSTRUCT( decl ) __pragma( pack(push, 1) ) decl __pragma( pack(pop) )
#define ALIGNED
#endif
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef signed short int16;
typedef unsigned long uint32;
typedef signed char int8;
typedef struct bd_addr_t {
uint8 addr[6];
} bd_addr;
typedef bd_addr hwaddr;
typedef struct {
uint8 len;
uint8 data[];
} uint8array;
typedef struct {
uint8 len;
int8 data[];
} string;
#endif
Binary file not shown.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+338
View File
@@ -0,0 +1,338 @@
//
// Bluegigas Bluetooth Smart Demo Application
// Contact: support@bluegiga.com.
//
// This is free software distributed under the terms of the MIT license reproduced below.
//
// Copyright (c) 2012, Bluegiga Technologies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//
#include <stdio.h>
#include "uart.h"
#ifdef PLATFORM_WIN
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#include <windows.h>
#include <Setupapi.h>
HANDLE serial_handle;
void uart_list_devices()
{
char name[] = "Bluegiga Bluetooth Low Energy";
BYTE *pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
//guid for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
char *str;
char tmp[MAX_PATH + 1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports
0L,
NULL,
DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
return;
}
while (1) {
if (!SetupDiEnumDeviceInfo(
hDevInfo,
n++,
&DeviceInfoData
)) {
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = (BYTE *)malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) {
free(pbuf);
continue;
}
str = (char *)pbuf;
if (sscanf(str, tmp, &i) == 1) {
printf("%s\n", str);
//emit DeviceFound(str,QString("\\\\.\\COM%1").arg(i));
}
free(pbuf);
}
return;
}
int uart_find_serialport(char *name)
{
BYTE *pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
//guid for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
char *str;
char tmp[MAX_PATH + 1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports
0L,
NULL,
DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
return -1;
}
while (1) {
if (!SetupDiEnumDeviceInfo(
hDevInfo,
n++,
&DeviceInfoData
)) {
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) {
free(pbuf);
continue;
}
str = (char *)pbuf;
if (sscanf(str, tmp, &i) == 1) {
free(pbuf);
SetupDiDestroyDeviceInfoList(hDevInfo);
return i;
}
free(pbuf);
}
return -1;
}
int uart_open(char *port)
{
char str[20];
snprintf(str, sizeof(str) - 1, "\\\\.\\%s", port);
serial_handle = CreateFileA(str,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,//FILE_FLAG_OVERLAPPED,
NULL);
if (serial_handle == INVALID_HANDLE_VALUE) {
return -1;
}
return 0;
}
void uart_close()
{
CloseHandle(serial_handle);
}
int uart_tx(int len, unsigned char *data)
{
DWORD r, written;
while (len) {
r = WriteFile(serial_handle,
data,
len,
&written,
NULL
);
if (!r) {
return -1;
}
len -= written;
data += len;
}
return 0;
}
int uart_rx(int len, unsigned char *data, int timeout_ms)
{
int l = len;
DWORD r, rread;
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = timeout_ms;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(
serial_handle,
&timeouts
);
while (len) {
r = ReadFile(serial_handle,
data,
len,
&rread,
NULL
);
if (!r) {
l = GetLastError();
if (l == ERROR_SUCCESS) {
return 0;
}
return -1;
} else {
if (rread == 0) {
return 0;
}
}
len -= rread;
data += len;
}
return l;
}
#else
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int serial_handle;
void uart_list_devices() {}
int uart_open(char *port)
{
struct termios options;
int i;
serial_handle = open(port, (O_RDWR | O_NOCTTY /*| O_NONBLOCK | O_NDELAY*/));
if (serial_handle < 0) {
return errno;
}
/*
* Get the current options for the port...
*/
tcgetattr(serial_handle, &options);
/*
* Set the baud rates to 115200...
*/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
//cfsetispeed(&options, B921600);
//cfsetospeed(&options, B921600);
/*
* Enable the receiver and set parameters ...
*/
options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS | HUPCL);
options.c_cflag |= (CS8 | CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | IEXTEN);
options.c_iflag &= ~(INPCK | IXON | IXOFF | IXANY | ICRNL);
options.c_oflag &= ~(OPOST | ONLCR);
//printf( "size of c_cc = %d\n", sizeof( options.c_cc ) );
for (i = 0; i < sizeof(options.c_cc); i++) {
options.c_cc[i] = _POSIX_VDISABLE;
}
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
/*
* Set the new options for the port...
*/
tcsetattr(serial_handle, TCSAFLUSH, &options);
return 0;
}
void uart_close()
{
close(serial_handle);
}
int uart_tx(int len, unsigned char *data)
{
ssize_t written;
while (len) {
written = write(serial_handle, data, len);
if (!written) {
return -1;
}
len -= written;
data += len;
}
return 0;
}
int uart_rx(int len, unsigned char *data, int timeout_ms)
{
int l = len;
ssize_t rread;
struct termios options;
tcgetattr(serial_handle, &options);
options.c_cc[VTIME] = timeout_ms / 100;
options.c_cc[VMIN] = 0;
tcsetattr(serial_handle, TCSANOW, &options);
while (len) {
rread = read(serial_handle, data, len);
if (!rread) {
return 0;
} else if (rread < 0) {
return -1;
}
len -= rread;
data += len;
}
return l;
}
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef UART_H
#define UART_H
void uart_list_devices();
int uart_find_serialport(char *name);
int uart_open(char *port);
void uart_close();
int uart_tx(int len, unsigned char *data);
int uart_rx(int len, unsigned char *data, int timeout_ms);
#endif // UART_H