Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AS5045.cpp
00001 #include "AS5045.h" 00002 //we have 5040 00003 /** Constructor 00004 * 00005 * @param CS Pin number for the digital output 00006 * 00007 * @note 00008 * PinName CS is the digital output pin number 00009 */ 00010 00011 00012 Timer timer_enc; 00013 AS5045::AS5045(PinName CS) : 00014 _spi(NC,D12, D13), // MBED SPI init 00015 _cs(CS) // Digital output pin init 00016 { 00017 // Set SPI bitwidth9 00018 _spi.format(8, 1); 00019 00020 // Set SPI frequency 00021 _spi.frequency(SPI_FREQ);//SPI_FREQ); 00022 // Set the digital output high 00023 _cs = 1; 00024 } 00025 00026 /** Read tick amount from encoder (position) 00027 * 00028 * @note 00029 * Tick amount is recieved through SPI 00030 */ 00031 int AS5045::getPosition() 00032 { 00033 unsigned int upper, // Upper part of the tick amount integer 00034 lower; // Lower part of the tick amount integer 00035 // 00036 // // Set the chip select pin low 00037 // //timer_enc.reset(); 00038 // timer_enc.start(); 00039 // _cs = 0; 00040 // 00041 // wait_ms(1); 00042 // // Read data from the encoder 00043 // upper = (_spi.write(0x00)) ; 00044 //// lower = (_spi.write(0x00)); 00045 // 00046 //// lower = lower >> 6; 00047 //// upper = (upper >> 6)+lower; 00048 //// upper = upper & 0xffc0; 00049 //// upper = upper >> 6; 00050 // 00051 // // Set the chip select pin high 00052 // _cs = 1; 00053 // //wait_ms(5); 00054 // Return full 9-bits tick amount 00055 // return (upper>>5); 00056 //return upper ; 00057 00058 _cs = 0; 00059 upper = _spi.write(0x00); 00060 lower = _spi.write(0x00); 00061 _cs = 1; 00062 00063 00064 //upper &=~ 0xF0;//mask out the first 4 bits 00065 // EncoderByteData = upper << 8; //shift MSB to correct EncoderByteData in EncoderByteData message 00066 // EncoderByteData += lower; // add LSB to EncoderByteData message to complete message 00067 return ((upper << 2)|(lower >> 6)); 00068 //return EncoderByteData; 00069 } 00070 00071 /** Convert position of the encoder to degrees 00072 * 00073 * @note 00074 * Tick amount is recieved internally 00075 */ 00076 float AS5045::getAngle() 00077 { 00078 // Get data from the encoder 00079 float value = (float)getPosition(); 00080 00081 // Return degrees of rotation of the encoder 00082 return value * RESOLUTION; 00083 }
Generated on Thu Jul 14 2022 20:23:26 by
1.7.2