Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Fri Dec 05 05:56:33 2014 +0000
Revision:
27:8c2b30c855d1
Parent:
26:a00bf9837e03
Child:
28:4a833d59897b
Moved ethernet initialization to beginning of program.  The program now compiles.

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