Working Maveric

As5047.h

Committer:
mettrque
Date:
2017-08-22
Revision:
10:36a2131f636c
Parent:
9:b6a9d1c44ed9

File content as of revision 10:36a2131f636c:

/*
 * As5047.h
 * Copyright (c) 2017, ZHAW
 * All rights reserved.
 *
 *  Created on: 03.05.2017
 *      Author: Quentin Mettraux
 */

#ifndef _AS5047_H_
#define _AS5047_H_

#include <stdint.h>
#include <mbed.h>
#include "Signal.h"

/**
 * Interfacing with the AMS AS5047P magnetic rotary sensor using SPI protocol
 * AS5047 uses 16-bit transfer;
 * We use two 8-bit transfers for compatibility with 8-bit SPI master devices
 * SPI protocol:
 *   Mode = 1: 
 *   clock polarity = 0 --> clock pulse is high
 *   clock phase = 1 --> sample on falling edge of clock pulse
 */
class As5047 {

public:

    As5047(PinName mosi, PinName miso, PinName sck, PinName cs);
    virtual ~As5047();

    static const int kNumSensorBits;
    static const uint16_t kCountsPerRev;
    static const uint16_t kMask;
    static const int kParity;   
    static const int kSpiFrequency;
    static const int kSpiBitsPerTransfer;
    static const int kSpiMode; 
    static const float kDegPerRev;
    static const float kRadPerRev;

    // AS5047 flags
    typedef enum {
        AS_FLAG_PARITY          = 0x8000,
        AS_FLAG_READ            = 0x4000,
    } As5047Flag;
    
    // AS5047 commands
    typedef enum {
        AS_CMD_NOP              = 0x0000,
        AS_CMD_ERROR            = 0x0001 | AS_FLAG_READ,   // Reads error register of sensor and clear error flags
        AS_CMD_DIAGNOSTICS      = 0x3FFD | AS_FLAG_READ,   // Reads automatic gain control and diagnostics info
        AS_CMD_MAGNITUDE        = 0x3FFE | AS_FLAG_READ,
        AS_CMD_ANGLE            = 0x3FFF | AS_FLAG_PARITY | AS_FLAG_READ,
    } As5047Command;
    
    // AS5047 diagnostics
    typedef enum {
        AS_DIAG_CORDIC_OVERFLOW = 0x0200,
        AS_DIAG_HIGH_MAGNETIC   = 0x0400,
        AS_DIAG_LOW_MAGNETIC    = 0x0800,
    } As5047Diagnostics;

    static bool CheckParity(int n);
    uint16_t get_read_buffer(); 
    uint16_t get_angle_buffer(); 
    uint16_t get_angle_offset();
    bool get_directions_();
    int getAngle();
    float getAngleRatio();
    float getAngleDegrees(); 
    float getAngleRadians();   
    void setDirection(bool dir);
    void setOffset(uint16_t offset);    
    void setOffsetRatio(float offset_ratio);     
    void setOffsetDegrees(float offset_degrees); 
    void setOffsetRadians(float offset_radians);
    void SelectChip();
    void DeselectChip();
    void Transfer(As5047Command cmd); 
    
private: 

    static const uint32_t   STACK_SIZE = 1024;          // stack size of thread, given in [bytes]
    static const float      PERIOD = 0.02f;              // the period of the timer interrupt, given in [s]
    
    DigitalOut              chip_;            // chip select port
    SPI                     spi_;             // mbed spi communiation object
    
    uint16_t                read_buffer_;     // buffer for results from last transfer
    uint16_t                angle_buffer_;    // buffer for angle results from last transfer
    uint16_t                angle_offset_;    // offset array for each sensor
    bool                    directions_;      // direction true positive, false negative
    
    As5047Command           last_command_;    // command sent during last Transfer
    
    Signal                  signal;
    Thread                  thread;
    Ticker                  ticker;
    
    void                    sendSignal();
    void                    run();

    
};
#endif /* AS5047_H_ */