Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
bhepp
Date:
Mon Jan 04 16:02:47 2016 +0000
Revision:
54:a59803fcce58
Parent:
53:79a72d752ec4
Child:
55:0587f7ad4bdd
Replaced InterruptMultiplexer with simple InterruptHandler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 24:6f25ba679490 1 // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
manumaet 0:f50e671ffff7 2 #include "mbed.h"
manumaet 26:a65c6f26c458 3 #include "PC.h" // Serial Port via USB for debugging with Terminal
manumaet 27:71178fdb78e1 4 #include "DW1000.h" // our DW1000 device driver
manumaet 44:2e0045042a59 5 #include "MM2WayRanging.h" // our self developed ranging application
bhepp 54:a59803fcce58 6 // HACK
bhepp 54:a59803fcce58 7 //#include "InterruptMultiplexer.h"
bhepp 54:a59803fcce58 8 #include "InterruptHandler.h"
manumaet 44:2e0045042a59 9
bhepp 48:5999e510f154 10 #include "mavlink_bridge/mavlink_bridge.h"
bhepp 48:5999e510f154 11
bhepp 48:5999e510f154 12 #define ANCHOR false
bhepp 48:5999e510f154 13
bhepp 48:5999e510f154 14 using namespace ait;
manumaet 44:2e0045042a59 15
bhepp 50:50b8aea54a51 16 const int SPI_FREQUENCY = 1000000;
bhepp 52:94688f414dcd 17 const int NUM_OF_ANCHORS = 1;
bhepp 52:94688f414dcd 18 const int ANCHOR_ADDRESS_OFFSET = 10;
naegelit 53:79a72d752ec4 19 const bool USE_NLOS_SETTINGS = false;
bhepp 48:5999e510f154 20 const PinName DW_RESET_PIN = D15;
bhepp 48:5999e510f154 21 const PinName DW_IRQ_PIN = D14;
bhepp 48:5999e510f154 22 const PinName DW_MOSI_PIN = D11;
bhepp 48:5999e510f154 23 const PinName DW_MISO_PIN = D12;
bhepp 48:5999e510f154 24 const PinName DW_SCLK_PIN = D13;
bhepp 48:5999e510f154 25 #if ANCHOR
naegelit 53:79a72d752ec4 26 const bool MAVLINK_COMM = false;
naegelit 53:79a72d752ec4 27 const int NUM_OF_DW_UNITS = 1;
naegelit 53:79a72d752ec4 28 const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10};
bhepp 48:5999e510f154 29 #else
bhepp 52:94688f414dcd 30 // const bool MAVLINK_COMM = true;
naegelit 53:79a72d752ec4 31 const bool MAVLINK_COMM = false;
naegelit 53:79a72d752ec4 32 const int NUM_OF_DW_UNITS = 2;
naegelit 53:79a72d752ec4 33 //const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D9, D8,D10};
naegelit 53:79a72d752ec4 34 //const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10,D9,D8};
naegelit 53:79a72d752ec4 35 //const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10,D9,D8,D7};
naegelit 53:79a72d752ec4 36 const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D9,D10};
naegelit 53:79a72d752ec4 37 //const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10};
bhepp 48:5999e510f154 38 #endif
bhepp 48:5999e510f154 39
bhepp 48:5999e510f154 40 PC pc(USBTX, USBRX, 115200); // USB UART Terminal
bhepp 48:5999e510f154 41
naegelit 53:79a72d752ec4 42 void rangeAndDisplayAll(MM2WayRanging& node, MAVLinkBridge& mb, Timer& timer)
naegelit 53:79a72d752ec4 43 {
bhepp 52:94688f414dcd 44 for (int i = 0; i < NUM_OF_ANCHORS; i++) {
bhepp 52:94688f414dcd 45 uint8_t remote_address = ANCHOR_ADDRESS_OFFSET + i;
bhepp 52:94688f414dcd 46 node.requestRanging(remote_address);
bhepp 52:94688f414dcd 47
manumaet 0:f50e671ffff7 48
naegelit 53:79a72d752ec4 49 pc.printf("%d - %d> dist = %.4f\r\n", node.address, remote_address, node.distances[remote_address]);
bhepp 48:5999e510f154 50
manumaet 44:2e0045042a59 51 }
manumaet 44:2e0045042a59 52 }
naegelit 53:79a72d752ec4 53 //continue;
manumaet 45:01a33363bc21 54
manumaet 45:01a33363bc21 55
manumaet 46:6398237672a0 56 struct __attribute__((packed, aligned(1))) DistancesFrame {
bhepp 48:5999e510f154 57 uint8_t source;
bhepp 48:5999e510f154 58 uint8_t destination;
bhepp 48:5999e510f154 59 uint8_t type;
bhepp 48:5999e510f154 60 float dist[4];
bhepp 48:5999e510f154 61 };
manumaet 45:01a33363bc21 62
bhepp 48:5999e510f154 63
bhepp 48:5999e510f154 64 // -----------------------------------------------------------------------------------------------
manumaet 46:6398237672a0 65 void altCallbackRX();
manumaet 45:01a33363bc21 66
naegelit 53:79a72d752ec4 67 int main()
naegelit 53:79a72d752ec4 68 {
naegelit 53:79a72d752ec4 69 //first set the CS to high
naegelit 53:79a72d752ec4 70 DigitalOut test1(D9);
naegelit 53:79a72d752ec4 71 DigitalOut test2(D10);
naegelit 53:79a72d752ec4 72 test1=1;
naegelit 53:79a72d752ec4 73 test2=1;
naegelit 53:79a72d752ec4 74
naegelit 53:79a72d752ec4 75 wait(0.1);
bhepp 50:50b8aea54a51 76 UART_Mbed uart(USBTX, USBRX, 115200);
bhepp 50:50b8aea54a51 77 MAVLinkBridge mb(&uart);
bhepp 50:50b8aea54a51 78
bhepp 50:50b8aea54a51 79 if (!MAVLINK_COMM) {
bhepp 50:50b8aea54a51 80 pc.printf("==== AIT UWB Range ====\r\n");
bhepp 50:50b8aea54a51 81 }
bhepp 48:5999e510f154 82
bhepp 48:5999e510f154 83 SPI spi(DW_MOSI_PIN, DW_MISO_PIN, DW_SCLK_PIN);
bhepp 50:50b8aea54a51 84 spi.format(8, 0); // Setup the spi for standard 8 bit data and SPI-Mode 0 (GPIO5, GPIO6 open circuit or ground on DW1000)
bhepp 48:5999e510f154 85 // NOTE: Minimum Frequency 1MHz. Below it is now working. Could be something with the activation and deactivation of interrupts.
bhepp 50:50b8aea54a51 86 spi.frequency(SPI_FREQUENCY); // with a 1MHz clock rate (worked up to 49MHz in our Test)
bhepp 50:50b8aea54a51 87
bhepp 50:50b8aea54a51 88 // Setup interrupt pin
bhepp 54:a59803fcce58 89 // HACK
bhepp 54:a59803fcce58 90 //InterruptMultiplexer irq_mp(DW_IRQ_PIN);
bhepp 54:a59803fcce58 91 //irq_mp.getIRQ().rise(&irq_mp, &InterruptMultiplexer::trigger); // attach interrupt handler to rising edge of interrupt pin from DW1000
bhepp 50:50b8aea54a51 92
bhepp 50:50b8aea54a51 93 Timer timer;
bhepp 50:50b8aea54a51 94 timer.start();
bhepp 50:50b8aea54a51 95
naegelit 53:79a72d752ec4 96
bhepp 48:5999e510f154 97
naegelit 53:79a72d752ec4 98 DW1000* dw_array[NUM_OF_DW_UNITS + 0];
naegelit 53:79a72d752ec4 99 MM2WayRanging* node_array[NUM_OF_DW_UNITS + 0]; // Instance of the two way ranging algorithm
bhepp 48:5999e510f154 100
bhepp 50:50b8aea54a51 101 if (!MAVLINK_COMM) {
bhepp 50:50b8aea54a51 102 pc.printf("Performing hardware reset of UWB modules\r\n");
bhepp 50:50b8aea54a51 103 }
bhepp 48:5999e510f154 104 // == IMPORTANT == Create all DW objects first (this will cause a reset of the DW module)
bhepp 48:5999e510f154 105 DW1000::hardwareReset(DW_RESET_PIN);
naegelit 53:79a72d752ec4 106 for (int i = 0; i < 2; ++i) {
naegelit 53:79a72d752ec4 107
bhepp 54:a59803fcce58 108 // HACK
bhepp 54:a59803fcce58 109 InterruptHandler irq_mp(DW_IRQ_PIN);
bhepp 54:a59803fcce58 110 irq_mp.getIRQ().rise(&irq_mp, &InterruptHandler::trigger); // attach interrupt handler to rising edge of interrupt pin from DW1000
bhepp 54:a59803fcce58 111
bhepp 48:5999e510f154 112 dw_array[i] = new DW1000(spi, irq_mp, DW_CS_PINS[i]); // Device driver instanceSPI pins: (MOSI, MISO, SCLK, CS, IRQ, RESET)
naegelit 53:79a72d752ec4 113
naegelit 53:79a72d752ec4 114 // dw_array[1] = new DW1000(spi, irq_mp, DW_CS_PINS[1]); // Device driver instanceSPI pins: (MOSI, MISO, SCLK, CS, IRQ, RESET)
bhepp 48:5999e510f154 115
bhepp 48:5999e510f154 116 DW1000& dw = *dw_array[i];
naegelit 53:79a72d752ec4 117
naegelit 53:79a72d752ec4 118 wait(0.1) ;
naegelit 53:79a72d752ec4 119
naegelit 53:79a72d752ec4 120 dw.setEUI(0xFAEDCD01FAEDCD01 + i);
naegelit 53:79a72d752ec4 121 wait(0.1) ;
naegelit 53:79a72d752ec4 122 // basic methods called to check if we have a working SPI connection
bhepp 48:5999e510f154 123 node_array[i] = new MM2WayRanging(*dw_array[i]);
bhepp 48:5999e510f154 124
naegelit 53:79a72d752ec4 125 pc.printf("\r\nUnit %d\r\n", i);
naegelit 53:79a72d752ec4 126 pc.printf("\r\nDecaWave 1.0 up and running!\r\n"); // Splashscreen
naegelit 53:79a72d752ec4 127 pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID());
naegelit 53:79a72d752ec4 128 pc.printf("EUI register: %016llX\r\n", dw.getEUI());
naegelit 53:79a72d752ec4 129 pc.printf("Voltage: %.2fV\r\n", dw.getVoltage());
naegelit 53:79a72d752ec4 130
naegelit 53:79a72d752ec4 131
bhepp 48:5999e510f154 132 MM2WayRanging& node = *node_array[i];
bhepp 48:5999e510f154 133 node.isAnchor = ANCHOR; // declare as anchor or beacon
bhepp 48:5999e510f154 134 if (ANCHOR) {
bhepp 48:5999e510f154 135 node.address = ANCHOR_ADDRESS_OFFSET + i;
bhepp 48:5999e510f154 136 if (!MAVLINK_COMM)
bhepp 48:5999e510f154 137 pc.printf("This node is Anchor node %d\r\n", node.address);
bhepp 48:5999e510f154 138 } else {
bhepp 48:5999e510f154 139 node.address = i;
bhepp 48:5999e510f154 140 if (!MAVLINK_COMM)
bhepp 50:50b8aea54a51 141 pc.printf("This node is a Beacon\r\n");
bhepp 48:5999e510f154 142 }
naegelit 53:79a72d752ec4 143
naegelit 53:79a72d752ec4 144 uint8_t remote_address = ANCHOR_ADDRESS_OFFSET + 0;
naegelit 53:79a72d752ec4 145 node.requestRanging(remote_address);
naegelit 53:79a72d752ec4 146 rangeAndDisplayAll(node, mb, timer);
naegelit 53:79a72d752ec4 147 irq_mp.clear();
naegelit 53:79a72d752ec4 148 wait(2);
manumaet 47:b6120c152ad1 149 }
bhepp 48:5999e510f154 150
naegelit 53:79a72d752ec4 151 // wait(1);
naegelit 53:79a72d752ec4 152 // MM2WayRanging& node = *node_array[1];
naegelit 53:79a72d752ec4 153 // uint8_t remote_address = ANCHOR_ADDRESS_OFFSET + 0;
naegelit 53:79a72d752ec4 154 // node.requestRanging(remote_address);
naegelit 53:79a72d752ec4 155 // rangeAndDisplayAll(node, mb, timer);
bhepp 50:50b8aea54a51 156 if (!MAVLINK_COMM)
bhepp 50:50b8aea54a51 157 pc.printf("Entering main loop\r\n");
bhepp 51:e9391d04af00 158
naegelit 53:79a72d752ec4 159 //for (int i = 0; i < 2; ++i) {
naegelit 53:79a72d752ec4 160 //
naegelit 53:79a72d752ec4 161 // if (ANCHOR) {
naegelit 53:79a72d752ec4 162 // pc.printf("."); // to see if the core and output is working
naegelit 53:79a72d752ec4 163 // wait_ms(1);
naegelit 53:79a72d752ec4 164 // } else {
naegelit 53:79a72d752ec4 165 // for (int j = 0; j < NUM_OF_DW_UNITS; ++j) {
naegelit 53:79a72d752ec4 166 // MM2WayRanging& node = *node_array[j];
naegelit 53:79a72d752ec4 167 // rangeAndDisplayAll(node, mb, timer);
naegelit 53:79a72d752ec4 168 // wait(0.1);
naegelit 53:79a72d752ec4 169 // }
naegelit 53:79a72d752ec4 170 // }
naegelit 53:79a72d752ec4 171 // }
bhepp 50:50b8aea54a51 172
bhepp 54:a59803fcce58 173 while (true)
bhepp 54:a59803fcce58 174 {
bhepp 54:a59803fcce58 175 }
manumaet 46:6398237672a0 176 }