Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Fri Dec 19 21:39:42 2014 +0000
Revision:
31:09e7a8b3c7e7
Parent:
30:6a4ef939a93e
Child:
32:bf1e079a2ee3
What we used for the final presentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timmey9 20:f533b3c9296f 1 // Server code
donatien 0:bb128f0e952f 2 #include "mbed.h"
timmey9 24:a5891669afc5 3 #include <stdio.h>
timmey9 22:523e316cbe70 4
timmey9 22:523e316cbe70 5 // Ethernet
donatien 0:bb128f0e952f 6 #include "EthernetInterface.h"
timmey9 17:2f978f823020 7 #include "NetworkAPI/buffer.hpp"
timmey9 17:2f978f823020 8 #include "NetworkAPI/select.hpp"
timmey9 17:2f978f823020 9 #include "NetworkAPI/ip/address.hpp"
timmey9 17:2f978f823020 10 #include "NetworkAPI/tcp/socket.hpp"
timmey9 18:b17ddeeb1c09 11
timmey9 22:523e316cbe70 12 // Angle encoder and motor control
timmey9 22:523e316cbe70 13 #include "AngleEncoder.h"
timmey9 22:523e316cbe70 14 #include "MotorControl.h"
timmey9 22:523e316cbe70 15
timmey9 22:523e316cbe70 16 // Analog sampling
timmey9 22:523e316cbe70 17 #include "PeripheralNames.h"
timmey9 22:523e316cbe70 18 #include "PeripheralPins.h"
timmey9 22:523e316cbe70 19 #include "fsl_adc_hal.h"
timmey9 22:523e316cbe70 20 #include "fsl_clock_manager.h"
timmey9 22:523e316cbe70 21 #include "fsl_dspi_hal.h"
timmey9 22:523e316cbe70 22 #include "AngleEncoder.h"
timmey9 22:523e316cbe70 23
timmey9 22:523e316cbe70 24 /*****************************************
timmey9 22:523e316cbe70 25 *
timmey9 22:523e316cbe70 26 * Configuration
timmey9 22:523e316cbe70 27 *
timmey9 22:523e316cbe70 28 * Take the time to set these constants
timmey9 22:523e316cbe70 29 *
timmey9 22:523e316cbe70 30 *****************************************/
timmey9 31:09e7a8b3c7e7 31 #define MALLET 5 // set mallet to a value between 1-7
timmey9 21:1fb5023b72af 32 #define STATIC 1 // set STATIC to 1 for static ip, set STATIC to 0 for dynamic
timmey9 21:1fb5023b72af 33 #define PORT 22 // set to a random port number. All the mallets can use the same port number.
timmey9 21:1fb5023b72af 34 #define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit)
timmey9 22:523e316cbe70 35 #define INVERT_ANGLE 0 // inverts whether the angle encoder counts up or down
timmey9 22:523e316cbe70 36
timmey9 28:4a833d59897b 37
timmey9 22:523e316cbe70 38 // Analog sampling
timmey9 22:523e316cbe70 39 #define MAX_FADC 6000000
timmey9 29:e6309316c35d 40 #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
timmey9 27:8c2b30c855d1 41 #define TOTAL_SAMPLES 30000 // originally 30000 for 0.3 ms of sampling.
timmey9 22:523e316cbe70 42
timmey9 22:523e316cbe70 43 #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms
timmey9 22:523e316cbe70 44 #define FIRST_SAMPLE_INDEX 0
timmey9 22:523e316cbe70 45 #define BEGIN_SAMPLING 0xFFFFFFFF
timmey9 22:523e316cbe70 46 #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1)
timmey9 22:523e316cbe70 47
timmey9 22:523e316cbe70 48 #define CHANNEL_STORAGE_OFFSET 16 // For storing the 16 bits and the 16 bits in a single 32 bit array
timmey9 22:523e316cbe70 49 #define PERIOD 3000 // make sure PERIOD >= ON_OFF_TIME
timmey9 22:523e316cbe70 50 #define ON_OFF_TIME 300 // time it takes for relay to turn on
timmey9 22:523e316cbe70 51
timmey9 22:523e316cbe70 52
timmey9 22:523e316cbe70 53 // Ethernet
timmey9 22:523e316cbe70 54 #define GATEWAY "169.254.225.1"
timmey9 22:523e316cbe70 55 #define MASK "255.255.0.0"
timmey9 22:523e316cbe70 56
timmey9 22:523e316cbe70 57 // used for assign different mallets their ip addresses
timmey9 21:1fb5023b72af 58 #if MALLET == 1
timmey9 22:523e316cbe70 59 #define IP "169.254.225.206"
timmey9 21:1fb5023b72af 60 #define NAME "Mallet1\n\r"
timmey9 21:1fb5023b72af 61
timmey9 21:1fb5023b72af 62 #elif MALLET == 2
timmey9 22:523e316cbe70 63 #define IP "169.254.225.207"
timmey9 21:1fb5023b72af 64 #define NAME "Mallet2\n\r"
timmey9 21:1fb5023b72af 65
timmey9 21:1fb5023b72af 66 #elif MALLET == 3
timmey9 22:523e316cbe70 67 #define IP "169.254.225.208"
timmey9 21:1fb5023b72af 68 #define NAME "Mallet3\n\r"
timmey9 21:1fb5023b72af 69
timmey9 21:1fb5023b72af 70 #elif MALLET == 4
timmey9 22:523e316cbe70 71 #define IP "169.254.225.209"
timmey9 21:1fb5023b72af 72 #define NAME "Mallet4\n\r"
timmey9 21:1fb5023b72af 73
timmey9 21:1fb5023b72af 74 #elif MALLET == 5
timmey9 22:523e316cbe70 75 #define IP "169.254.225.210"
timmey9 21:1fb5023b72af 76 #define NAME "Mallet5\n\r"
timmey9 21:1fb5023b72af 77
timmey9 21:1fb5023b72af 78 #elif MALLET == 6
timmey9 22:523e316cbe70 79 #define IP "169.254.225.211"
timmey9 21:1fb5023b72af 80 #define NAME "Mallet6\n\r"
timmey9 21:1fb5023b72af 81
timmey9 21:1fb5023b72af 82 #elif MALLET == 7
timmey9 22:523e316cbe70 83 #define IP "169.254.225.212"
timmey9 21:1fb5023b72af 84 #define NAME "Mallet7\n\r"
timmey9 21:1fb5023b72af 85
timmey9 20:f533b3c9296f 86 #endif
timmey9 20:f533b3c9296f 87
timmey9 21:1fb5023b72af 88
timmey9 22:523e316cbe70 89 // for debug purposes
timmey9 18:b17ddeeb1c09 90 Serial pc(USBTX, USBRX);
timmey9 18:b17ddeeb1c09 91 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 92 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 93 DigitalOut led_blue(LED_BLUE);
timmey9 18:b17ddeeb1c09 94
timmey9 22:523e316cbe70 95 // motor control and angle encoder
timmey9 31:09e7a8b3c7e7 96 PwmOut motorA(PTC2);
timmey9 31:09e7a8b3c7e7 97 PwmOut motorB(PTA2);
timmey9 31:09e7a8b3c7e7 98 //MotorControl motor(PTC2, PTA2, PERIOD, ON_OFF_TIME); // forward, backward, period, safetyPeriod
timmey9 22:523e316cbe70 99 AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
timmey9 22:523e316cbe70 100 DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 101 DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 102
timmey9 22:523e316cbe70 103 // Analog sampling
timmey9 28:4a833d59897b 104 //Ticker Sampler;
timmey9 23:9e5141647775 105 //Timer timer;
timmey9 23:9e5141647775 106 //Timer timeStamp;
timmey9 22:523e316cbe70 107 AnalogIn A0_pin(A0);
timmey9 22:523e316cbe70 108 AnalogIn A2_pin(A2);
timmey9 22:523e316cbe70 109
timmey9 22:523e316cbe70 110 //DigitalIn SW3_switch(PTA4);
timmey9 22:523e316cbe70 111 //DigitalIn SW2_switch(PTC6);
timmey9 28:4a833d59897b 112 DigitalOut TotalInd(PTC4);
timmey9 28:4a833d59897b 113 DigitalOut SampleInd(PTC5); // originally PTD0 but needed for CS for spi
timmey9 22:523e316cbe70 114
timmey9 22:523e316cbe70 115 uint32_t current_sample_index = WAITING_TO_BEGIN;
timmey9 25:abbc19af13f9 116 uint16_t sample_array1[TOTAL_SAMPLES];
timmey9 25:abbc19af13f9 117 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 118 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 119
timmey9 22:523e316cbe70 120 // Declaration of functions
timmey9 22:523e316cbe70 121 void analog_initialization(PinName pin);
timmey9 22:523e316cbe70 122 void output_data(uint32_t iteration_number);
timmey9 22:523e316cbe70 123 void timed_sampling();
timmey9 22:523e316cbe70 124
timmey9 22:523e316cbe70 125 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 126 int rotary_count = 0;
timmey9 22:523e316cbe70 127 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 128
timmey9 22:523e316cbe70 129 using namespace std;
timmey9 17:2f978f823020 130
emilmont 7:65188f4a8c25 131 int main() {
timmey9 25:abbc19af13f9 132 //for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;}
timmey9 28:4a833d59897b 133 TotalInd = 0;
timmey9 28:4a833d59897b 134 SampleInd = 0;
timmey9 22:523e316cbe70 135 led_blue = 1;
timmey9 30:6a4ef939a93e 136 led_green = 0;
timmey9 18:b17ddeeb1c09 137 led_red = 1;
timmey9 31:09e7a8b3c7e7 138 motorA = 0;
timmey9 31:09e7a8b3c7e7 139 motorB = 0;
timmey9 31:09e7a8b3c7e7 140 motorA.period_ms(8);
timmey9 31:09e7a8b3c7e7 141 motorB.period_ms(8);
timmey9 18:b17ddeeb1c09 142 pc.baud(230400);
timmey9 24:a5891669afc5 143 pc.printf("Starting %s\r\n",NAME);
timmey9 23:9e5141647775 144
timmey9 31:09e7a8b3c7e7 145
timmey9 31:09e7a8b3c7e7 146 /*
timmey9 31:09e7a8b3c7e7 147 wait(1);
timmey9 31:09e7a8b3c7e7 148
timmey9 31:09e7a8b3c7e7 149
timmey9 31:09e7a8b3c7e7 150 motorB = 1; // 0.6 gets it around quickly enough
timmey9 31:09e7a8b3c7e7 151 wait_us(10000);
timmey9 31:09e7a8b3c7e7 152 motorB = 0;
timmey9 31:09e7a8b3c7e7 153
timmey9 31:09e7a8b3c7e7 154 wait_us(60000);
timmey9 31:09e7a8b3c7e7 155
timmey9 31:09e7a8b3c7e7 156 motorA = 0.75;
timmey9 31:09e7a8b3c7e7 157 wait_us(5000);
timmey9 31:09e7a8b3c7e7 158 motorA = 0;
timmey9 31:09e7a8b3c7e7 159
timmey9 31:09e7a8b3c7e7 160 wait_ms(300);
timmey9 31:09e7a8b3c7e7 161
timmey9 31:09e7a8b3c7e7 162 motorA = 1;
timmey9 31:09e7a8b3c7e7 163 wait_us(50000);
timmey9 31:09e7a8b3c7e7 164 motorA = 0;
timmey9 31:09e7a8b3c7e7 165
timmey9 31:09e7a8b3c7e7 166 wait_us(45000);
timmey9 31:09e7a8b3c7e7 167
timmey9 31:09e7a8b3c7e7 168 motorB = 0.75;
timmey9 31:09e7a8b3c7e7 169 wait_us(8000);
timmey9 31:09e7a8b3c7e7 170 motorB = 0;
timmey9 31:09e7a8b3c7e7 171
timmey9 31:09e7a8b3c7e7 172
timmey9 31:09e7a8b3c7e7 173 //motorB = 0.2;
timmey9 31:09e7a8b3c7e7 174 //wait_us(5000);
timmey9 31:09e7a8b3c7e7 175
timmey9 31:09e7a8b3c7e7 176
timmey9 31:09e7a8b3c7e7 177 motorB = 0;
timmey9 31:09e7a8b3c7e7 178 motorA = 0;
timmey9 31:09e7a8b3c7e7 179 // make sure motors are off
timmey9 31:09e7a8b3c7e7 180 wait_us(PERIOD);
timmey9 31:09e7a8b3c7e7 181
timmey9 31:09e7a8b3c7e7 182
timmey9 31:09e7a8b3c7e7 183
timmey9 31:09e7a8b3c7e7 184 while(1) {}
timmey9 31:09e7a8b3c7e7 185 */
timmey9 31:09e7a8b3c7e7 186
timmey9 28:4a833d59897b 187 for(int i = 0; i < 86; i++)
timmey9 28:4a833d59897b 188 {
timmey9 28:4a833d59897b 189 if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
timmey9 28:4a833d59897b 190 }
timmey9 28:4a833d59897b 191
timmey9 28:4a833d59897b 192 NVIC_SetPriority(PIT3_IRQn,0);
timmey9 28:4a833d59897b 193
timmey9 28:4a833d59897b 194 NVIC_SetPriority(ADC1_IRQn,0);
timmey9 28:4a833d59897b 195 NVIC_SetPriority(ADC0_IRQn,0);
timmey9 28:4a833d59897b 196 NVIC_SetPriority(ENET_1588_Timer_IRQn,0);
timmey9 28:4a833d59897b 197 NVIC_SetPriority(ENET_Transmit_IRQn,0);
timmey9 28:4a833d59897b 198 NVIC_SetPriority(ENET_Receive_IRQn,0);
timmey9 28:4a833d59897b 199 NVIC_SetPriority(ENET_Error_IRQn,0);
timmey9 28:4a833d59897b 200
timmey9 28:4a833d59897b 201
timmey9 27:8c2b30c855d1 202 // The ethernet setup must be within the first few lines of code, otherwise the program hangs
timmey9 17:2f978f823020 203 EthernetInterface interface;
timmey9 21:1fb5023b72af 204 #if STATIC == 1
timmey9 20:f533b3c9296f 205 interface.init(IP, MASK, GATEWAY);
timmey9 20:f533b3c9296f 206 #else
timmey9 17:2f978f823020 207 interface.init();
timmey9 20:f533b3c9296f 208 #endif
timmey9 17:2f978f823020 209 interface.connect();
timmey9 20:f533b3c9296f 210 pc.printf("IP Address is: %s\n\r", interface.getIPAddress());
timmey9 20:f533b3c9296f 211 pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask());
timmey9 20:f533b3c9296f 212 pc.printf("MAC address is: %s\n\r", interface.getMACAddress());
timmey9 20:f533b3c9296f 213 pc.printf("Gateway is: %s\n\r", interface.getGateway());
timmey9 21:1fb5023b72af 214 pc.printf("Port is: %i\n\r", PORT);
timmey9 20:f533b3c9296f 215
timmey9 28:4a833d59897b 216
timmey9 22:523e316cbe70 217 // ethernet setup failed for some reason. Flash yellow light then uC resets itself
timmey9 23:9e5141647775 218 if(interface.getIPAddress() == 0)
timmey9 22:523e316cbe70 219 {
timmey9 22:523e316cbe70 220 for(int i = 0; i < 5; i++)
timmey9 22:523e316cbe70 221 {
timmey9 22:523e316cbe70 222 led_red = 0;
timmey9 22:523e316cbe70 223 led_green = 0;
timmey9 22:523e316cbe70 224 wait_ms(500);
timmey9 22:523e316cbe70 225 led_red = 1;
timmey9 22:523e316cbe70 226 led_green = 1;
timmey9 22:523e316cbe70 227 wait_ms(1000);
timmey9 22:523e316cbe70 228 }
timmey9 22:523e316cbe70 229 NVIC_SystemReset();
timmey9 23:9e5141647775 230 }
timmey9 22:523e316cbe70 231
timmey9 27:8c2b30c855d1 232
timmey9 27:8c2b30c855d1 233 analog_initialization(A0);
timmey9 27:8c2b30c855d1 234 analog_initialization(A2);
timmey9 27:8c2b30c855d1 235
timmey9 27:8c2b30c855d1 236
timmey9 27:8c2b30c855d1 237 // Start the sampling loop
timmey9 27:8c2b30c855d1 238 current_sample_index = WAITING_TO_BEGIN;
timmey9 28:4a833d59897b 239 //Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
timmey9 27:8c2b30c855d1 240
timmey9 28:4a833d59897b 241 //NVIC_SetPriority(TIMER3_IRQn,0);
timmey9 28:4a833d59897b 242 //pc.printf("Ticker IRQ: %i\r\n", Sampler.irq());
timmey9 27:8c2b30c855d1 243
timmey9 27:8c2b30c855d1 244
timmey9 22:523e316cbe70 245 network::Select select;
timmey9 22:523e316cbe70 246 network::tcp::Socket server;
timmey9 22:523e316cbe70 247 network::tcp::Socket client[MAX_CLIENTS];
timmey9 22:523e316cbe70 248 network::tcp::Socket *socket = NULL;
timmey9 17:2f978f823020 249
timmey9 17:2f978f823020 250 int result = 0;
timmey9 17:2f978f823020 251 int index = 0;
timmey9 17:2f978f823020 252
timmey9 27:8c2b30c855d1 253 network::Buffer buffer(50);
timmey9 21:1fb5023b72af 254 std::string message(NAME);
timmey9 17:2f978f823020 255
timmey9 21:1fb5023b72af 256 // Configure the server socket (assume every thing works)
timmey9 17:2f978f823020 257 server.open();
timmey9 20:f533b3c9296f 258 server.bind(PORT);
timmey9 17:2f978f823020 259 server.listen(MAX_CLIENTS);
timmey9 27:8c2b30c855d1 260
timmey9 17:2f978f823020 261 // Add sockets to the select api
timmey9 22:523e316cbe70 262 select.set(&server, network::Select::Read);
timmey9 17:2f978f823020 263 for (index = 0; index < MAX_CLIENTS; index++) {
timmey9 22:523e316cbe70 264 select.set(&client[index], network::Select::Read);
timmey9 17:2f978f823020 265 }
timmey9 22:523e316cbe70 266
timmey9 22:523e316cbe70 267 led_red = 1;
timmey9 22:523e316cbe70 268 led_green = 1;
timmey9 22:523e316cbe70 269 led_blue = 0;
timmey9 23:9e5141647775 270 wait_ms(500);
timmey9 22:523e316cbe70 271 led_blue = 1;
timmey9 23:9e5141647775 272 wait_ms(200);
timmey9 23:9e5141647775 273 led_blue = 0;
timmey9 23:9e5141647775 274 wait_ms(500);
timmey9 23:9e5141647775 275 led_blue = 1;
timmey9 22:523e316cbe70 276
timmey9 28:4a833d59897b 277 NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
timmey9 28:4a833d59897b 278 NVIC_SetPriority(ENET_Transmit_IRQn,1);
timmey9 28:4a833d59897b 279 NVIC_SetPriority(ENET_Receive_IRQn,1);
timmey9 28:4a833d59897b 280 NVIC_SetPriority(ENET_Error_IRQn,1);
timmey9 28:4a833d59897b 281
timmey9 28:4a833d59897b 282 //for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i));
timmey9 28:4a833d59897b 283
timmey9 17:2f978f823020 284 do {
timmey9 17:2f978f823020 285 // Wait for activity
timmey9 17:2f978f823020 286 result = select.wait();
timmey9 17:2f978f823020 287 if (result < -1) {
timmey9 18:b17ddeeb1c09 288 pc.printf("Failed to select\n\r");
emilmont 7:65188f4a8c25 289 break;
timmey9 16:c3f922f61b8f 290 }
timmey9 17:2f978f823020 291
timmey9 17:2f978f823020 292 // Get the first socket
timmey9 22:523e316cbe70 293 socket = (network::tcp::Socket *)select.getReadable();
timmey9 17:2f978f823020 294
timmey9 22:523e316cbe70 295 for (; socket != NULL; socket = (network::tcp::Socket *)select.getReadable()) {
timmey9 17:2f978f823020 296 // Check if there was a connection request.
timmey9 17:2f978f823020 297 if (socket->getHandle() == server.getHandle()) {
timmey9 17:2f978f823020 298 // Find an unused client
timmey9 17:2f978f823020 299 for (index = 0; index < MAX_CLIENTS; index++) {
timmey9 17:2f978f823020 300 if (client[index].getStatus() == network::Socket::Closed) {
timmey9 17:2f978f823020 301 break;
timmey9 17:2f978f823020 302 }
timmey9 17:2f978f823020 303 }
timmey9 17:2f978f823020 304
timmey9 17:2f978f823020 305 // Maximum connections reached
timmey9 17:2f978f823020 306 if (index == MAX_CLIENTS) {
timmey9 18:b17ddeeb1c09 307 pc.printf("Maximum connections reached\n\r");
timmey9 21:1fb5023b72af 308 wait(1);
timmey9 17:2f978f823020 309 continue;
timmey9 17:2f978f823020 310 }
timmey9 20:f533b3c9296f 311
timmey9 17:2f978f823020 312 // Accept the client
timmey9 17:2f978f823020 313 socket->accept(client[index]);
timmey9 18:b17ddeeb1c09 314 pc.printf("Client connected %s:%d\n\r",
timmey9 17:2f978f823020 315 client[index].getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 316 client[index].getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 317
timmey9 21:1fb5023b72af 318 // Send a nice message to the client (tell MATLAB your name
timmey9 17:2f978f823020 319 client[index].write((void *)message.data(), message.size());
timmey9 20:f533b3c9296f 320
timmey9 17:2f978f823020 321 continue;
timmey9 16:c3f922f61b8f 322 }
timmey9 20:f533b3c9296f 323
timmey9 17:2f978f823020 324 // It was not the server socket, so it must be a client talking to us.
timmey9 17:2f978f823020 325 switch (socket->read(buffer)) {
timmey9 17:2f978f823020 326 case 0:
timmey9 17:2f978f823020 327 // Remote end disconnected
timmey9 18:b17ddeeb1c09 328 pc.printf("Client disconnected %s:%d\n\r",
timmey9 17:2f978f823020 329 socket->getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 330 socket->getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 331
timmey9 17:2f978f823020 332 // Close socket
timmey9 17:2f978f823020 333 socket->close();
timmey9 17:2f978f823020 334 break;
timmey9 20:f533b3c9296f 335
timmey9 17:2f978f823020 336 case -1:
timmey9 18:b17ddeeb1c09 337 pc.printf("Error while reading data from socket\n\r");
timmey9 17:2f978f823020 338 socket->close();
timmey9 17:2f978f823020 339 break;
timmey9 20:f533b3c9296f 340 //************* this is where data is printed to the screen
timmey9 17:2f978f823020 341 default:
timmey9 18:b17ddeeb1c09 342 pc.printf("Message from %s:%d\n\r",
timmey9 17:2f978f823020 343 socket->getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 344 socket->getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 345
timmey9 21:1fb5023b72af 346 pc.printf("%s\n\r", (char *)buffer.data());
timmey9 21:1fb5023b72af 347
timmey9 21:1fb5023b72af 348 // read first character for command
timmey9 21:1fb5023b72af 349 char command[2];
timmey9 21:1fb5023b72af 350 buffer.read(command,2,0);
timmey9 21:1fb5023b72af 351 if(command[1] == ':') {
timmey9 21:1fb5023b72af 352 switch(command[0])
timmey9 21:1fb5023b72af 353 {
timmey9 21:1fb5023b72af 354 case 'b':
timmey9 21:1fb5023b72af 355 led_blue = !led_blue;
timmey9 21:1fb5023b72af 356 client[index].write((void *)"Blue LED\n",9);
timmey9 21:1fb5023b72af 357 break;
timmey9 25:abbc19af13f9 358
timmey9 21:1fb5023b72af 359 case 'r':
timmey9 21:1fb5023b72af 360 led_red = !led_red;
timmey9 21:1fb5023b72af 361 client[index].write((void *)"Red LED\n",8);
timmey9 21:1fb5023b72af 362 break;
timmey9 25:abbc19af13f9 363
timmey9 21:1fb5023b72af 364 case 'p':
timmey9 21:1fb5023b72af 365 led_green = !led_green;
timmey9 21:1fb5023b72af 366 client[index].write((void *)"Data\n",5);
timmey9 27:8c2b30c855d1 367 for(int i = 0; i < 99; i++) sample_array1[i] = i;
timmey9 27:8c2b30c855d1 368 client[index].write((void *)&sample_array1,2*99);
timmey9 24:a5891669afc5 369 break;
timmey9 28:4a833d59897b 370 case 't':
timmey9 28:4a833d59897b 371 {
timmey9 28:4a833d59897b 372 for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i));
timmey9 28:4a833d59897b 373 }
timmey9 28:4a833d59897b 374 break;
timmey9 25:abbc19af13f9 375
timmey9 25:abbc19af13f9 376 case '1': // run motor and sample
timmey9 25:abbc19af13f9 377 {
timmey9 28:4a833d59897b 378
timmey9 31:09e7a8b3c7e7 379 motorA = 1;
timmey9 31:09e7a8b3c7e7 380 wait_us(10000);
timmey9 31:09e7a8b3c7e7 381 motorA = 0;
timmey9 28:4a833d59897b 382 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 28:4a833d59897b 383 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 28:4a833d59897b 384 client[index].write((void *)"Data\n",5);
timmey9 28:4a833d59897b 385
timmey9 28:4a833d59897b 386 TotalInd = 1;
timmey9 28:4a833d59897b 387
timmey9 28:4a833d59897b 388 uint32_t AMT20_AB;
timmey9 28:4a833d59897b 389 rotary_count = 0;
timmey9 28:4a833d59897b 390 __disable_irq();
timmey9 28:4a833d59897b 391 SampleInd = 0;
timmey9 28:4a833d59897b 392 for(int i = 0; i < TOTAL_SAMPLES; i++)
timmey9 28:4a833d59897b 393 {
timmey9 28:4a833d59897b 394 SampleInd = !SampleInd;
timmey9 28:4a833d59897b 395 sample_array1[i] = adc_hal_get_conversion_value(0, 0);
timmey9 28:4a833d59897b 396 sample_array2[i] = adc_hal_get_conversion_value(1, 0);
timmey9 28:4a833d59897b 397 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 28:4a833d59897b 398 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 28:4a833d59897b 399
timmey9 28:4a833d59897b 400 // The following updates the rotary counter for the AMT20 sensor
timmey9 28:4a833d59897b 401 // Put A on PTC0
timmey9 28:4a833d59897b 402 // Put B on PTC1
timmey9 28:4a833d59897b 403 AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 28:4a833d59897b 404
timmey9 28:4a833d59897b 405 if (AMT20_AB != last_AMT20_AB_read)
timmey9 28:4a833d59897b 406 {
timmey9 28:4a833d59897b 407 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 28:4a833d59897b 408 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 28:4a833d59897b 409 #if INVERT_ANGLE == 1
timmey9 28:4a833d59897b 410 {rotary_count--;}
timmey9 28:4a833d59897b 411 else
timmey9 28:4a833d59897b 412 {rotary_count++;}
timmey9 28:4a833d59897b 413 #else
timmey9 28:4a833d59897b 414 {rotary_count++;}
timmey9 28:4a833d59897b 415 else
timmey9 28:4a833d59897b 416 {rotary_count--;}
timmey9 28:4a833d59897b 417 #endif
timmey9 28:4a833d59897b 418
timmey9 28:4a833d59897b 419 last_AMT20_AB_read = AMT20_AB;
timmey9 27:8c2b30c855d1 420 }
timmey9 28:4a833d59897b 421 angle_array[i] = rotary_count;
timmey9 28:4a833d59897b 422 wait_us(8);
timmey9 28:4a833d59897b 423 }
timmey9 28:4a833d59897b 424 __enable_irq();
timmey9 26:a00bf9837e03 425
timmey9 28:4a833d59897b 426 NVIC_SetPriority(ENET_1588_Timer_IRQn,0);
timmey9 28:4a833d59897b 427 NVIC_SetPriority(ENET_Transmit_IRQn,0);
timmey9 28:4a833d59897b 428 NVIC_SetPriority(ENET_Receive_IRQn,0);
timmey9 28:4a833d59897b 429 NVIC_SetPriority(ENET_Error_IRQn,0);
timmey9 28:4a833d59897b 430 TotalInd = 1;
timmey9 27:8c2b30c855d1 431 client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES);
timmey9 27:8c2b30c855d1 432 client[index].write((void *)&sample_array2,2*TOTAL_SAMPLES);
timmey9 27:8c2b30c855d1 433 client[index].write((void *)&angle_array,2*TOTAL_SAMPLES);
timmey9 28:4a833d59897b 434 TotalInd = 0;
timmey9 28:4a833d59897b 435
timmey9 28:4a833d59897b 436 NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
timmey9 28:4a833d59897b 437 NVIC_SetPriority(ENET_Transmit_IRQn,1);
timmey9 28:4a833d59897b 438 NVIC_SetPriority(ENET_Receive_IRQn,1);
timmey9 28:4a833d59897b 439 NVIC_SetPriority(ENET_Error_IRQn,1);
timmey9 28:4a833d59897b 440
timmey9 25:abbc19af13f9 441 }
timmey9 24:a5891669afc5 442 break;
timmey9 25:abbc19af13f9 443
timmey9 25:abbc19af13f9 444 case '2': // run just the motor
timmey9 25:abbc19af13f9 445 {
timmey9 25:abbc19af13f9 446
timmey9 25:abbc19af13f9 447 }
timmey9 24:a5891669afc5 448 break;
timmey9 24:a5891669afc5 449 case 'a':
timmey9 24:a5891669afc5 450 if(angle_encoder.set_zero(&rotary_count)) {
timmey9 24:a5891669afc5 451 client[index].write((void *) "Zero set\n",9);
timmey9 24:a5891669afc5 452 }
timmey9 24:a5891669afc5 453 else {
timmey9 24:a5891669afc5 454 client[index].write((void *) "Zero NOT set\n",13);
timmey9 24:a5891669afc5 455 }
timmey9 24:a5891669afc5 456 break;
timmey9 24:a5891669afc5 457 case 's':
timmey9 24:a5891669afc5 458 {
timmey9 24:a5891669afc5 459 char buf[16];
timmey9 24:a5891669afc5 460 sprintf(buf,"NOP: %x\n",angle_encoder.nop());
timmey9 24:a5891669afc5 461 client[index].write((void *) buf,16);
timmey9 24:a5891669afc5 462 break;
timmey9 24:a5891669afc5 463 }
timmey9 24:a5891669afc5 464 case 'd':
timmey9 24:a5891669afc5 465 {
timmey9 24:a5891669afc5 466 char buf[29];
timmey9 24:a5891669afc5 467 sprintf(buf,"Angle: %i %i\n",angle_encoder.absolute_angle(), rotary_count);
timmey9 24:a5891669afc5 468 client[index].write((void *) buf,29);
timmey9 24:a5891669afc5 469 break;
timmey9 28:4a833d59897b 470 }
timmey9 21:1fb5023b72af 471 }
timmey9 21:1fb5023b72af 472 }
timmey9 17:2f978f823020 473 break;
timmey9 17:2f978f823020 474 }
timmey9 16:c3f922f61b8f 475 }
timmey9 17:2f978f823020 476
timmey9 17:2f978f823020 477 } while (server.getStatus() == network::Socket::Listening);
timmey9 22:523e316cbe70 478 }
timmey9 23:9e5141647775 479
timmey9 22:523e316cbe70 480 void timed_sampling() {
timmey9 28:4a833d59897b 481 SampleInd = 1;
timmey9 27:8c2b30c855d1 482 //__disable_irq(); // Disable Interrupts
timmey9 23:9e5141647775 483 //timeStamp.start();
timmey9 28:4a833d59897b 484 /*
timmey9 22:523e316cbe70 485 // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another
timmey9 22:523e316cbe70 486 uint32_t A0_value = adc_hal_get_conversion_value(0, 0);
timmey9 22:523e316cbe70 487 uint32_t A2_value = adc_hal_get_conversion_value(1, 0);
timmey9 22:523e316cbe70 488 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 22:523e316cbe70 489 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 22:523e316cbe70 490
timmey9 22:523e316cbe70 491 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 492 // Put A on PTC0
timmey9 22:523e316cbe70 493 // Put B on PTC1
timmey9 22:523e316cbe70 494 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 495 //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time.
timmey9 22:523e316cbe70 496 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 497 {
timmey9 22:523e316cbe70 498 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 499 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 500 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 501 {rotary_count--;}
timmey9 22:523e316cbe70 502 else
timmey9 22:523e316cbe70 503 {rotary_count++;}
timmey9 22:523e316cbe70 504 #else
timmey9 22:523e316cbe70 505 {rotary_count++;}
timmey9 22:523e316cbe70 506 else
timmey9 22:523e316cbe70 507 {rotary_count--;}
timmey9 22:523e316cbe70 508 #endif
timmey9 22:523e316cbe70 509
timmey9 22:523e316cbe70 510 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 511 }
timmey9 22:523e316cbe70 512 //current_sample_index = BEGIN_SAMPLING; // Used to force extra time.
timmey9 22:523e316cbe70 513 if (current_sample_index == WAITING_TO_BEGIN) {}
timmey9 22:523e316cbe70 514 else
timmey9 28:4a833d59897b 515 {
timmey9 22:523e316cbe70 516 if (current_sample_index == BEGIN_SAMPLING) {
timmey9 22:523e316cbe70 517 current_sample_index = FIRST_SAMPLE_INDEX;
timmey9 22:523e316cbe70 518 }
timmey9 22:523e316cbe70 519
timmey9 27:8c2b30c855d1 520 sample_array1[current_sample_index] = A0_value;
timmey9 27:8c2b30c855d1 521 sample_array2[current_sample_index] = A2_value;
timmey9 22:523e316cbe70 522 angle_array[current_sample_index] = rotary_count;
timmey9 22:523e316cbe70 523
timmey9 22:523e316cbe70 524 if (current_sample_index == LAST_SAMPLE_INDEX) {
timmey9 22:523e316cbe70 525 current_sample_index = WAITING_TO_BEGIN;
timmey9 22:523e316cbe70 526 }
timmey9 22:523e316cbe70 527 else { current_sample_index++; }
timmey9 28:4a833d59897b 528 }
timmey9 22:523e316cbe70 529
timmey9 23:9e5141647775 530 //int tempVar = timeStamp.read_us();
timmey9 23:9e5141647775 531 //timeStamp.stop();
timmey9 23:9e5141647775 532 //timeStamp.reset();
timmey9 23:9e5141647775 533 //pc.printf("TimeStamp: %i\r\n", tempVar);
timmey9 27:8c2b30c855d1 534 //__enable_irq(); // Enable Interrupts
timmey9 27:8c2b30c855d1 535 */
timmey9 28:4a833d59897b 536 SampleInd = 0;
timmey9 22:523e316cbe70 537 }
timmey9 22:523e316cbe70 538
timmey9 22:523e316cbe70 539 void analog_initialization(PinName pin)
timmey9 22:523e316cbe70 540 {
timmey9 22:523e316cbe70 541 ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
timmey9 22:523e316cbe70 542 // MBED_ASSERT(adc != (ADCName)NC);
timmey9 22:523e316cbe70 543
timmey9 22:523e316cbe70 544 uint32_t instance = adc >> ADC_INSTANCE_SHIFT;
timmey9 22:523e316cbe70 545
timmey9 22:523e316cbe70 546 clock_manager_set_gate(kClockModuleADC, instance, true);
timmey9 22:523e316cbe70 547
timmey9 22:523e316cbe70 548 uint32_t bus_clock;
timmey9 22:523e316cbe70 549 clock_manager_get_frequency(kBusClock, &bus_clock);
timmey9 22:523e316cbe70 550 uint32_t clkdiv;
timmey9 22:523e316cbe70 551 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
timmey9 22:523e316cbe70 552 if ((bus_clock >> clkdiv) <= MAX_FADC)
timmey9 22:523e316cbe70 553 break;
timmey9 22:523e316cbe70 554 }
timmey9 22:523e316cbe70 555 if (clkdiv == 4) {
timmey9 22:523e316cbe70 556 clkdiv = 0x7; //Set max div
timmey9 22:523e316cbe70 557 }
timmey9 22:523e316cbe70 558 // adc is enabled/triggered when reading.
timmey9 22:523e316cbe70 559 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
timmey9 22:523e316cbe70 560 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
timmey9 22:523e316cbe70 561 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
timmey9 22:523e316cbe70 562 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
timmey9 22:523e316cbe70 563 adc_hal_configure_continuous_conversion(instance, false);
timmey9 22:523e316cbe70 564 adc_hal_configure_hw_trigger(instance, false); // sw trigger
timmey9 22:523e316cbe70 565 adc_hal_configure_hw_average(instance, false);
timmey9 22:523e316cbe70 566 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
timmey9 22:523e316cbe70 567 adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail
timmey9 22:523e316cbe70 568
timmey9 22:523e316cbe70 569 pinmap_pinout(pin, PinMap_ADC);
timmey9 22:523e316cbe70 570 }
timmey9 22:523e316cbe70 571
timmey9 22:523e316cbe70 572 void output_data(uint32_t iteration_number)
timmey9 22:523e316cbe70 573 {
timmey9 22:523e316cbe70 574 pc.printf("Iteration: %i\n\r", iteration_number);
timmey9 22:523e316cbe70 575 pc.printf("Sampling rate: %i\n\r", SAMPLING_RATE);
timmey9 22:523e316cbe70 576 pc.printf("Data length: %i\n\r", TOTAL_SAMPLES);
timmey9 27:8c2b30c855d1 577
timmey9 27:8c2b30c855d1 578 //for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) {
timmey9 27:8c2b30c855d1 579 // pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]);
timmey9 27:8c2b30c855d1 580 // }
timmey9 22:523e316cbe70 581
timmey9 22:523e316cbe70 582 }
timmey9 27:8c2b30c855d1 583
timmey9 27:8c2b30c855d1 584 // read some registers for some info.
timmey9 27:8c2b30c855d1 585 //uint32_t* rcr = (uint32_t*) 0x400C0084;
timmey9 27:8c2b30c855d1 586 //uint32_t* ecr = (uint32_t*) 0x400C0024;
timmey9 27:8c2b30c855d1 587 //pc.printf("RCR register: %x\r\n", *rcr);
timmey9 27:8c2b30c855d1 588 //pc.printf("ECR register: %x\r\n", *ecr);