DCS_TEAM / Mbed 2 deprecated Chemical_Sensor_DMA

Dependencies:   mbed

Dependents:   DCS_FINAL_CODE

Fork of Chemical_Sensor_DMA by Jared Baxter

Files at this revision

API Documentation at this revision

Comitter:
DeWayneDennis
Date:
Fri Nov 06 20:50:30 2015 +0000
Parent:
5:1b2dc43e8947
Child:
7:af255a90505e
Commit message:
Jared's DAC Code

Changed in this revision

AngleEncoder.cpp Show diff for this revision Revisions of this file
AngleEncoder.h Show diff for this revision Revisions of this file
MotorControl.cpp Show diff for this revision Revisions of this file
MotorControl.h Show diff for this revision Revisions of this file
Sample/quad.cpp Show diff for this revision Revisions of this file
Sample/quad.h Show diff for this revision Revisions of this file
SignalProcessing.cpp Show annotated file Show diff for this revision Revisions of this file
SignalProcessing.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
--- a/AngleEncoder.cpp	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +0,0 @@
-#include "AngleEncoder.h"
-#include "mbed.h"
- 
-AngleEncoder::AngleEncoder(PinName mosi, PinName miso, PinName sclk, PinName cs, int bits, int mode, int hz) :  
-        _spi(mosi, miso, sclk), 
-        _cs(cs) {
-    // constructor
-    _cs = 1;
-    _spi.format(bits,mode);
-    _spi.frequency(hz);
-    
-    wait_ms(1000); // Angle encoder requires 0.1 seconds to boot up, so I gave it an entire second.
-    
-}
-
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
- * 
- * Sends a NOP command.  This is a read command.
- *
- * Returns the 8 bit data read from the encoder.  
- *      0xA5 indicates communication is good, but no data to receive from encoder
- *      
- *      0x00 indicates that angle encoder probably wasn't read.  May need to 
- *      increase SPI_DELAY
- *  
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
-int AngleEncoder::nop() { // this function takes about 90us to complete (tested using Timer library)
-    _cs = 0;
-    wait_us(SPI_DELAY);
-    int received = _spi.write(0x00);
-    wait_us(SPI_DELAY);
-    _cs = 1;
-    wait_us(SPI_DELAY);
-    return received;
-}
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
- * 
- * Sets the current position as the zero point from which angles are calculated
- *  
- * Returns true if successful, false otherwise
- * 
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-bool AngleEncoder::set_zero() {
-    char received;
-    
-    // send "set_zero_point" command
-    _cs = 0;
-    wait_us(SPI_DELAY);
-    received = _spi.write(0x70); // send the command
-    wait_us(SPI_DELAY);
-    _cs = 1;
-    wait_us(SPI_DELAY);
-    
-    // send NOP until reset is confirmed 
-    while(received == 0xa5) received = nop();
-    
-    // 0x80 indicates that reset is confirmed
-    if(received == 0x80) return true;
-    else return false;
-}
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
- * 
- *  Sets the current position as the zero point from which angles are calculated
- *  @param rotary_count  The address to the rotary_count variable, allowing this 
- *                       function to reset both the absolute and relative angles.
- *  
- *  Returns true if successful, false otherwise
- *
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-bool AngleEncoder::set_zero(int* rotary_count) {
-    
-    *rotary_count = 0;  // reset relative counter
-    return set_zero();  // reset absolute counter
-}
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
- * 
- * Reads the absolute angle from the angle encoder via SPI
- * Returns the 12 bit angle (-2048, 2047)
- *  
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-int AngleEncoder::absolute_angle() { // this function takes about 500us to complete (tested using Timer library)
-    char received;
-    uint16_t absolute_pos;
-    
-    // send read command
-    _cs = 0;
-    wait_us(SPI_DELAY);
-    received = _spi.write(0x10);
-    wait_us(SPI_DELAY);
-    _cs = 1;
-    
-    // wait for angle encoder to echo the command
-    wait_us(SPI_DELAY);
-    
-    // read until encoder sends 0x10
-    for(uint32_t i = 0; i < 100; i++)
-    {
-        received = nop();
-        if(received == 0x10) break;
-    }
-    
-    //while(received != 0x10) received = nop(); // read until 0x10 is received
-        
-    // the command should have been echoed back.  If so, then read the angle    
-    if(received == 0x10)
-    {
-        // receive the most significatn 4 bits of the 12 bit angle
-        absolute_pos = nop();  // read first 4 bits of absolute angle
-        absolute_pos <<= 8; // shift the 4 bits into the right spot
-        absolute_pos |= nop(); // receive the last 8 bits of absolute angle
-        
-        //make data symmetric around 0.
-        //if(absolute_pos > 2048) absolute_pos -= 4096;
-        //absolute_pos = ~absolute_pos + 1; // inverts the data
-        return absolute_pos;
-        }
-    else return 0x00ff0000; // this is just a random number outside of the range of the encoder
-}
--- a/AngleEncoder.h	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-#ifndef ANGLE_ENCODER_H
-#define ANGLE_ENCODER_H
- 
-#include "mbed.h"
- 
-#define SPI_DELAY 4 // must be 4 or greater, otherwise the angle encoder can't keep up with the communication
- 
-class AngleEncoder {
-public:
-    AngleEncoder(PinName mosi, PinName miso, PinName sclk, PinName cs, int bits, int mode, int hz);
-    int nop();
-    int absolute_angle();
-    bool set_zero();
-    bool set_zero(int*);
-    
-private:  
-    SPI _spi;
-    DigitalOut _cs;
-};
- 
-#endif
\ No newline at end of file
--- a/MotorControl.cpp	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-#include "MotorControl.h"
-#include "mbed.h"
-
-// constructor 
-MotorControl::MotorControl(PinName cw, PinName ccw, int period, int safetyPeriod) :  
-        _cw(cw),
-        _ccw(ccw) {
-    
-    // turn motor off
-    _cw = 0;
-    _ccw = 0;
-    
-    _cw.period_us(period);
-    _ccw.period_us(period);
-    
-    _period = period;
-    _safetyPeriod = safetyPeriod;
-}
-
-void MotorControl::off() {
-    _cw = 0;
-    _ccw = 0;    
-    pause_us(_safetyPeriod);
-}
-
-void MotorControl::clockwise(float dutyCycle) {
-    _cw = dutyCycle;
-}
-
-void MotorControl::releaseMallet() {
-    // make sure motor is off
-    off();
-    
-    // pulse clockwise to release mallet
-    _cw = 1;
-    pause_ms(10);
-    _cw = 0;
-    pause_ms(75);
-    
-    // pulse counter-clockwise to stop snail cam
-    _ccw = 0.7;
-    pause_ms(8);
-    _ccw = 0;
-    
-    // make sure motor is off
-    off();
-}
-
-void MotorControl::reset() {
-    
-}
-
-void MotorControl::hardReset(int duration) {
-    // make sure motor is off
-    off();
-    
-    // long pulse clockwise to reset mallet
-    _cw = 1;
-    pause_ms(200);
-    _cw = 0;
-    
-    // short pulse counter-clockwise to stop snail cam
-    _ccw = 1;
-    pause_ms(10);
-    _ccw = 0;
-    
-    
-    wait_ms(duration);
-    // short pulse counter-clockwise to set snail cam in groove on mallet
-    _ccw = 1;
-    pause_ms(3);
-    _ccw = 0;
-    
-    // make sure motor is off
-    off();
-}
\ No newline at end of file
--- a/MotorControl.h	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-#ifndef MOTOR_CONTROL_H
-#define MOTOR_CONTROL_H
- 
-#include "mbed.h"
-#include "pause.cpp"
- 
-class MotorControl {
-public:
-    MotorControl(PinName cw, PinName ccw, int period, int safetyPeriod);
-    
-    void off(); // make sure the motor is off
-    void clockwise(float);
-    void releaseMallet(); // release mallet using hard coded timing
-    void reset(); // reset mallet using feedback (NEEDS TO BE IMPLEMENTED)
-    
-    // what we threw together just for testing
-    void hardReset(int);         // reset mallet using hard coded timing
-    
-private:  
-    PwmOut _cw;    // clock wise rotation when _cw = 1
-    PwmOut _ccw;   // counter-clock wise rotation when _ccw = 1
-    int _period;       // the period of the motor switching cycle
-    int _safetyPeriod; // dead time between transition from cw to ccw (and visa versa)
-};
- 
-#endif
\ No newline at end of file
--- a/Sample/quad.cpp	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-#include "quad.h"
-
-void quad_init() {
-    
-    // Enable clock for FTM2
-    SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK; // there are two of them for some reason
-    SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK; // 
-    
-    // Enable clock for PortB
-    SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
-    
-    //enable the counter
-    FTM2_MODE |= FTM_MODE_WPDIS_MASK; // unlock write protection on certain FTM bits and registers
-    FTM2_MODE |= FTM_MODE_FTMEN_MASK; 
-    
-    //enable the counter to run in the BDM mode
-    FTM2_CONF |= FTM_CONF_BDMMODE(3);
-    
-    //load the Modulo register and counter initial value
-    FTM2_MOD = 4095;
-    FTM2_CNTIN = 0;
-    
-    //configuring FTM for quadrature mode
-    FTM2_QDCTRL = 0;
-    FTM2_QDCTRL |= FTM_QDCTRL_QUADEN_MASK; // enable Quadrature decoding
-    FTM2_QDCTRL &= ~FTM_QDCTRL_QUADMODE_MASK; // select phase A and phase B decoding mode (this is what the angle encoder uses)
-    FTM2_QDCTRL |= FTM_QDCTRL_PHAPOL_MASK; // change direction of counting by changing the phase of input A (for some reason QUADIR wouldn't work)
-    
-    
-    // start the timer clock, source is the external clock
-    FTM2_SC |= FTM_SC_CLKS(3);
-    
-    //configuring the input pins:
-    PORTB_PCR18 = PORT_PCR_MUX(6); // FTM2_QD_PHA  (PTB18) ALT6
-    PORTB_PCR19 = PORT_PCR_MUX(6); // FTM2_QD_PHB  (PTB19) ALT6
-    
-    // Enable write protection
-    FTM2_MODE |= FTM_MODE_WPDIS_MASK; // Lock write protection on certain FTM bits and registers
-    
-}   
- 
-int32_t quad_read() {
-    int angle = FTM2_CNT;
-    if(angle > 2048) angle -= 4096;
-    return angle;  // this is the register with the counter in it.
-}
-
-void quad_update(int value) {
-    if(value < 0) value = value + 4096; // convert back to raw form (both absolute angle and relative angle are actually integers between 0-4095)
-    FTM2_CNTIN = value; // change reset value to desired value
-    FTM2_CNT = 0; // writing anything to FTM_CNT loads FTM2_CNTIN into FTM_CNT;
-    FTM2_CNTIN = 0; // change reset value back to 0
-}
-
-void quad_invert(){
-    FTM2_QDCTRL &= ~FTM_QDCTRL_PHAPOL_MASK; // change direction of counting by changing the phase of input A
-}
\ No newline at end of file
--- a/Sample/quad.h	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-/*
-Add this to the Angle Encoder class later
-
-http://cache.freescale.com/files/32bit/doc/app_note/AN4381.pdf
-*/
-
-#ifndef QUAD_H_
-#define QUAD_H_
-
-#include "mbed.h"
-
-void quad_update(int);    
-void quad_init();
-int quad_read();
-void quad_invert();
-
-#endif /* DMA_H_ */
\ No newline at end of file
--- a/SignalProcessing.cpp	Fri Nov 06 19:28:49 2015 +0000
+++ b/SignalProcessing.cpp	Fri Nov 06 20:50:30 2015 +0000
@@ -11,6 +11,7 @@
 float i_mod_pre[pre_compute_length];
 float q_mod_pre[pre_compute_length];
 //uint16_t out_val_pre[pre_compute_length]; 
+float filteredLong, filteredLongRef;
 
 #define twopi 3.14159265359 * 2
 
@@ -477,6 +478,7 @@
     
     
     static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated
+    static uint16_t finalAverageCounter = 0; //used to keep track of how many elements to average across
     static float FIR1K_Sample1_i_DecimatedSum=0;
     static float FIR1K_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value
     static float FIR1K_Sample2_i_DecimatedSum=0;
@@ -535,7 +537,10 @@
             finalAverageCounter=0;
             float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q);
             float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q);
-            printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1);
+            //store filtered values for later reading
+            filteredLong = mag1;
+            filteredLongRef = mag2;
+            
             Final_Average1_i=0;
             Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value
             Final_Average2_i=0;
@@ -714,5 +719,9 @@
     filter10K(FIR100K_Sample1_i_Output, FIR100K_Sample1_q_Output, FIR100K_Sample2_i_Output, FIR100K_Sample2_q_Output);
 }
 
-
-    
+float getFiltered(){
+    return filteredLong;    
+}
+float getFilteredRef(){
+    return filteredLongRef;   
+}    
--- a/SignalProcessing.h	Fri Nov 06 19:28:49 2015 +0000
+++ b/SignalProcessing.h	Fri Nov 06 20:50:30 2015 +0000
@@ -3,4 +3,6 @@
 
 void pre_compute_tables();
 void filter100K(int sample1, int sample2);
+float getFiltered();
+float getFilteredRef();
 #endif
\ No newline at end of file
--- a/main.cpp	Fri Nov 06 19:28:49 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,117 +0,0 @@
-#include "mbed.h"
-#include "pause.cpp"
-#include "Sample/adc.h"
-#include "Sample/pdb.h"
-#include "SignalProcessing.h"
-// for debug purposes
-Serial pc(USBTX, USBRX);
-DigitalOut led_red(LED_RED);
-DigitalOut led_green(LED_GREEN);
-DigitalOut led_blue(LED_BLUE);
-
-Timer t1;
-using namespace std;
- 
- 
- 
- 
-#define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled
-
-#define DAC0_DAT0 (uint16_t *)0x400CC000 // DAC word buffer base address
-
-uint16_t *p1;
-void setUpDac()
-{
-    SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK;  // turn on clock to the DAC
-    SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK;  // turn on clock to the DAC
-    DAC0_C0 |= DAC_C0_DACEN_MASK ;   // enable the DAC; must do before any of the following
-    DAC0_C2 =9;//cycle through the first 10 values in the buffer
-    DAC0_C1 = 1;//enable the dac buffer
-    p1 = DAC0_DAT0;
-    for (int i = 0; i < 16; i++)//fill the buffer 
-    {
-        *p1++ = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0); // 3351.0
-        //printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,(int) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 800.0 + 3103.0));
-    } 
-
-}
- 
-int main()
-{
-    
-    led_blue = 1;
-    led_green = 1;
-    led_red = 1;
-    pre_compute_tables();
-    
-    
-//    t1.reset();
-//    for (int t  = 0; t<500000; t++)
-//    {
-//        int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500;
-//        int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500;
-//        t1.start();
-//        filter100K(input1, input2);
-//        t1.stop();
-//    }
-//    printf("FINAL TIME: %f\n\r",t1.read());
-   
-    pc.printf("Starting...\r\n");    
-    for(int i = 0; i < 86; i++) 
-    {
-        if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
-    }
-    
-    // Give hardware associated with 
-    // sampling the highest priority
-    NVIC_SetPriority(ADC1_IRQn,0);
-    NVIC_SetPriority(ADC0_IRQn,0);
-    NVIC_SetPriority(PDB0_IRQn,0);
-    NVIC_SetPriority(DMA0_IRQn,0);
-    NVIC_SetPriority(DMA1_IRQn,0);
-    NVIC_SetPriority(DMA2_IRQn,0);
-    
-    NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
-    NVIC_SetPriority(ENET_Transmit_IRQn,1);
-    NVIC_SetPriority(ENET_Receive_IRQn,1);
-    NVIC_SetPriority(ENET_Error_IRQn,1);
-    
-    adc_init(); // initialize ADCs (always initialize adc before dma)
-    setUpDac();
-    dma_init(); // initializes DMAs
-    pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 
-    
-    // flash green led indicating startup complete
-    led_red = 1;
-    led_blue = 1;
-    led_green = 0;
-    pause_ms(500);
-    led_green = 1;
-    pause_ms(200);
-    led_green = 0;
-    pause_ms(500); 
-    led_green = 1;
-    pdb_start();
-    int startAddress = (int)&sample_array0[0];
-    int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;
-    int currentIndex;
-    while (1)
-    {
-        
-    
-        destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;//-1;
-        //if (destinationIndex<0)
-        //    destinationIndex = 1999;
-        while (currentIndex!=destinationIndex)
-        {
-            filter100K(sample_array0[currentIndex], sample_array1[currentIndex]);
-            currentIndex++;
-            if (currentIndex>=2000)
-                currentIndex = 0;
-            
-        }
-        
-    }
-    
-    
-}