Code to run the controller for the wifi robot

Dependencies:   ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed

Committer:
anewton8
Date:
Tue Oct 20 17:35:45 2015 +0000
Revision:
0:e68e51f935d1
Finished controller code. Tuesday October 20th, 2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
anewton8 0:e68e51f935d1 1 /*
anewton8 0:e68e51f935d1 2 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
anewton8 0:e68e51f935d1 3
anewton8 0:e68e51f935d1 4 Permission is hereby granted, free of charge, to any person obtaining a copy
anewton8 0:e68e51f935d1 5 of this software and associated documentation files (the "Software"), to deal
anewton8 0:e68e51f935d1 6 in the Software without restriction, including without limitation the rights
anewton8 0:e68e51f935d1 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
anewton8 0:e68e51f935d1 8 copies of the Software, and to permit persons to whom the Software is
anewton8 0:e68e51f935d1 9 furnished to do so, subject to the following conditions:
anewton8 0:e68e51f935d1 10
anewton8 0:e68e51f935d1 11 The above copyright notice and this permission notice shall be included in
anewton8 0:e68e51f935d1 12 all copies or substantial portions of the Software.
anewton8 0:e68e51f935d1 13
anewton8 0:e68e51f935d1 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
anewton8 0:e68e51f935d1 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
anewton8 0:e68e51f935d1 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
anewton8 0:e68e51f935d1 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
anewton8 0:e68e51f935d1 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
anewton8 0:e68e51f935d1 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
anewton8 0:e68e51f935d1 20 THE SOFTWARE.
anewton8 0:e68e51f935d1 21 */
anewton8 0:e68e51f935d1 22
anewton8 0:e68e51f935d1 23 #include <mbed.h>
anewton8 0:e68e51f935d1 24 #include <sstream>
anewton8 0:e68e51f935d1 25 #include <string>
anewton8 0:e68e51f935d1 26 #include <list>
anewton8 0:e68e51f935d1 27
anewton8 0:e68e51f935d1 28 #include <mpr121.h>
anewton8 0:e68e51f935d1 29
anewton8 0:e68e51f935d1 30 Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
anewton8 0:e68e51f935d1 31 {
anewton8 0:e68e51f935d1 32 this->i2c = i2c;
anewton8 0:e68e51f935d1 33
anewton8 0:e68e51f935d1 34 address = i2cAddress;
anewton8 0:e68e51f935d1 35
anewton8 0:e68e51f935d1 36 // Configure the MPR121 settings to default
anewton8 0:e68e51f935d1 37 this->configureSettings();
anewton8 0:e68e51f935d1 38 }
anewton8 0:e68e51f935d1 39
anewton8 0:e68e51f935d1 40
anewton8 0:e68e51f935d1 41 void Mpr121::configureSettings()
anewton8 0:e68e51f935d1 42 {
anewton8 0:e68e51f935d1 43 // Put the MPR into setup mode
anewton8 0:e68e51f935d1 44 this->write(ELE_CFG,0x00);
anewton8 0:e68e51f935d1 45
anewton8 0:e68e51f935d1 46 // Electrode filters for when data is > baseline
anewton8 0:e68e51f935d1 47 unsigned char gtBaseline[] = {
anewton8 0:e68e51f935d1 48 0x01, //MHD_R
anewton8 0:e68e51f935d1 49 0x01, //NHD_R
anewton8 0:e68e51f935d1 50 0x00, //NCL_R
anewton8 0:e68e51f935d1 51 0x00 //FDL_R
anewton8 0:e68e51f935d1 52 };
anewton8 0:e68e51f935d1 53
anewton8 0:e68e51f935d1 54 writeMany(MHD_R,gtBaseline,4);
anewton8 0:e68e51f935d1 55
anewton8 0:e68e51f935d1 56 // Electrode filters for when data is < baseline
anewton8 0:e68e51f935d1 57 unsigned char ltBaseline[] = {
anewton8 0:e68e51f935d1 58 0x01, //MHD_F
anewton8 0:e68e51f935d1 59 0x01, //NHD_F
anewton8 0:e68e51f935d1 60 0xFF, //NCL_F
anewton8 0:e68e51f935d1 61 0x02 //FDL_F
anewton8 0:e68e51f935d1 62 };
anewton8 0:e68e51f935d1 63
anewton8 0:e68e51f935d1 64 writeMany(MHD_F,ltBaseline,4);
anewton8 0:e68e51f935d1 65
anewton8 0:e68e51f935d1 66 // Electrode touch and release thresholds
anewton8 0:e68e51f935d1 67 unsigned char electrodeThresholds[] = {
anewton8 0:e68e51f935d1 68 E_THR_T, // Touch Threshhold
anewton8 0:e68e51f935d1 69 E_THR_R // Release Threshold
anewton8 0:e68e51f935d1 70 };
anewton8 0:e68e51f935d1 71
anewton8 0:e68e51f935d1 72 for(int i=0; i<12; i++){
anewton8 0:e68e51f935d1 73 int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
anewton8 0:e68e51f935d1 74 }
anewton8 0:e68e51f935d1 75
anewton8 0:e68e51f935d1 76 // Proximity Settings
anewton8 0:e68e51f935d1 77 unsigned char proximitySettings[] = {
anewton8 0:e68e51f935d1 78 0xff, //MHD_Prox_R
anewton8 0:e68e51f935d1 79 0xff, //NHD_Prox_R
anewton8 0:e68e51f935d1 80 0x00, //NCL_Prox_R
anewton8 0:e68e51f935d1 81 0x00, //FDL_Prox_R
anewton8 0:e68e51f935d1 82 0x01, //MHD_Prox_F
anewton8 0:e68e51f935d1 83 0x01, //NHD_Prox_F
anewton8 0:e68e51f935d1 84 0xFF, //NCL_Prox_F
anewton8 0:e68e51f935d1 85 0xff, //FDL_Prox_F
anewton8 0:e68e51f935d1 86 0x00, //NHD_Prox_T
anewton8 0:e68e51f935d1 87 0x00, //NCL_Prox_T
anewton8 0:e68e51f935d1 88 0x00 //NFD_Prox_T
anewton8 0:e68e51f935d1 89 };
anewton8 0:e68e51f935d1 90 writeMany(MHDPROXR,proximitySettings,11);
anewton8 0:e68e51f935d1 91
anewton8 0:e68e51f935d1 92 unsigned char proxThresh[] = {
anewton8 0:e68e51f935d1 93 PROX_THR_T, // Touch Threshold
anewton8 0:e68e51f935d1 94 PROX_THR_R // Release Threshold
anewton8 0:e68e51f935d1 95 };
anewton8 0:e68e51f935d1 96 writeMany(EPROXTTH,proxThresh,2);
anewton8 0:e68e51f935d1 97
anewton8 0:e68e51f935d1 98 this->write(FIL_CFG,0x04);
anewton8 0:e68e51f935d1 99
anewton8 0:e68e51f935d1 100 // Set the electrode config to transition to active mode
anewton8 0:e68e51f935d1 101 this->write(ELE_CFG,0x0c);
anewton8 0:e68e51f935d1 102 }
anewton8 0:e68e51f935d1 103
anewton8 0:e68e51f935d1 104 void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
anewton8 0:e68e51f935d1 105
anewton8 0:e68e51f935d1 106 if(electrode > 11) return;
anewton8 0:e68e51f935d1 107
anewton8 0:e68e51f935d1 108 // Get the current mode
anewton8 0:e68e51f935d1 109 unsigned char mode = this->read(ELE_CFG);
anewton8 0:e68e51f935d1 110
anewton8 0:e68e51f935d1 111 // Put the MPR into setup mode
anewton8 0:e68e51f935d1 112 this->write(ELE_CFG,0x00);
anewton8 0:e68e51f935d1 113
anewton8 0:e68e51f935d1 114 // Write the new threshold
anewton8 0:e68e51f935d1 115 this->write((ELE0_T+(electrode*2)), touch);
anewton8 0:e68e51f935d1 116 this->write((ELE0_T+(electrode*2)+1), release);
anewton8 0:e68e51f935d1 117
anewton8 0:e68e51f935d1 118 //Restore the operating mode
anewton8 0:e68e51f935d1 119 this->write(ELE_CFG, mode);
anewton8 0:e68e51f935d1 120 }
anewton8 0:e68e51f935d1 121
anewton8 0:e68e51f935d1 122
anewton8 0:e68e51f935d1 123 unsigned char Mpr121::read(int key){
anewton8 0:e68e51f935d1 124
anewton8 0:e68e51f935d1 125 unsigned char data[2];
anewton8 0:e68e51f935d1 126
anewton8 0:e68e51f935d1 127 //Start the command
anewton8 0:e68e51f935d1 128 i2c->start();
anewton8 0:e68e51f935d1 129
anewton8 0:e68e51f935d1 130 // Address the target (Write mode)
anewton8 0:e68e51f935d1 131 int ack1= i2c->write(address);
anewton8 0:e68e51f935d1 132
anewton8 0:e68e51f935d1 133 // Set the register key to read
anewton8 0:e68e51f935d1 134 int ack2 = i2c->write(key);
anewton8 0:e68e51f935d1 135
anewton8 0:e68e51f935d1 136 // Re-start for read of data
anewton8 0:e68e51f935d1 137 i2c->start();
anewton8 0:e68e51f935d1 138
anewton8 0:e68e51f935d1 139 // Re-send the target address in read mode
anewton8 0:e68e51f935d1 140 int ack3 = i2c->write(address+1);
anewton8 0:e68e51f935d1 141
anewton8 0:e68e51f935d1 142 // Read in the result
anewton8 0:e68e51f935d1 143 data[0] = i2c->read(0);
anewton8 0:e68e51f935d1 144
anewton8 0:e68e51f935d1 145 // Reset the bus
anewton8 0:e68e51f935d1 146 i2c->stop();
anewton8 0:e68e51f935d1 147
anewton8 0:e68e51f935d1 148 return data[0];
anewton8 0:e68e51f935d1 149 }
anewton8 0:e68e51f935d1 150
anewton8 0:e68e51f935d1 151
anewton8 0:e68e51f935d1 152 int Mpr121::write(int key, unsigned char value){
anewton8 0:e68e51f935d1 153
anewton8 0:e68e51f935d1 154 //Start the command
anewton8 0:e68e51f935d1 155 i2c->start();
anewton8 0:e68e51f935d1 156
anewton8 0:e68e51f935d1 157 // Address the target (Write mode)
anewton8 0:e68e51f935d1 158 int ack1= i2c->write(address);
anewton8 0:e68e51f935d1 159
anewton8 0:e68e51f935d1 160 // Set the register key to write
anewton8 0:e68e51f935d1 161 int ack2 = i2c->write(key);
anewton8 0:e68e51f935d1 162
anewton8 0:e68e51f935d1 163 // Read in the result
anewton8 0:e68e51f935d1 164 int ack3 = i2c->write(value);
anewton8 0:e68e51f935d1 165
anewton8 0:e68e51f935d1 166 // Reset the bus
anewton8 0:e68e51f935d1 167 i2c->stop();
anewton8 0:e68e51f935d1 168
anewton8 0:e68e51f935d1 169 return (ack1+ack2+ack3)-3;
anewton8 0:e68e51f935d1 170 }
anewton8 0:e68e51f935d1 171
anewton8 0:e68e51f935d1 172
anewton8 0:e68e51f935d1 173 int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
anewton8 0:e68e51f935d1 174 //Start the command
anewton8 0:e68e51f935d1 175 i2c->start();
anewton8 0:e68e51f935d1 176
anewton8 0:e68e51f935d1 177 // Address the target (Write mode)
anewton8 0:e68e51f935d1 178 int ack= i2c->write(address);
anewton8 0:e68e51f935d1 179 if(ack!=1){
anewton8 0:e68e51f935d1 180 return -1;
anewton8 0:e68e51f935d1 181 }
anewton8 0:e68e51f935d1 182
anewton8 0:e68e51f935d1 183 // Set the register key to write
anewton8 0:e68e51f935d1 184 ack = i2c->write(start);
anewton8 0:e68e51f935d1 185 if(ack!=1){
anewton8 0:e68e51f935d1 186 return -1;
anewton8 0:e68e51f935d1 187 }
anewton8 0:e68e51f935d1 188
anewton8 0:e68e51f935d1 189 // Write the date set
anewton8 0:e68e51f935d1 190 int count = 0;
anewton8 0:e68e51f935d1 191 while(ack==1 && (count < length)){
anewton8 0:e68e51f935d1 192 ack = i2c->write(dataSet[count]);
anewton8 0:e68e51f935d1 193 count++;
anewton8 0:e68e51f935d1 194 }
anewton8 0:e68e51f935d1 195 // Stop the cmd
anewton8 0:e68e51f935d1 196 i2c->stop();
anewton8 0:e68e51f935d1 197
anewton8 0:e68e51f935d1 198 return count;
anewton8 0:e68e51f935d1 199 }
anewton8 0:e68e51f935d1 200
anewton8 0:e68e51f935d1 201
anewton8 0:e68e51f935d1 202 bool Mpr121::getProximityMode(){
anewton8 0:e68e51f935d1 203 if(this->read(ELE_CFG) > 0x0c)
anewton8 0:e68e51f935d1 204 return true;
anewton8 0:e68e51f935d1 205 else
anewton8 0:e68e51f935d1 206 return false;
anewton8 0:e68e51f935d1 207 }
anewton8 0:e68e51f935d1 208
anewton8 0:e68e51f935d1 209 void Mpr121::setProximityMode(bool mode){
anewton8 0:e68e51f935d1 210 this->write(ELE_CFG,0x00);
anewton8 0:e68e51f935d1 211 if(mode){
anewton8 0:e68e51f935d1 212 this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
anewton8 0:e68e51f935d1 213 } else {
anewton8 0:e68e51f935d1 214 this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
anewton8 0:e68e51f935d1 215 }
anewton8 0:e68e51f935d1 216 }
anewton8 0:e68e51f935d1 217
anewton8 0:e68e51f935d1 218
anewton8 0:e68e51f935d1 219 int Mpr121::readTouchData(){
anewton8 0:e68e51f935d1 220 return this->read(0x00);
anewton8 0:e68e51f935d1 221 }