Advertisement
Guest User

EncoderInterface.h

a guest
Mar 7th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1.  
  2. /*******************************************************************************
  3. * Digital Encoder Library -- By: Andrew Farris                                 *
  4. *                                                                              *
  5. * This library is intended to provide easy access to the AMT203-V absolute     *
  6. * encoder.                                                                     *
  7. *                                                                              *
  8. * This file sets up function prototypes for the actual library.                *
  9. *                                                                              *
  10. * Data flow for the encoder:                                                   *
  11. *     The host issues a command, receives zero or more wait sequence (0xA5),   *
  12. *     then the echo of the command followed by an optional payload.            *
  13. *                                                                              *
  14. *   The sequence for RD_POS goes as follows (from AMT203 datasheet):           *
  15. *       1) issue read command, receive idle character                          *
  16. *       2) issue NOP, receive idle character 0xA5 or 0x10                      *
  17. *       3) repeat step 2 if it is 0xA5                                         *
  18. *       4) issue NOP and receive MSB position (4 bits valid data)              *
  19. *       5) issue NOP and receive LSB position (8 bits valid data)              *
  20. *                                                                              *
  21. * NOTE: Debugging program execution over serial terminal is supported. To turn *
  22. *       on debug messages, add the line #define DEBUG_ENCODER in the program.  *
  23. *       Note that doing so assumes the AVR-Serial library is already loaded    *
  24. *       and initialized.                                                       *
  25. *                                                                              *
  26. *******************************************************************************/
  27.  
  28. #include <WProgram.h>
  29. #include <SPI.h>
  30.  
  31.  
  32. // These are the SPI codes that the Encoder supports
  33. #define NOP_A5          0x00        // Command causes the next data to be read (reply is usually 1 or more WAITs) or an echo of NOP_A5 if there's nothing to do
  34. #define RD_POS          0x10        // Command to Read position.
  35. #define WAIT            0xA5        // This is basically a 'standby' signal from the encoder... it means it's trying to do something
  36. #define SET_ZERO_POINT  0x70        // Sending this command causes us to read from the
  37. #define ZERO_POINT_SET  0x80        // The encoder returns this signal when the zero-point is successfully set (note, it sends WAIT signals until then)
  38. #define NO_CHIP_SELECT  0xFF        // This code is returned by the SPI link if a command is sent, but no chip is selected/responding.
  39.  
  40.  
  41. // Constants for declaring encoder state
  42. #define ENCODER_INACTIVE    0
  43. #define ENCODER_ACTIVE      1
  44. #define ENCODER_NEED_FLUSH  2
  45.  
  46.  
  47. float map_value( float x, float in_min, float in_max, float out_min, float out_max );
  48.     /*
  49.         This function works as a linear interpolator, and returns a floating
  50.         point value.
  51.        
  52.         WARNING: This function does no bounds checking! It is possible for x to
  53.                  be larger than in_max, or smaller than in_min.
  54.     */
  55.    
  56.    
  57. int Test_encoder_SPI_link( int CSBpin );
  58.     /*
  59.         This function tests if a link with a particular Encoder is active by
  60.         sending a null operation, and looking for a known response.
  61.        
  62.         WARNING: This function expects the AVR-SPI library to be active already.
  63.         DO NOT USE BEFORE SETTING UP THE SPI LIBRARY.
  64.        
  65.         The three possible responses are as follows:
  66.             ENCODER_INACTIVE -
  67.                 Value: 0
  68.                 Meaning: The selected encoder is disconnected, or unresponsive.
  69.             ENCODER_ACTIVE -
  70.                 Value: 1
  71.                 Meaning: The selected encoder is ready to send/recieve data.
  72.             ENCODER_NEED_FLUSH -
  73.                 Value: 2
  74.                 Meaning: The selected encoder responded, but has data waiting to
  75.                          be flushed from queue before it can be used.
  76.     */
  77.    
  78.    
  79. int Flush_encoder_cache( int CSBpin );
  80.     /*
  81.         This function should be used to flush any stray data from the encoder
  82.         buffer. The function will cease flush attempts after 100 failures to
  83.         recieve the appropriate response, indicating an issue with the encoder.
  84.         TODO: Change attempt number to account for clearing the encoder's entire
  85.               buffer, not just 'sufficiently large' arbitrary value.
  86.        
  87.         The intended use of this function is to clear leftover position
  88.         data from the encoder after unexpected power loss, disconnection, or
  89.         other operational interuption, which may result in stray data being left
  90.         in the encoder's FIFO.
  91.        
  92.         Returns and their meanings:
  93.             ENCODER_INACTIVE -
  94.                 Value: 0
  95.                 Meaning: The selected encoder is disconnected, or unresponsive.
  96.             ENCODER_ACTIVE -
  97.                 Value: 1
  98.                 Meaning: The selected encoder's FIFO is clear, and
  99.                          is ready to send/recieve data.
  100.             ENCODER_NEED_FLUSH -
  101.                 Value: 2
  102.                 Meaning: The selected encoder responded, either had too much
  103.                          waiting in queue, or
  104.     */
  105.        
  106.        
  107. float Encoder_read_angle( int CSBpin );
  108.     /*
  109.         This function reads the position of the encoder, and returns it as a
  110.         floating point number between 0 and 360.
  111.        
  112.         Return value will be < 0 if an issue occurred.
  113.     */
  114.    
  115.    
  116. void Encoder_setup();
  117.     /*
  118.         This function does some basic setup activities needed to properly use
  119.         the encoder, particularly multiple encoders.
  120.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement