https://www.hackster.io/PSoC_Rocks/water-quality-monitoring-autonomous-robot-0bbf88
Dependencies: BLE_API MAG3110 MMA8652 PID mbed nRF51822
Fork of uBit_BLE_UART_Voltmeter_IoT by
main.cpp@12:0777ec4114c8, 2018-10-29 (annotated)
- Committer:
- suntopbd
- Date:
- Mon Oct 29 19:05:11 2018 +0000
- Revision:
- 12:0777ec4114c8
- Parent:
- 11:6916c05fde52
bbc microbit waterbot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
suntopbd | 12:0777ec4114c8 | 1 | /* mbed MicrocontrollerLL Library |
rgrover1 | 0:2130d7f559dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:2130d7f559dc | 3 | * |
rgrover1 | 0:2130d7f559dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:2130d7f559dc | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:2130d7f559dc | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:2130d7f559dc | 7 | * |
rgrover1 | 0:2130d7f559dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:2130d7f559dc | 9 | * |
rgrover1 | 0:2130d7f559dc | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:2130d7f559dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:2130d7f559dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:2130d7f559dc | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:2130d7f559dc | 14 | * limitations under the License. |
rgrover1 | 0:2130d7f559dc | 15 | */ |
suntopbd | 12:0777ec4114c8 | 16 | |
suntopbd | 10:053397a8dc40 | 17 | ////////////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 18 | ///////////////////////NOTE /////////////////////////////// |
suntopbd | 10:053397a8dc40 | 19 | // This program uses BBC microbit as a NRF51822 board // |
suntopbd | 10:053397a8dc40 | 20 | // with limited onboard functionality, mainly focused // |
suntopbd | 10:053397a8dc40 | 21 | // BLE-uart/ADC/DIO/PWM capability, see pin maping below // |
suntopbd | 10:053397a8dc40 | 22 | /////////////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 23 | /////////////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 24 | |
suntopbd | 12:0777ec4114c8 | 25 | //////////////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 26 | //////////////////////BLE WATER BOT///////////////////////// |
suntopbd | 12:0777ec4114c8 | 27 | //////////////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 28 | |
suntopbd | 12:0777ec4114c8 | 29 | |
suntopbd | 10:053397a8dc40 | 30 | |
rgrover1 | 5:2607923acfa1 | 31 | #include <string.h> |
rgrover1 | 0:2130d7f559dc | 32 | #include "mbed.h" |
suntopbd | 12:0777ec4114c8 | 33 | #include "PID.h" |
rgrover1 | 8:f9bf6d403cde | 34 | #include "BLE.h" |
suntopbd | 10:053397a8dc40 | 35 | #include "stdio.h" |
rgrover1 | 0:2130d7f559dc | 36 | #include "UARTService.h" |
rgrover1 | 0:2130d7f559dc | 37 | |
suntopbd | 11:6916c05fde52 | 38 | #include "ble/BLE.h" |
suntopbd | 11:6916c05fde52 | 39 | #include "ble/services/URIBeaconConfigService.h" |
suntopbd | 11:6916c05fde52 | 40 | #include "ble/services/DFUService.h" |
suntopbd | 11:6916c05fde52 | 41 | #include "ble/services/DeviceInformationService.h" |
suntopbd | 11:6916c05fde52 | 42 | #include "ConfigParamsPersistence.h" |
suntopbd | 11:6916c05fde52 | 43 | |
rgrover1 | 0:2130d7f559dc | 44 | |
suntopbd | 12:0777ec4114c8 | 45 | #include "math.h" |
suntopbd | 12:0777ec4114c8 | 46 | #include "MAG3110.h" |
suntopbd | 12:0777ec4114c8 | 47 | |
suntopbd | 12:0777ec4114c8 | 48 | |
suntopbd | 12:0777ec4114c8 | 49 | |
suntopbd | 12:0777ec4114c8 | 50 | #define INTERVAL 0.1 |
suntopbd | 12:0777ec4114c8 | 51 | |
suntopbd | 10:053397a8dc40 | 52 | #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host); |
rgrover1 | 0:2130d7f559dc | 53 | #if NEED_CONSOLE_OUTPUT |
suntopbd | 11:6916c05fde52 | 54 | #define blePrintf(STR) { if (uart) uart->write(STR, strlen(STR)); } |
rgrover1 | 0:2130d7f559dc | 55 | #else |
suntopbd | 11:6916c05fde52 | 56 | #define blePrintf(...) |
suntopbd | 10:053397a8dc40 | 57 | #endif |
suntopbd | 10:053397a8dc40 | 58 | |
suntopbd | 10:053397a8dc40 | 59 | ///////////////// pin table ///////////////////////// |
suntopbd | 10:053397a8dc40 | 60 | ///////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 61 | // edge.pin nrf51822pin functions note // |
suntopbd | 10:053397a8dc40 | 62 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 63 | // P2 P0_1 ADC/PWM/DIO 2 // |
suntopbd | 10:053397a8dc40 | 64 | // P1 P0_2 ADC/PWM/DIO 1 // |
suntopbd | 10:053397a8dc40 | 65 | // P0 P0_3 ADC/PWM/DIO 0 // |
suntopbd | 12:0777ec4114c8 | 66 | // P4 P0_5 ADC COL 2 // |
suntopbd | 12:0777ec4114c8 | 67 | // P10 P0_6 ADC COL 3 // |
suntopbd | 10:053397a8dc40 | 68 | // P16 P_16 DIO // |
suntopbd | 10:053397a8dc40 | 69 | // P14 P0_25 SPI MIS/DIO // |
suntopbd | 10:053397a8dc40 | 70 | // P5 P_17 Button A/DI pullup // |
suntopbd | 10:053397a8dc40 | 71 | // P11 P_26 Button B/DI pullup // |
suntopbd | 12:0777ec4114c8 | 72 | // P20 P0_30 I2C SDA/DIO pullup // |
suntopbd | 12:0777ec4114c8 | 73 | // P19 P0_0 I2C SCL/DIO pullup // |
suntopbd | 10:053397a8dc40 | 74 | // P15 SPI MOS/DIO // |
suntopbd | 10:053397a8dc40 | 75 | // P13 SPI SCK/DIO // |
suntopbd | 10:053397a8dc40 | 76 | |
suntopbd | 10:053397a8dc40 | 77 | ///////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 78 | // LED Matrix pins // |
suntopbd | 12:0777ec4114c8 | 79 | // ROW 1 P0_13 ROW 2 P0_14 ROW 3 P0_15 // |
suntopbd | 12:0777ec4114c8 | 80 | // COL 1 P0_4 COL 2 P0_5 COL 3 P0_6 // |
suntopbd | 12:0777ec4114c8 | 81 | // COL 4 P0_7 COL 5 P0_8 COL 6 P0_9 // |
suntopbd | 12:0777ec4114c8 | 82 | // COL 7 P0_10 COL 8 P0_11 COL 9 P0_12 // |
suntopbd | 10:053397a8dc40 | 83 | ///////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 84 | /////////// See uBit to nrf51822 Schematic for detailed pinout ////////////////////// |
suntopbd | 12:0777ec4114c8 | 85 | // https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf // |
suntopbd | 12:0777ec4114c8 | 86 | ///////////////////////////////////////////////////////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 87 | //Kc, Ti, Td, interval |
suntopbd | 12:0777ec4114c8 | 88 | PID controllerL(1.0, 0.1, 0.1, INTERVAL); |
suntopbd | 12:0777ec4114c8 | 89 | PID controllerR(1.0, 0.1, 0.1, INTERVAL); |
suntopbd | 12:0777ec4114c8 | 90 | |
suntopbd | 12:0777ec4114c8 | 91 | |
suntopbd | 10:053397a8dc40 | 92 | |
rgrover1 | 0:2130d7f559dc | 93 | |
suntopbd | 12:0777ec4114c8 | 94 | // Fan drive pwm |
suntopbd | 12:0777ec4114c8 | 95 | |
suntopbd | 12:0777ec4114c8 | 96 | PwmOut LP(P0_2); // left propeller |
suntopbd | 12:0777ec4114c8 | 97 | PwmOut RP(P0_1); // right propeller |
suntopbd | 12:0777ec4114c8 | 98 | |
suntopbd | 12:0777ec4114c8 | 99 | MAG3110 mag(P0_30, P0_0); // set i2c pins SDA, SCL here // |
rgrover1 | 0:2130d7f559dc | 100 | BLEDevice ble; |
rgrover1 | 5:2607923acfa1 | 101 | UARTService *uart; |
suntopbd | 12:0777ec4114c8 | 102 | |
suntopbd | 12:0777ec4114c8 | 103 | AnalogIn temperature(P0_3); |
suntopbd | 12:0777ec4114c8 | 104 | AnalogIn turbidity(P0_5); |
suntopbd | 12:0777ec4114c8 | 105 | AnalogIn conductivity(P0_6); |
suntopbd | 12:0777ec4114c8 | 106 | |
suntopbd | 12:0777ec4114c8 | 107 | DigitalOut row1(P0_13); // uBit 1st LED cathode |
suntopbd | 12:0777ec4114c8 | 108 | DigitalOut col1(P0_4); // uBit 1st LED anode |
suntopbd | 12:0777ec4114c8 | 109 | DigitalIn swA(P0_17); // uBit button A |
suntopbd | 12:0777ec4114c8 | 110 | // col2(P0_5); // adc pin now led removed |
suntopbd | 12:0777ec4114c8 | 111 | // col3(P0_6); // adc pin now led removed |
suntopbd | 12:0777ec4114c8 | 112 | |
suntopbd | 12:0777ec4114c8 | 113 | //DigitalOut col1(P0_4); |
suntopbd | 12:0777ec4114c8 | 114 | |
suntopbd | 12:0777ec4114c8 | 115 | |
suntopbd | 11:6916c05fde52 | 116 | int val,dVal, dec, i; |
suntopbd | 12:0777ec4114c8 | 117 | int blepush = 0; |
suntopbd | 12:0777ec4114c8 | 118 | int refupdate = 0; |
suntopbd | 11:6916c05fde52 | 119 | char result[100]; |
suntopbd | 12:0777ec4114c8 | 120 | float TEMP = 0.0; |
suntopbd | 12:0777ec4114c8 | 121 | float TURB = 0.0; |
suntopbd | 12:0777ec4114c8 | 122 | float COND = 0.0; |
suntopbd | 12:0777ec4114c8 | 123 | |
suntopbd | 12:0777ec4114c8 | 124 | float heading = 0.0; // magnetometer heading |
suntopbd | 12:0777ec4114c8 | 125 | float heading_ref = 0.0; |
rgrover1 | 0:2130d7f559dc | 126 | |
rgrover1 | 9:5f0732aa3008 | 127 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
rgrover1 | 0:2130d7f559dc | 128 | { |
rgrover1 | 0:2130d7f559dc | 129 | ble.startAdvertising(); |
rgrover1 | 0:2130d7f559dc | 130 | } |
rgrover1 | 0:2130d7f559dc | 131 | |
rgrover1 | 0:2130d7f559dc | 132 | void periodicCallback(void) |
rgrover1 | 0:2130d7f559dc | 133 | { |
suntopbd | 12:0777ec4114c8 | 134 | blepush = 1; |
suntopbd | 12:0777ec4114c8 | 135 | // blePrintf("heading "); |
suntopbd | 12:0777ec4114c8 | 136 | // blePrintf(result); |
suntopbd | 12:0777ec4114c8 | 137 | // blePrintf("\r\n"); |
suntopbd | 12:0777ec4114c8 | 138 | } |
suntopbd | 12:0777ec4114c8 | 139 | |
suntopbd | 12:0777ec4114c8 | 140 | void navigationAdjust (void) |
suntopbd | 12:0777ec4114c8 | 141 | { |
suntopbd | 10:053397a8dc40 | 142 | |
rgrover1 | 0:2130d7f559dc | 143 | } |
suntopbd | 11:6916c05fde52 | 144 | |
suntopbd | 12:0777ec4114c8 | 145 | void calXY() //magnetometer calibration: finding max and min of X, Y axis |
suntopbd | 12:0777ec4114c8 | 146 | { |
suntopbd | 12:0777ec4114c8 | 147 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
suntopbd | 12:0777ec4114c8 | 148 | |
suntopbd | 12:0777ec4114c8 | 149 | wait(3); |
suntopbd | 12:0777ec4114c8 | 150 | |
suntopbd | 12:0777ec4114c8 | 151 | tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); |
suntopbd | 12:0777ec4114c8 | 152 | tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); |
suntopbd | 12:0777ec4114c8 | 153 | int sample = 0; |
suntopbd | 12:0777ec4114c8 | 154 | |
suntopbd | 12:0777ec4114c8 | 155 | while(sample<501) { |
suntopbd | 12:0777ec4114c8 | 156 | wait(0.05); |
suntopbd | 12:0777ec4114c8 | 157 | newX = mag.readVal(MAG_OUT_X_MSB); |
suntopbd | 12:0777ec4114c8 | 158 | newY = mag.readVal(MAG_OUT_Y_MSB); |
suntopbd | 12:0777ec4114c8 | 159 | if (newX > tempXmax) tempXmax = newX; |
suntopbd | 12:0777ec4114c8 | 160 | if (newX < tempXmin) tempXmin = newX; |
suntopbd | 12:0777ec4114c8 | 161 | if (newY > tempYmax) tempYmax = newY; |
suntopbd | 12:0777ec4114c8 | 162 | if (newY < tempYmin) tempYmin = newY; |
suntopbd | 12:0777ec4114c8 | 163 | sample++; |
suntopbd | 12:0777ec4114c8 | 164 | col1.write(!(col1.read())); |
suntopbd | 12:0777ec4114c8 | 165 | //col3.write(!(col3.read())); |
suntopbd | 12:0777ec4114c8 | 166 | |
suntopbd | 12:0777ec4114c8 | 167 | } |
suntopbd | 12:0777ec4114c8 | 168 | |
suntopbd | 12:0777ec4114c8 | 169 | mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
suntopbd | 12:0777ec4114c8 | 170 | |
suntopbd | 12:0777ec4114c8 | 171 | |
suntopbd | 12:0777ec4114c8 | 172 | } |
suntopbd | 12:0777ec4114c8 | 173 | |
suntopbd | 12:0777ec4114c8 | 174 | |
rgrover1 | 0:2130d7f559dc | 175 | int main(void) |
rgrover1 | 0:2130d7f559dc | 176 | { |
suntopbd | 12:0777ec4114c8 | 177 | // pwm freq |
suntopbd | 12:0777ec4114c8 | 178 | LP.period(0.01f); |
suntopbd | 12:0777ec4114c8 | 179 | RP.period(0.01f); |
suntopbd | 12:0777ec4114c8 | 180 | LP.write(1.0); |
suntopbd | 12:0777ec4114c8 | 181 | RP.write(1.0); |
suntopbd | 12:0777ec4114c8 | 182 | wait(1); |
suntopbd | 12:0777ec4114c8 | 183 | LP.write(0.0); |
suntopbd | 12:0777ec4114c8 | 184 | RP.write(0.0); |
suntopbd | 11:6916c05fde52 | 185 | |
suntopbd | 12:0777ec4114c8 | 186 | row1.write(1); |
suntopbd | 12:0777ec4114c8 | 187 | |
rgrover1 | 0:2130d7f559dc | 188 | ble.init(); |
suntopbd | 11:6916c05fde52 | 189 | ble.onDisconnection(disconnectionCallback); |
suntopbd | 11:6916c05fde52 | 190 | |
rgrover1 | 5:2607923acfa1 | 191 | uart = new UARTService(ble); |
rgrover1 | 0:2130d7f559dc | 192 | |
suntopbd | 11:6916c05fde52 | 193 | /* setup advertising */ |
rgrover1 | 0:2130d7f559dc | 194 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
rgrover1 | 0:2130d7f559dc | 195 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 0:2130d7f559dc | 196 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
suntopbd | 10:053397a8dc40 | 197 | (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1); |
suntopbd | 11:6916c05fde52 | 198 | // device name on BLE // |
rgrover1 | 0:2130d7f559dc | 199 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 0:2130d7f559dc | 200 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
rgrover1 | 0:2130d7f559dc | 201 | |
suntopbd | 11:6916c05fde52 | 202 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
rgrover1 | 0:2130d7f559dc | 203 | ble.startAdvertising(); |
suntopbd | 12:0777ec4114c8 | 204 | |
suntopbd | 12:0777ec4114c8 | 205 | |
suntopbd | 12:0777ec4114c8 | 206 | // may necessary // |
suntopbd | 12:0777ec4114c8 | 207 | while(swA.read()) |
suntopbd | 12:0777ec4114c8 | 208 | { |
suntopbd | 12:0777ec4114c8 | 209 | ble.waitForEvent(); |
suntopbd | 12:0777ec4114c8 | 210 | col1.write(!(col1.read())); |
suntopbd | 12:0777ec4114c8 | 211 | wait(1); |
suntopbd | 12:0777ec4114c8 | 212 | blePrintf("Hold A to start\n"); |
suntopbd | 12:0777ec4114c8 | 213 | } |
suntopbd | 12:0777ec4114c8 | 214 | // max 20 Character at a time over BLE |
suntopbd | 12:0777ec4114c8 | 215 | // max 20 Character at a time over BLE |
suntopbd | 12:0777ec4114c8 | 216 | // max 20 Character at a time over BLE |
suntopbd | 12:0777ec4114c8 | 217 | |
suntopbd | 12:0777ec4114c8 | 218 | |
suntopbd | 12:0777ec4114c8 | 219 | ble.waitForEvent(); |
suntopbd | 12:0777ec4114c8 | 220 | blePrintf("Init nRF51822 \n"); |
suntopbd | 12:0777ec4114c8 | 221 | blePrintf("Calibrating Sensors\n"); |
suntopbd | 12:0777ec4114c8 | 222 | blePrintf("Rotate To Calibrate\n"); |
suntopbd | 12:0777ec4114c8 | 223 | blePrintf("when LED blinks fast\n"); |
suntopbd | 12:0777ec4114c8 | 224 | |
suntopbd | 12:0777ec4114c8 | 225 | calXY(); // do the mag calib |
suntopbd | 12:0777ec4114c8 | 226 | ble.waitForEvent(); |
suntopbd | 12:0777ec4114c8 | 227 | |
suntopbd | 12:0777ec4114c8 | 228 | blePrintf("Calibrate Completed\n"); |
suntopbd | 12:0777ec4114c8 | 229 | blePrintf("Put Robot on Water\n"); |
suntopbd | 12:0777ec4114c8 | 230 | |
suntopbd | 12:0777ec4114c8 | 231 | while(swA.read()) |
suntopbd | 12:0777ec4114c8 | 232 | { |
suntopbd | 12:0777ec4114c8 | 233 | col1.write(!(col1.read())); |
suntopbd | 12:0777ec4114c8 | 234 | wait(1); |
suntopbd | 12:0777ec4114c8 | 235 | blePrintf("Hold A to proceed\n"); |
suntopbd | 12:0777ec4114c8 | 236 | |
suntopbd | 12:0777ec4114c8 | 237 | } |
suntopbd | 12:0777ec4114c8 | 238 | |
suntopbd | 12:0777ec4114c8 | 239 | Ticker ticker; |
suntopbd | 12:0777ec4114c8 | 240 | ticker.attach(&periodicCallback, 5); |
suntopbd | 12:0777ec4114c8 | 241 | |
suntopbd | 12:0777ec4114c8 | 242 | Ticker ticker2; |
suntopbd | 12:0777ec4114c8 | 243 | ticker2.attach(&navigationAdjust, .5); |
suntopbd | 12:0777ec4114c8 | 244 | |
suntopbd | 12:0777ec4114c8 | 245 | |
suntopbd | 12:0777ec4114c8 | 246 | //Reference angle input from 0.0 to 3.3V |
suntopbd | 12:0777ec4114c8 | 247 | controllerL.setInputLimits(0.0, 360.0); |
suntopbd | 12:0777ec4114c8 | 248 | controllerR.setInputLimits(0.0, 360.0); |
suntopbd | 12:0777ec4114c8 | 249 | |
suntopbd | 12:0777ec4114c8 | 250 | //Pwm output from 0.0 to 1.0 |
suntopbd | 12:0777ec4114c8 | 251 | controllerL.setOutputLimits(0.0, 1.0); |
suntopbd | 12:0777ec4114c8 | 252 | controllerR.setOutputLimits(0.0, 1.0); |
suntopbd | 12:0777ec4114c8 | 253 | |
suntopbd | 12:0777ec4114c8 | 254 | //If there's a bias. |
suntopbd | 12:0777ec4114c8 | 255 | controllerL.setBias(0.0); |
suntopbd | 12:0777ec4114c8 | 256 | controllerR.setBias(0.0); |
suntopbd | 12:0777ec4114c8 | 257 | |
suntopbd | 12:0777ec4114c8 | 258 | controllerL.setMode(AUTO_MODE); |
suntopbd | 12:0777ec4114c8 | 259 | controllerR.setMode(AUTO_MODE); |
suntopbd | 12:0777ec4114c8 | 260 | |
suntopbd | 12:0777ec4114c8 | 261 | //process variable or heading to be 90 |
suntopbd | 12:0777ec4114c8 | 262 | controllerL.setSetPoint(90.0); |
suntopbd | 12:0777ec4114c8 | 263 | controllerR.setSetPoint(90.0); |
suntopbd | 12:0777ec4114c8 | 264 | |
suntopbd | 12:0777ec4114c8 | 265 | |
rgrover1 | 0:2130d7f559dc | 266 | |
suntopbd | 11:6916c05fde52 | 267 | while (true) { |
rgrover1 | 0:2130d7f559dc | 268 | ble.waitForEvent(); |
suntopbd | 12:0777ec4114c8 | 269 | heading_ref = mag.getHeading(); |
suntopbd | 12:0777ec4114c8 | 270 | if (heading_ref<0.0) heading_ref = heading_ref+360.00; |
suntopbd | 12:0777ec4114c8 | 271 | if (heading_ref>360.00) heading_ref = heading_ref -360.00; |
suntopbd | 12:0777ec4114c8 | 272 | controllerL.setProcessValue(heading_ref); |
suntopbd | 12:0777ec4114c8 | 273 | controllerR.setProcessValue(heading_ref); |
suntopbd | 12:0777ec4114c8 | 274 | float LPwr = controllerL.compute(); |
suntopbd | 12:0777ec4114c8 | 275 | float RPwr = controllerR.compute(); |
suntopbd | 12:0777ec4114c8 | 276 | ////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 277 | ////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 278 | //// WORKS BETTER THAN PID ////// |
suntopbd | 12:0777ec4114c8 | 279 | ////////////////////////////////// |
suntopbd | 12:0777ec4114c8 | 280 | ndr: |
suntopbd | 12:0777ec4114c8 | 281 | |
suntopbd | 12:0777ec4114c8 | 282 | if(refupdate == 1) |
suntopbd | 12:0777ec4114c8 | 283 | { |
suntopbd | 12:0777ec4114c8 | 284 | while(heading_ref != heading) |
suntopbd | 12:0777ec4114c8 | 285 | { |
suntopbd | 12:0777ec4114c8 | 286 | heading = mag.getHeading(); |
suntopbd | 12:0777ec4114c8 | 287 | if (heading<0.0) heading_ref = heading+360.00; |
suntopbd | 12:0777ec4114c8 | 288 | if (heading>360.00) heading_ref = heading -360.00; |
suntopbd | 12:0777ec4114c8 | 289 | LP.write(.9); |
suntopbd | 12:0777ec4114c8 | 290 | } |
suntopbd | 12:0777ec4114c8 | 291 | } |
suntopbd | 12:0777ec4114c8 | 292 | |
suntopbd | 12:0777ec4114c8 | 293 | |
suntopbd | 12:0777ec4114c8 | 294 | heading = heading_ref; |
suntopbd | 12:0777ec4114c8 | 295 | |
suntopbd | 12:0777ec4114c8 | 296 | for (int z =0;z<10;z++) |
suntopbd | 12:0777ec4114c8 | 297 | { |
suntopbd | 12:0777ec4114c8 | 298 | if (heading> heading_ref+2.0) |
suntopbd | 12:0777ec4114c8 | 299 | LP.write((heading - 90.0)/20.0); |
suntopbd | 12:0777ec4114c8 | 300 | else |
suntopbd | 12:0777ec4114c8 | 301 | LP.write(.0); |
suntopbd | 12:0777ec4114c8 | 302 | wait(0.2); |
suntopbd | 12:0777ec4114c8 | 303 | |
suntopbd | 12:0777ec4114c8 | 304 | if (heading> heading_ref+2.0) |
suntopbd | 12:0777ec4114c8 | 305 | RP.write(( 90 - heading )/20.0); |
suntopbd | 12:0777ec4114c8 | 306 | else |
suntopbd | 12:0777ec4114c8 | 307 | RP.write(.0); |
suntopbd | 12:0777ec4114c8 | 308 | wait(0.2); |
suntopbd | 12:0777ec4114c8 | 309 | LP.write(.7); |
suntopbd | 12:0777ec4114c8 | 310 | RP.write(.7); |
suntopbd | 12:0777ec4114c8 | 311 | wait(0.2); |
suntopbd | 12:0777ec4114c8 | 312 | |
suntopbd | 12:0777ec4114c8 | 313 | |
suntopbd | 12:0777ec4114c8 | 314 | } |
suntopbd | 12:0777ec4114c8 | 315 | /// heading is corrected after this line /// |
suntopbd | 12:0777ec4114c8 | 316 | /// calib may needed // |
suntopbd | 12:0777ec4114c8 | 317 | |
suntopbd | 12:0777ec4114c8 | 318 | // val = ain.read_u16(); |
suntopbd | 11:6916c05fde52 | 319 | // 22k+100K VOLTAGE DIVIDER AND 0.15 IS OFFSET |
suntopbd | 12:0777ec4114c8 | 320 | // batt = (122/22)*3.6*val/1023.0 - 0.15 |
suntopbd | 12:0777ec4114c8 | 321 | |
suntopbd | 12:0777ec4114c8 | 322 | //Update the process variable. |
suntopbd | 12:0777ec4114c8 | 323 | //Set the new output. |
suntopbd | 12:0777ec4114c8 | 324 | |
suntopbd | 12:0777ec4114c8 | 325 | |
suntopbd | 12:0777ec4114c8 | 326 | if (blepush ==1) |
suntopbd | 12:0777ec4114c8 | 327 | { |
suntopbd | 12:0777ec4114c8 | 328 | |
suntopbd | 12:0777ec4114c8 | 329 | TEMP = 3.3*100*temperature.read_u16()/(5*1023); // 10 mV= 1 deg c, vcc =3.3 v, Opamp gain 5 |
suntopbd | 12:0777ec4114c8 | 330 | dVal = TEMP; |
suntopbd | 12:0777ec4114c8 | 331 | dec = (int)(TEMP * 100) % 100; |
suntopbd | 10:053397a8dc40 | 332 | |
suntopbd | 11:6916c05fde52 | 333 | memset(result, 0, 100); |
suntopbd | 12:0777ec4114c8 | 334 | |
suntopbd | 12:0777ec4114c8 | 335 | result[0] = (dVal / 100) + '0'; |
suntopbd | 12:0777ec4114c8 | 336 | result[1] = ((dVal / 10)%10) + '0'; |
suntopbd | 12:0777ec4114c8 | 337 | result[2] = (dVal % 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 338 | result[3] = '.'; |
suntopbd | 12:0777ec4114c8 | 339 | result[4] = (dec / 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 340 | result[5] = (dec % 10) + '0'; |
suntopbd | 11:6916c05fde52 | 341 | for (i=strlen(result)-1; i>=0; i--) |
suntopbd | 11:6916c05fde52 | 342 | putc(result[i], stdout); |
suntopbd | 12:0777ec4114c8 | 343 | |
suntopbd | 12:0777ec4114c8 | 344 | blePrintf("temperature "); |
suntopbd | 12:0777ec4114c8 | 345 | blePrintf(result); |
suntopbd | 12:0777ec4114c8 | 346 | blePrintf("\n"); |
suntopbd | 12:0777ec4114c8 | 347 | |
suntopbd | 12:0777ec4114c8 | 348 | TURB = 3.3*10*turbidity.read_u16()/1023; // fix gain later |
suntopbd | 12:0777ec4114c8 | 349 | dVal = TURB; |
suntopbd | 12:0777ec4114c8 | 350 | dec = (int)(TURB * 100) % 100; |
suntopbd | 12:0777ec4114c8 | 351 | |
suntopbd | 12:0777ec4114c8 | 352 | memset(result, 0, 100); |
suntopbd | 12:0777ec4114c8 | 353 | |
suntopbd | 12:0777ec4114c8 | 354 | result[0] = (dVal / 100) + '0'; |
suntopbd | 12:0777ec4114c8 | 355 | result[1] = ((dVal / 10)%10) + '0'; |
suntopbd | 12:0777ec4114c8 | 356 | result[2] = (dVal % 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 357 | result[3] = '.'; |
suntopbd | 12:0777ec4114c8 | 358 | result[4] = (dec / 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 359 | result[5] = (dec % 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 360 | for (i=strlen(result)-1; i>=0; i--) |
suntopbd | 12:0777ec4114c8 | 361 | putc(result[i], stdout); |
suntopbd | 12:0777ec4114c8 | 362 | |
suntopbd | 12:0777ec4114c8 | 363 | blePrintf("turbidity "); |
suntopbd | 12:0777ec4114c8 | 364 | blePrintf(result); |
suntopbd | 12:0777ec4114c8 | 365 | blePrintf("\n"); |
suntopbd | 11:6916c05fde52 | 366 | |
suntopbd | 11:6916c05fde52 | 367 | |
suntopbd | 11:6916c05fde52 | 368 | |
suntopbd | 12:0777ec4114c8 | 369 | COND = 3.3*10*conductivity.read_u16()/1023; // fix gain later |
suntopbd | 12:0777ec4114c8 | 370 | dVal = COND; |
suntopbd | 12:0777ec4114c8 | 371 | dec = (int)(COND * 100) % 100; |
suntopbd | 12:0777ec4114c8 | 372 | |
suntopbd | 12:0777ec4114c8 | 373 | memset(result, 0, 100); |
suntopbd | 12:0777ec4114c8 | 374 | |
suntopbd | 12:0777ec4114c8 | 375 | result[0] = (dVal / 100) + '0'; |
suntopbd | 12:0777ec4114c8 | 376 | result[1] = ((dVal / 10)%10) + '0'; |
suntopbd | 12:0777ec4114c8 | 377 | result[2] = (dVal % 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 378 | result[3] = '.'; |
suntopbd | 12:0777ec4114c8 | 379 | result[4] = (dec / 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 380 | result[5] = (dec % 10) + '0'; |
suntopbd | 12:0777ec4114c8 | 381 | for (i=strlen(result)-1; i>=0; i--) |
suntopbd | 12:0777ec4114c8 | 382 | putc(result[i], stdout); |
suntopbd | 12:0777ec4114c8 | 383 | |
suntopbd | 12:0777ec4114c8 | 384 | blePrintf("conductivity "); |
suntopbd | 12:0777ec4114c8 | 385 | blePrintf(result); |
suntopbd | 12:0777ec4114c8 | 386 | blePrintf("\n"); |
suntopbd | 12:0777ec4114c8 | 387 | blepush =0; |
suntopbd | 12:0777ec4114c8 | 388 | } |
suntopbd | 12:0777ec4114c8 | 389 | goto ndr; |
suntopbd | 12:0777ec4114c8 | 390 | } |
suntopbd | 12:0777ec4114c8 | 391 | // end of while(1) |
suntopbd | 12:0777ec4114c8 | 392 | } |
suntopbd | 12:0777ec4114c8 | 393 | // end of main() |
suntopbd | 12:0777ec4114c8 | 394 | |
suntopbd | 12:0777ec4114c8 | 395 | |
suntopbd | 12:0777ec4114c8 | 396 |