Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

MM2WayRanging/MM2WayRanging.h

Committer:
bhepp
Date:
2016-01-04
Revision:
58:b824bdac7cd8
Parent:
48:5999e510f154
Child:
60:43be9228b3b9

File content as of revision 58:b824bdac7cd8:

// Adapted from Matthias Grob & Manuel Stalder - ETH Zürich - 2015

#pragma once

#include "mbed.h"
#include "DW1000.h"

#define TIMEUNITS_TO_US       (1/(128*499.2))               // conversion between the decawave timeunits (ca 15.65ps) to microseconds.
#define US_TO_TIMEUNITS       (128*499.2)                   // conversion between microseconds to the decawave timeunits (ca 15.65ps).
#define MMRANGING_2POWER40          1099511627776               // decimal value of 2^40 to correct timeroverflow between timestamps

#define NUM_OF_UWB_ADDRESSES 256


//Predefined delay for the critical answers in the ranging algorithm
//HAS TO BE BIGGER THAN THE PROCESSING TIME OF THE FRAME ON THE NODE
#define ANSWER_DELAY_US             2500                                    //2500 works for 110kbps, 900 for 6.8Mbps
#define ANSWER_DELAY_TIMEUNITS      ANSWER_DELAY_US * (128*499.2)

class MM2WayRanging {

public:
    MM2WayRanging(DW1000& DW);

    void requestRanging(uint8_t remote_address);

    void setInterrupt(bool enable)
    {
        dw.setInterrupt(enable, enable);
    }

    //TODO: Better capsulation on those?
    bool isAnchor;
    uint8_t address; // Identifies the nodes as address and remote_address in rangingframes

    //TODO: Make those PRIVATE!
    float roundtriptimes[NUM_OF_UWB_ADDRESSES]; // Array containing the round trip times to the anchors or the timeout which occured
    float distances[NUM_OF_UWB_ADDRESSES]; // Array containing the finally calculated Distances to the anchors

    struct __attribute__((packed, aligned(1))) ReceptionStats {
        uint16_t std_noise;
        uint16_t preamble_acc_count;
        uint16_t first_path_index;
        uint16_t first_path_amp_1;
        uint16_t first_path_amp_2;
        uint16_t first_path_amp_3;
        uint16_t channel_impulse_response_power;
        uint8_t prf;
    };
    ReceptionStats reception_stats[NUM_OF_UWB_ADDRESSES][3];

    bool overflow;              // TRUE if counter overflows while ranging

private:


    DW1000& dw;
    Timer LocalTimer;

    void callbackRX();
    void callbackTX();
    void sendPingFrame(uint8_t remote_address);
    void sendDelayedAnswer(uint8_t remote_address, uint8_t type, uint64_t rxTimestamp);
    void sendTransferFrame(uint8_t remote_address, int timestamp);

    inline float calibratedDistance(uint8_t remote_address);

    /**
     * These two functions correct the timestamps if the counter had an overflow between measurements
     */
    void correctReceiverTimestamps(uint8_t address);
    void correctSenderTimestamps(uint8_t address);

    int timediffRec;
    int timediffSend;

    enum FrameType{
        PING = 1,
        ANCHOR_RESPONSE,
        BEACON_RESPONSE,
        TRANSFER_FRAME,
        DISTANCES_FRAME
    };

    //the packed attribute makes sure the types only use their respective size in memory (8 bit for uint8_t), otherwise they would always use 32 bit
    //IT IS A GCC SPECIFIC DIRECTIVE
    struct __attribute__((packed, aligned(1))) RangingFrame {
        uint8_t address;
        uint8_t remote_address;
        uint8_t type;
    };

    struct __attribute__((packed, aligned(1))) ExtendedRangingFrame : RangingFrame{
        int signedTime;
        ReceptionStats stats1;
        ReceptionStats stats2;
    };


    RangingFrame rangingFrame;                  // buffer in class for sending a frame (not made locally because then we can recall in the interrupt what was sent)
    ExtendedRangingFrame transferFrame;
    ExtendedRangingFrame receivedFrame;

    uint64_t rxTimestamp;
    uint64_t senderTimestamps[NUM_OF_UWB_ADDRESSES][3];
    uint64_t receiverTimestamps[NUM_OF_UWB_ADDRESSES][3];
    bool acknowledgement[NUM_OF_UWB_ADDRESSES];                   // flag to indicate if ranging has succeeded
    uint32_t tofs[NUM_OF_UWB_ADDRESSES];                          // Array containing time of flights for each node (index is address of node)

};