
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
Diff: main.cpp
- Revision:
- 34:44cc9b76a507
- Parent:
- 33:9806eb964362
- Child:
- 35:df40c4566826
--- a/main.cpp Fri Jan 09 20:07:09 2015 +0000 +++ b/main.cpp Sun Jan 25 02:45:58 2015 +0000 @@ -2,17 +2,6 @@ #include "mbed.h" #include <stdio.h> -// Ethernet -#include "EthernetInterface.h" -#include "NetworkAPI/buffer.hpp" -#include "NetworkAPI/select.hpp" -#include "NetworkAPI/ip/address.hpp" -#include "NetworkAPI/tcp/socket.hpp" - -// Angle encoder and motor control -#include "AngleEncoder.h" -#include "MotorControl.h" - // Analog sampling #include "PeripheralNames.h" #include "PeripheralPins.h" @@ -21,70 +10,18 @@ #include "fsl_dspi_hal.h" #include "AngleEncoder.h" -/***************************************** - * - * Configuration - * - * Take the time to set these constants - * - *****************************************/ -#define MALLET 5 // set mallet to a value between 1-7 -#define STATIC 1 // set STATIC to 1 for static ip, set STATIC to 0 for dynamic -#define PORT 22 // set to a random port number. All the mallets can use the same port number. -#define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit) -#define INVERT_ANGLE 0 // inverts whether the angle encoder counts up or down - +#include "dma.h" // Analog sampling #define MAX_FADC 6000000 -#define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz -#define TOTAL_SAMPLES 10000 // originally 30000 for 0.3 ms of sampling. +#define SAMPLING_RATE 1000 // In microseconds, so 10 us will be a sampling rate of 100 kHz +#define TOTAL_SAMPLES 10 // originally 30000 for 0.3 ms of sampling. #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms #define FIRST_SAMPLE_INDEX 0 #define BEGIN_SAMPLING 0xFFFFFFFF #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1) -#define CHANNEL_STORAGE_OFFSET 16 // For storing the 16 bits and the 16 bits in a single 32 bit array -#define PERIOD 3000 // make sure PERIOD >= ON_OFF_TIME -#define ON_OFF_TIME 300 // time it takes for relay to turn on - - -// Ethernet -#define GATEWAY "169.254.225.1" -#define MASK "255.255.0.0" - -// used for assign different mallets their ip addresses -#if MALLET == 1 -#define IP "169.254.225.206" -#define NAME "Mallet1\n\r" - -#elif MALLET == 2 -#define IP "169.254.225.207" -#define NAME "Mallet2\n\r" - -#elif MALLET == 3 -#define IP "169.254.225.208" -#define NAME "Mallet3\n\r" - -#elif MALLET == 4 -#define IP "169.254.225.209" -#define NAME "Mallet4\n\r" - -#elif MALLET == 5 -#define IP "169.254.225.210" -#define NAME "Mallet5\n\r" - -#elif MALLET == 6 -#define IP "169.254.225.211" -#define NAME "Mallet6\n\r" - -#elif MALLET == 7 -#define IP "169.254.225.212" -#define NAME "Mallet7\n\r" - -#endif - // for debug purposes Serial pc(USBTX, USBRX); @@ -92,60 +29,26 @@ DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); -// motor control and angle encoder -PwmOut motorA(PTC2); -PwmOut motorB(PTA2); -//MotorControl motor(PTC2, PTA2, PERIOD, ON_OFF_TIME); // forward, backward, period, safetyPeriod AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder // Analog sampling -//Ticker Sampler; -//Timer timer; -//Timer timeStamp; AnalogIn A0_pin(A0); AnalogIn A2_pin(A2); - -//DigitalIn SW3_switch(PTA4); -//DigitalIn SW2_switch(PTC6); -DigitalOut TotalInd(PTC4); -DigitalOut SampleInd(PTC5); // originally PTD0 but needed for CS for spi +Ticker Sampler; uint32_t current_sample_index = WAITING_TO_BEGIN; uint16_t sample_array1[TOTAL_SAMPLES]; uint16_t sample_array2[TOTAL_SAMPLES]; uint16_t angle_array[TOTAL_SAMPLES]; +float currA0 = 0; +float currA2 = 0; // Declaration of functions void analog_initialization(PinName pin); -void output_data(uint32_t iteration_number); void timed_sampling(); - -int motorVal = 0; -void releaseMallet() -{ - motorA = 0; - motorB = 1; - wait_ms(10); - motorB = 0; - wait_ms(75); - - motorA = 0.7; - wait_ms(8); - motorA = 0; - } - -void resetMallet() -{ - motorA = 0; - motorB = 1; - wait_ms(153); - motorB = 0; - } - - // Important globabl variables necessary for the sampling every interval int rotary_count = 0; uint32_t last_AMT20_AB_read = 0; @@ -153,469 +56,56 @@ using namespace std; int main() { - //for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;} - TotalInd = 0; - SampleInd = 0; led_blue = 1; led_green = 0; led_red = 1; - motorA = 0; - motorB = 0; - motorA.period_ms(8); - motorB.period_ms(8); + pc.baud(230400); - pc.printf("Starting %s\r\n",NAME); - - - - for(int i = 0; i < 86; i++) - { - if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2); - } - - NVIC_SetPriority(PIT3_IRQn,0); - - NVIC_SetPriority(ADC1_IRQn,0); - NVIC_SetPriority(ADC0_IRQn,0); - NVIC_SetPriority(ENET_1588_Timer_IRQn,0); - NVIC_SetPriority(ENET_Transmit_IRQn,0); - NVIC_SetPriority(ENET_Receive_IRQn,0); - NVIC_SetPriority(ENET_Error_IRQn,0); - - - // The ethernet setup must be within the first few lines of code, otherwise the program hangs - EthernetInterface interface; - #if STATIC == 1 - interface.init(IP, MASK, GATEWAY); - #else - interface.init(); - #endif - interface.connect(); - pc.printf("IP Address is: %s\n\r", interface.getIPAddress()); - pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask()); - pc.printf("MAC address is: %s\n\r", interface.getMACAddress()); - pc.printf("Gateway is: %s\n\r", interface.getGateway()); - pc.printf("Port is: %i\n\r", PORT); - - - // ethernet setup failed for some reason. Flash yellow light then uC resets itself - if(interface.getIPAddress() == 0) - { - for(int i = 0; i < 5; i++) - { - led_red = 0; - led_green = 0; - wait_ms(500); - led_red = 1; - led_green = 1; - wait_ms(1000); - } - NVIC_SystemReset(); - } - + pc.printf("Starting\r\n"); analog_initialization(A0); analog_initialization(A2); + dma_init(sample_array1,TOTAL_SAMPLES); // Start the sampling loop current_sample_index = WAITING_TO_BEGIN; - //Sampler.attach_us(&timed_sampling, SAMPLING_RATE); - - //NVIC_SetPriority(TIMER3_IRQn,0); - //pc.printf("Ticker IRQ: %i\r\n", Sampler.irq()); - + Sampler.attach_us(&timed_sampling, SAMPLING_RATE); - network::Select select; - network::tcp::Socket server; - network::tcp::Socket client[MAX_CLIENTS]; - network::tcp::Socket *socket = NULL; - - int result = 0; - int index = 0; - - network::Buffer buffer(50); - std::string message(NAME); - - // Configure the server socket (assume every thing works) - server.open(); - server.bind(PORT); - server.listen(MAX_CLIENTS); - - // Add sockets to the select api - select.set(&server, network::Select::Read); - for (index = 0; index < MAX_CLIENTS; index++) { - select.set(&client[index], network::Select::Read); + while(1) { + if(pc.readable() > 0) { + char temp = pc.getc(); + + switch(temp) { + case 's': + for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: ",i,sample_array1[i]); + break; + case 'f': + for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0; + break; + + } + } + pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2); + wait(1); } - led_red = 1; - led_green = 1; - led_blue = 0; - wait_ms(500); - led_blue = 1; - wait_ms(200); - led_blue = 0; - wait_ms(500); - led_blue = 1; - - 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); - - //for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i)); - - do { - // Wait for activity - result = select.wait(); - if (result < -1) { - pc.printf("Failed to select\n\r"); - break; - } - - // Get the first socket - socket = (network::tcp::Socket *)select.getReadable(); - - for (; socket != NULL; socket = (network::tcp::Socket *)select.getReadable()) { - // Check if there was a connection request. - if (socket->getHandle() == server.getHandle()) { - // Find an unused client - for (index = 0; index < MAX_CLIENTS; index++) { - if (client[index].getStatus() == network::Socket::Closed) { - break; - } - } - - // Maximum connections reached - if (index == MAX_CLIENTS) { - pc.printf("Maximum connections reached\n\r"); - wait(1); - continue; - } - - // Accept the client - socket->accept(client[index]); - pc.printf("Client connected %s:%d\n\r", - client[index].getRemoteEndpoint().getAddress().toString().c_str(), - client[index].getRemoteEndpoint().getPort()); - - // Send a nice message to the client (tell MATLAB your name - client[index].write((void *)message.data(), message.size()); - - continue; - } - - // It was not the server socket, so it must be a client talking to us. - switch (socket->read(buffer)) { - case 0: - // Remote end disconnected - pc.printf("Client disconnected %s:%d\n\r", - socket->getRemoteEndpoint().getAddress().toString().c_str(), - socket->getRemoteEndpoint().getPort()); - - // Close socket - socket->close(); - break; - - case -1: - pc.printf("Error while reading data from socket\n\r"); - socket->close(); - break; - - - -//************* this is where data is printed to the screen - default: - pc.printf("Message from %s:%d\n\r", - socket->getRemoteEndpoint().getAddress().toString().c_str(), - socket->getRemoteEndpoint().getPort()); - - pc.printf("%s\n\r", (char *)buffer.data()); - - // read first character for command - char command[2]; - buffer.read(command,2,0); - if(command[1] == ':') { - switch(command[0]) - { - case 'b': - led_blue = !led_blue; - client[index].write((void *)"Blue LED\n",9); - break; - - case 'r': - led_red = !led_red; - client[index].write((void *)"Red LED\n",8); - break; - - case 'p': - led_green = !led_green; - client[index].write((void *)"Data\n",5); - for(int i = 0; i < 99; i++) sample_array1[i] = i; - client[index].write((void *)&sample_array1,2*99); - break; - case 't': - { - for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i)); - } - break; - - case '3': // run motor and sample - { - // set angle to 0 - led_blue = 0; - while(!angle_encoder.set_zero()) {} - led_blue = 1; - - // release mallet - wait(1); - - - // reset mallet - motorA = 0; - int angleVar = 1; - int counter = 0; - while(angleVar) - { - counter++; - angleVar = angle_encoder.absolute_angle(); - //angleVar -= motorVal; - if(angleVar == 0x00ff0000) {} // do nothing - else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed - else if(angleVar < motorVal) { - angleVar = 0; // exit loop - motorB.pulsewidth_us(0);} // min speed - else motorB.pulsewidth_us(angleVar*800/34); - } - motorB = 0; - - char buf[29]; - sprintf(buf,"MotorVal: %i\n",motorVal); - client[index].write((void *) buf,29); - - - - /* - int tempDutyCycle = 2000; - for(int i = counter; i > 0; i--) - { - motorA.pulsewidth_us(tempDutyCycle); - } - */ - // make sure motors are off - motorA = 0; - motorB = 0; - //wait_ms(138); - motorB = 0; - } - break; - case '1': - { - // set angle to 0 - led_blue = 0; - while(!angle_encoder.set_zero()) {} - led_blue = 1; - - // release mallet - wait(1); - - - // reset mallet - motorA = 0; - int angleVar = 1; - int counter = 0; - while(angleVar) - { - counter++; - angleVar = angle_encoder.absolute_angle(); - angleVar -= motorVal; - if(angleVar == 0x00ff0000) {} // do nothing - else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed - else if(angleVar < 43) { - angleVar = 0; // exit loop - motorB.pulsewidth_us(0);} // min speed - else motorB.pulsewidth_us(angleVar*800/34); - } - motorB = 0; - - char buf[29]; - sprintf(buf,"MotorVal: %i\n",motorVal); - client[index].write((void *) buf,29); - - - - /* - int tempDutyCycle = 2000; - for(int i = counter; i > 0; i--) - { - motorA.pulsewidth_us(tempDutyCycle); - } - */ - // make sure motors are off - motorA = 0; - motorB = 0; - } - break; - case '2': - { - // release mallet - motorA = 0; - motorB = 1; - wait_ms(10); - motorB = 0; - wait_ms(75); - - motorA = 0.7; - wait_ms(8); - motorA = 0; - - // wait for angle to set to zero - led_blue = 0; - while(!angle_encoder.set_zero()) {} - led_blue = 1; - - // wait for mallet to drop to certin point before beginning to sample - led_red = 0; - int angleVal; - do { - angleVal = angle_encoder.absolute_angle(); - } - while(!(angleVal > 150 && angleVal < 400)); - led_red = 1; - - //char buf[29]; - //sprintf(buf,"AngleVal: %i\n",angleVal); - //client[index].write((void *) buf,29); - - - // begin sampling - BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) - BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) - client[index].write((void *)"Data\n",5); - - TotalInd = 1; - - uint32_t AMT20_AB; - rotary_count = 0; - __disable_irq(); - SampleInd = 0; - for(int i = 0; i < TOTAL_SAMPLES; i++) - { - SampleInd = !SampleInd; - sample_array1[i] = adc_hal_get_conversion_value(0, 0); - sample_array2[i] = adc_hal_get_conversion_value(1, 0); - BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) - BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) - - // The following updates the rotary counter for the AMT20 sensor - // Put A on PTC0 - // Put B on PTC1 - AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; - - if (AMT20_AB != last_AMT20_AB_read) - { - // change "INVERT_ANGLE" to change whether relative angle counts up or down. - if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) - #if INVERT_ANGLE == 1 - {rotary_count--;} - else - {rotary_count++;} - #else - {rotary_count++;} - else - {rotary_count--;} - #endif - - last_AMT20_AB_read = AMT20_AB; - } - angle_array[i] = rotary_count; - wait_us(8); - } - __enable_irq(); - - // reset mallet - motorA = 0; - motorB = 1; - wait_ms(138); - motorB = 0; - - - - NVIC_SetPriority(ENET_1588_Timer_IRQn,0); - NVIC_SetPriority(ENET_Transmit_IRQn,0); - NVIC_SetPriority(ENET_Receive_IRQn,0); - NVIC_SetPriority(ENET_Error_IRQn,0); - TotalInd = 1; - client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES); - client[index].write((void *)&sample_array2,2*TOTAL_SAMPLES); - client[index].write((void *)&angle_array,2*TOTAL_SAMPLES); - TotalInd = 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); - - } - break; - case '+': - motorVal++; - if(motorVal > 8000) motorVal = 43; - char buf[29]; - sprintf(buf,"MotorVal: %i\n",motorVal); - client[index].write((void *) buf,29); - - break; - case '-': - motorVal--; - if(motorVal < 0) motorVal = 0; - char buf[29]; - sprintf(buf,"MotorVal: %i\n",motorVal); - client[index].write((void *) buf,29); - - break; - case 'a': - if(angle_encoder.set_zero(&rotary_count)) { - client[index].write((void *) "Zero set\n",9); - } - else { - client[index].write((void *) "Zero NOT set\n",13); - } - break; - case 's': - { - char buf[16]; - sprintf(buf,"NOP: %x\n",angle_encoder.nop()); - client[index].write((void *) buf,16); - break; - } - case 'd': - { - char buf[29]; - sprintf(buf,"Angle: %i %i\n",angle_encoder.absolute_angle(), rotary_count); - client[index].write((void *) buf,29); - break; - } - } - } - break; - } - } - - } while (server.getStatus() == network::Socket::Listening); + } void timed_sampling() { - SampleInd = 1; - //__disable_irq(); // Disable Interrupts - //timeStamp.start(); - /* + __disable_irq(); // Disable Interrupts + + // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another uint32_t A0_value = adc_hal_get_conversion_value(0, 0); uint32_t A2_value = adc_hal_get_conversion_value(1, 0); BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) + currA0 = (float) A0_value*3.3/65535; + currA2 = (float) A2_value*3.3/65535; + // The following updates the rotary counter for the AMT20 sensor // Put A on PTC0 // Put B on PTC1 @@ -655,13 +145,7 @@ else { current_sample_index++; } } - //int tempVar = timeStamp.read_us(); - //timeStamp.stop(); - //timeStamp.reset(); - //pc.printf("TimeStamp: %i\r\n", tempVar); - //__enable_irq(); // Enable Interrupts - */ - SampleInd = 0; + __enable_irq(); // Enable Interrupts } void analog_initialization(PinName pin) @@ -695,22 +179,4 @@ adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail pinmap_pinout(pin, PinMap_ADC); -} - -void output_data(uint32_t iteration_number) -{ - pc.printf("Iteration: %i\n\r", iteration_number); - pc.printf("Sampling rate: %i\n\r", SAMPLING_RATE); - pc.printf("Data length: %i\n\r", TOTAL_SAMPLES); - - //for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) { - // pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]); - // } - -} - -// read some registers for some info. -//uint32_t* rcr = (uint32_t*) 0x400C0084; -//uint32_t* ecr = (uint32_t*) 0x400C0024; -//pc.printf("RCR register: %x\r\n", *rcr); -//pc.printf("ECR register: %x\r\n", *ecr); \ No newline at end of file +} \ No newline at end of file