Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
main.cpp@64:48ce74eca983, 2016-01-05 (annotated)
- Committer:
- bhepp
- Date:
- Tue Jan 05 09:39:50 2016 +0000
- Revision:
- 64:48ce74eca983
- Parent:
- 62:f043200d39bd
- Parent:
- 63:776a5c2dcef8
- Child:
- 65:4c3bd79b57d2
Merge
Who changed what in which revision?
User | Revision | Line number | New 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 | 55:0587f7ad4bdd | 32 | const int NUM_OF_DW_UNITS = 4; |
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 | 55:0587f7ad4bdd | 35 | const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10,D9,D8,D7}; |
naegelit | 55:0587f7ad4bdd | 36 | //const PinName DW_CS_PINS[NUM_OF_DW_UNITS] = {D10,D9}; |
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 | 55:0587f7ad4bdd | 70 | DigitalOut test1(D7); |
naegelit | 55:0587f7ad4bdd | 71 | DigitalOut test2(D8); |
naegelit | 55:0587f7ad4bdd | 72 | DigitalOut test3(D9); |
naegelit | 55:0587f7ad4bdd | 73 | DigitalOut test4(D10); |
naegelit | 53:79a72d752ec4 | 74 | test1=1; |
naegelit | 53:79a72d752ec4 | 75 | test2=1; |
naegelit | 55:0587f7ad4bdd | 76 | test3=1; |
naegelit | 55:0587f7ad4bdd | 77 | test4=1; |
naegelit | 53:79a72d752ec4 | 78 | |
naegelit | 53:79a72d752ec4 | 79 | wait(0.1); |
bhepp | 50:50b8aea54a51 | 80 | UART_Mbed uart(USBTX, USBRX, 115200); |
bhepp | 50:50b8aea54a51 | 81 | MAVLinkBridge mb(&uart); |
bhepp | 50:50b8aea54a51 | 82 | |
bhepp | 50:50b8aea54a51 | 83 | if (!MAVLINK_COMM) { |
bhepp | 50:50b8aea54a51 | 84 | pc.printf("==== AIT UWB Range ====\r\n"); |
bhepp | 50:50b8aea54a51 | 85 | } |
bhepp | 48:5999e510f154 | 86 | |
bhepp | 48:5999e510f154 | 87 | SPI spi(DW_MOSI_PIN, DW_MISO_PIN, DW_SCLK_PIN); |
bhepp | 50:50b8aea54a51 | 88 | 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 | 89 | // NOTE: Minimum Frequency 1MHz. Below it is now working. Could be something with the activation and deactivation of interrupts. |
bhepp | 50:50b8aea54a51 | 90 | spi.frequency(SPI_FREQUENCY); // with a 1MHz clock rate (worked up to 49MHz in our Test) |
bhepp | 50:50b8aea54a51 | 91 | |
bhepp | 50:50b8aea54a51 | 92 | // Setup interrupt pin |
bhepp | 54:a59803fcce58 | 93 | // HACK |
bhepp | 54:a59803fcce58 | 94 | //InterruptMultiplexer irq_mp(DW_IRQ_PIN); |
bhepp | 54:a59803fcce58 | 95 | //irq_mp.getIRQ().rise(&irq_mp, &InterruptMultiplexer::trigger); // attach interrupt handler to rising edge of interrupt pin from DW1000 |
bhepp | 50:50b8aea54a51 | 96 | |
bhepp | 50:50b8aea54a51 | 97 | Timer timer; |
bhepp | 50:50b8aea54a51 | 98 | timer.start(); |
bhepp | 50:50b8aea54a51 | 99 | |
naegelit | 53:79a72d752ec4 | 100 | |
bhepp | 48:5999e510f154 | 101 | |
naegelit | 53:79a72d752ec4 | 102 | DW1000* dw_array[NUM_OF_DW_UNITS + 0]; |
naegelit | 53:79a72d752ec4 | 103 | MM2WayRanging* node_array[NUM_OF_DW_UNITS + 0]; // Instance of the two way ranging algorithm |
bhepp | 63:776a5c2dcef8 | 104 | InterruptHandler* irq_mp_ptr_array[NUM_OF_DW_UNITS]; |
bhepp | 48:5999e510f154 | 105 | |
bhepp | 50:50b8aea54a51 | 106 | if (!MAVLINK_COMM) { |
bhepp | 50:50b8aea54a51 | 107 | pc.printf("Performing hardware reset of UWB modules\r\n"); |
bhepp | 50:50b8aea54a51 | 108 | } |
bhepp | 48:5999e510f154 | 109 | // == IMPORTANT == Create all DW objects first (this will cause a reset of the DW module) |
bhepp | 48:5999e510f154 | 110 | DW1000::hardwareReset(DW_RESET_PIN); |
naegelit | 55:0587f7ad4bdd | 111 | for (int i = 0; i < 4; ++i) { |
naegelit | 53:79a72d752ec4 | 112 | |
bhepp | 54:a59803fcce58 | 113 | // HACK |
bhepp | 63:776a5c2dcef8 | 114 | irq_mp_ptr_array[i] = new InterruptHandler(DW_IRQ_PIN); |
bhepp | 63:776a5c2dcef8 | 115 | InterruptHandler& irq_mp = *irq_mp_ptr_array[i]; |
bhepp | 56:2eba104e663f | 116 | if (i == 0) |
bhepp | 56:2eba104e663f | 117 | { |
bhepp | 56:2eba104e663f | 118 | irq_mp.getIRQ().rise(&irq_mp, &InterruptHandler::trigger); // attach interrupt handler to rising edge of interrupt pin from DW1000 |
bhepp | 56:2eba104e663f | 119 | } |
bhepp | 54:a59803fcce58 | 120 | |
bhepp | 48:5999e510f154 | 121 | 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 | 122 | |
naegelit | 53:79a72d752ec4 | 123 | // 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 | 124 | |
bhepp | 48:5999e510f154 | 125 | DW1000& dw = *dw_array[i]; |
naegelit | 53:79a72d752ec4 | 126 | |
naegelit | 53:79a72d752ec4 | 127 | wait(0.1) ; |
naegelit | 53:79a72d752ec4 | 128 | |
naegelit | 53:79a72d752ec4 | 129 | dw.setEUI(0xFAEDCD01FAEDCD01 + i); |
naegelit | 53:79a72d752ec4 | 130 | wait(0.1) ; |
naegelit | 53:79a72d752ec4 | 131 | // basic methods called to check if we have a working SPI connection |
bhepp | 48:5999e510f154 | 132 | node_array[i] = new MM2WayRanging(*dw_array[i]); |
bhepp | 48:5999e510f154 | 133 | |
naegelit | 53:79a72d752ec4 | 134 | pc.printf("\r\nUnit %d\r\n", i); |
naegelit | 53:79a72d752ec4 | 135 | pc.printf("\r\nDecaWave 1.0 up and running!\r\n"); // Splashscreen |
naegelit | 53:79a72d752ec4 | 136 | pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID()); |
naegelit | 53:79a72d752ec4 | 137 | pc.printf("EUI register: %016llX\r\n", dw.getEUI()); |
naegelit | 53:79a72d752ec4 | 138 | pc.printf("Voltage: %.2fV\r\n", dw.getVoltage()); |
naegelit | 53:79a72d752ec4 | 139 | |
naegelit | 53:79a72d752ec4 | 140 | |
bhepp | 48:5999e510f154 | 141 | MM2WayRanging& node = *node_array[i]; |
bhepp | 48:5999e510f154 | 142 | node.isAnchor = ANCHOR; // declare as anchor or beacon |
bhepp | 48:5999e510f154 | 143 | if (ANCHOR) { |
bhepp | 48:5999e510f154 | 144 | node.address = ANCHOR_ADDRESS_OFFSET + i; |
bhepp | 48:5999e510f154 | 145 | if (!MAVLINK_COMM) |
bhepp | 48:5999e510f154 | 146 | pc.printf("This node is Anchor node %d\r\n", node.address); |
bhepp | 48:5999e510f154 | 147 | } else { |
bhepp | 48:5999e510f154 | 148 | node.address = i; |
bhepp | 48:5999e510f154 | 149 | if (!MAVLINK_COMM) |
bhepp | 50:50b8aea54a51 | 150 | pc.printf("This node is a Beacon\r\n"); |
bhepp | 48:5999e510f154 | 151 | } |
bhepp | 58:b824bdac7cd8 | 152 | node.setInterrupt(false); |
naegelit | 53:79a72d752ec4 | 153 | |
bhepp | 56:2eba104e663f | 154 | /*uint8_t remote_address = ANCHOR_ADDRESS_OFFSET + 0; |
naegelit | 53:79a72d752ec4 | 155 | node.requestRanging(remote_address); |
naegelit | 53:79a72d752ec4 | 156 | rangeAndDisplayAll(node, mb, timer); |
naegelit | 53:79a72d752ec4 | 157 | irq_mp.clear(); |
bhepp | 56:2eba104e663f | 158 | wait(0.5);*/ |
manumaet | 47:b6120c152ad1 | 159 | } |
bhepp | 48:5999e510f154 | 160 | |
naegelit | 59:8fee21cddf92 | 161 | wait(1); |
naegelit | 57:b4e7324b0faf | 162 | |
bhepp | 56:2eba104e663f | 163 | uint8_t remote_address = ANCHOR_ADDRESS_OFFSET + 0; |
naegelit | 57:b4e7324b0faf | 164 | |
naegelit | 57:b4e7324b0faf | 165 | //for (int j = 0; j < NUM_OF_DW_UNITS; ++j) { |
naegelit | 57:b4e7324b0faf | 166 | MM2WayRanging& node = *node_array[0]; |
bhepp | 58:b824bdac7cd8 | 167 | node.setInterrupt(true); |
naegelit | 62:f043200d39bd | 168 | node.requestRanging(remote_address); |
naegelit | 57:b4e7324b0faf | 169 | rangeAndDisplayAll(node, mb, timer); |
bhepp | 58:b824bdac7cd8 | 170 | node.setInterrupt(false); |
naegelit | 57:b4e7324b0faf | 171 | wait(0.1); |
naegelit | 57:b4e7324b0faf | 172 | // } |
naegelit | 57:b4e7324b0faf | 173 | |
naegelit | 57:b4e7324b0faf | 174 | // |
naegelit | 57:b4e7324b0faf | 175 | // MM2WayRanging& node = *node_array[0]; |
naegelit | 57:b4e7324b0faf | 176 | // node.requestRanging(remote_address); |
naegelit | 57:b4e7324b0faf | 177 | // pc.printf("%d - %d> dist = %.4f\r\n", node.address, remote_address, node.distances[remote_address]); |
bhepp | 56:2eba104e663f | 178 | //rangeAndDisplayAll(node, mb, timer); |
bhepp | 56:2eba104e663f | 179 | |
bhepp | 50:50b8aea54a51 | 180 | if (!MAVLINK_COMM) |
bhepp | 50:50b8aea54a51 | 181 | pc.printf("Entering main loop\r\n"); |
bhepp | 51:e9391d04af00 | 182 | |
naegelit | 53:79a72d752ec4 | 183 | //for (int i = 0; i < 2; ++i) { |
naegelit | 53:79a72d752ec4 | 184 | // |
naegelit | 53:79a72d752ec4 | 185 | // if (ANCHOR) { |
naegelit | 53:79a72d752ec4 | 186 | // pc.printf("."); // to see if the core and output is working |
naegelit | 53:79a72d752ec4 | 187 | // wait_ms(1); |
naegelit | 53:79a72d752ec4 | 188 | // } else { |
naegelit | 53:79a72d752ec4 | 189 | // for (int j = 0; j < NUM_OF_DW_UNITS; ++j) { |
naegelit | 53:79a72d752ec4 | 190 | // MM2WayRanging& node = *node_array[j]; |
naegelit | 53:79a72d752ec4 | 191 | // rangeAndDisplayAll(node, mb, timer); |
naegelit | 53:79a72d752ec4 | 192 | // wait(0.1); |
naegelit | 53:79a72d752ec4 | 193 | // } |
naegelit | 53:79a72d752ec4 | 194 | // } |
naegelit | 57:b4e7324b0faf | 195 | // } |
bhepp | 50:50b8aea54a51 | 196 | |
naegelit | 55:0587f7ad4bdd | 197 | while (true) { |
bhepp | 54:a59803fcce58 | 198 | } |
manumaet | 46:6398237672a0 | 199 | } |