MRD Lab / ServoRingBuffer

Dependents:   SpindleBot_1_5b

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ServoRingBuffer.h Source File

ServoRingBuffer.h

00001 /* mbed Ring Buffer for Servos
00002  *
00003  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00004  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00005  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00006  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00007  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00008  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00009  * THE SOFTWARE.
00010  */
00011 
00012 #ifndef SERVORINGBUFFER_H
00013 #define SERVORINGBUFFER_H
00014 
00015 // Define variables for structure
00016 #define NUMBER_OF_SPINDLES         2
00017 #define LEFT_SERVO_INDEX           0
00018 #define RIGHT_SERVO_INDEX          1
00019 
00020 // Define variables for ring buffer
00021 #define BUFFER_SIZE     512            //This sets the ring buffer size to 512 elements
00022 
00023 // Define a structure hierarchy for storing data
00024 struct servoData {
00025     unsigned short pos;
00026     unsigned int force;
00027 };
00028 struct spindleData {
00029     servoData myServoData[NUMBER_OF_SPINDLES];
00030     unsigned char direction;
00031     unsigned char cycle;
00032     int time;
00033 };
00034 
00035 /** Servo ring buffer class, to store data between acquisiton and export
00036  *
00037  * Example:
00038  * @code
00039  * #include "mbed.h"
00040  * #include "ServoRingBuffer.h"
00041  *
00042  * int main() {
00043  *
00044  *   ServoRingBuffer Buffer;     //Set up ring buffer
00045  *   AnalogIn Ain(p20);          //Set up potentiometer on pin 20
00046  *
00047  *   while (1) {
00048  *
00049  *       // This section of code should run whenever data needs to be acquired
00050  *       Buffer.write(Ain.read_u16(),Ain.read_u16());
00051  *
00052  *       // This section of code should run whenever there is free time to print to the screen
00053  *       Buffer.dumpBufferToSerial();
00054  *       wait (0.1);
00055  *   }
00056  * }
00057  * @endcode
00058  */
00059 class ServoRingBuffer
00060 {
00061 
00062 public:
00063     ServoRingBuffer();
00064 
00065     void purge();
00066     void write(spindleData data);
00067     void write(unsigned short pos0, unsigned short force0, unsigned short pos1=0, unsigned short force1=0);
00068     spindleData read(void);
00069     void dumpBufferToSerial(void);
00070     void dumpBufferToSD(FILE * txtFile);
00071     unsigned int writesRemaining(void);
00072     unsigned int readsRemaining(void);
00073     float percentFull(void);
00074 
00075 private:
00076 
00077     volatile unsigned int lastWritten;  //This is the variable to track the last written index
00078     volatile unsigned int lastRead;     //This is the variable to track the last read index
00079     unsigned int idx;                       //Buffer index counter
00080     Timer t;
00081     spindleData ringBuffer[BUFFER_SIZE]; //This creates a structure of structures with indeces from 0 to BUFFER_SIZE - 1 (i.e.-There are BUFFER_SIZE amount of indeces )
00082 
00083 };
00084 
00085 #endif // SERVORINGBUFFER_H