Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Revision 49:c8f8946129b6, committed 2018-05-01
- Comitter:
- tkstreet
- Date:
- Tue May 01 21:31:15 2018 +0000
- Parent:
- 48:427bdb7bf31b
- Commit message:
- Modified for Rev.E. compatibility.
Changed in this revision
diff -r 427bdb7bf31b -r c8f8946129b6 akmanalogsensor.cpp --- a/akmanalogsensor.cpp Tue Dec 19 12:18:13 2017 -0800 +++ b/akmanalogsensor.cpp Tue May 01 21:31:15 2018 +0000 @@ -1,29 +1,25 @@ #include "akmanalogsensor.h" +#define SCALING_CONSTANT_3V 0.5997202865 +#define SCALING_CONSTANT_5V 0.3603082852 #define WAIT_ADC_MS 1 -/** - * Constructor. - * - */ AkmAnalogSensor::AkmAnalogSensor() : AkmSensor(){ ain = NULL; - mcp3428 = NULL; } -/** - * Destructor. - * - */ + AkmAnalogSensor::~AkmAnalogSensor(){ if(ain) delete ain; - if(mcp3428) delete mcp3428; } -AkmSensor::Status AkmAnalogSensor::init(const uint8_t id, const uint8_t subid){ + +AkmSensor::Status AkmAnalogSensor::init(const uint8_t p_id, const uint8_t s_id){ + VERBOSE("#AkmAnalogSensor init()\r\n"); - primaryId = id; - subId = subid; + + primaryId = p_id; + subId = s_id; if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ430L) sensorName = "EQ-430L"; else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ431L) sensorName = "EQ-431L"; @@ -53,24 +49,17 @@ else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_EM3242) sensorName = "EM3242"; else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_AK9710) sensorName = "AK9710"; else return AkmSensor::ERROR; - - MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default + if( ((primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId <= AKM_PRIMARY_ID_CURRENT_SENSOR_3V)) || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V){ - channel = MCP342X::ADC_CH4; // 0-3.0V out + + ain = new AnalogIn(ANALOG_IN_A0); + VERBOSE("#ADC Connected to 3V Channel\r\n"); } - - // 16bit ADC - I2C* i2c = new I2C(I2C_SDA,I2C_SCL); - i2c->frequency(I2C_SPEED); - VERBOSE("#AkmAnalogSensor::init: I2C created at %d Hz\r\n", I2C_SPEED); - - // ADC setting - mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); - mcp3428->setChannel(channel); - mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); - mcp3428->setConversionMode(MCP342X::CONTINUOUS); - VERBOSE("#AkmAnalogSensor::init: MPC342X ADC created\r\n"); + else { + ain = new AnalogIn(ANALOG_IN_A0);; + VERBOSE("#ADC Connected to 5V Channel\r\n"); + } interval = SENSOR_SAMPLING_RATE; // 10Hz @@ -97,27 +86,49 @@ } AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ + + uint16_t ain16 = 0; + int16_t ain16_sign = 0; + double ain_norm = 0; + double ain_scl = 0; + VERBOSE("#AkmAnalogSensor: Reading sensor data\r\n"); AkmSensor::clearEvent(); msg->setCommand(Message::CMD_START_MEASUREMENT); - if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || - primaryId == AKM_PRIMARY_ID_MISC_ANALOG || - primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V || - primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V ){ - MCP342X::Data data; - do { - VERBOSE("#Waiting..\r\n"); - mcp3428->getData(&data); - wait_ms(WAIT_ADC_MS); - } while(data.st == MCP342X::DATA_NOT_UPDATED); + if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || primaryId == AKM_PRIMARY_ID_MISC_ANALOG || + primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V ) + { + // Sample the ADC + ain_norm = ain->read(); + ain_norm *= 1.97; + + if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V){ + ain_scl = ain_norm * 1.8 / SCALING_CONSTANT_3V; + VERBOSE("#3V scale = %.4f\r\n", ain_scl); + } + if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V){ + ain_scl = ain_norm * 1.8 / SCALING_CONSTANT_5V; + VERBOSE("#5V scale = %.4f\r\n", ain_scl); + } + + ain16 = (uint16_t)(65535 * ain_norm); - msg->setArgument( 0, (char)( (data.value&0xFF00) >> 8) ); - msg->setArgument( 1, (char)( data.value & 0x00FF) ); - }else{ - msg->setArgument( 0, 0); - msg->setArgument( 1, 0); + //ain16_sign = (int16_t)ain16; + //ain16_sign = (int16_t)ain16 - 32768; + ain16_sign = (int16_t)ain_norm*48011.0 - 32768; + + VERBOSE("#ain_norm = %.4f\r\n", ain_norm); + VERBOSE("#ain16 = %d\r\n", ain16); + VERBOSE("#ain16_sign = %d\r\n", ain16_sign); + + msg->setArgument(0, (char)((ain16_sign & 0xFF00) >> 8)); + msg->setArgument(1, (char)(ain16_sign & 0x00FF)); + } + else{ + msg->setArgument(0, 0); + msg->setArgument(1, 0); MSG("readSensorData Error\r\n"); return AkmSensor::ERROR; }
diff -r 427bdb7bf31b -r c8f8946129b6 akmanalogsensor.h --- a/akmanalogsensor.h Tue Dec 19 12:18:13 2017 -0800 +++ b/akmanalogsensor.h Tue May 01 21:31:15 2018 +0000 @@ -3,7 +3,7 @@ #include "mbed.h" #include "akmsensor.h" -#include "mcp342x.h" +//#include "mcp342x.h" /** * Collection class for handling commands to all AKM Analog Sensor modules. @@ -96,7 +96,7 @@ * * @return Termination status type for debugging purposes. */ - virtual AkmSensor::Status init(const uint8_t id, const uint8_t subid); + virtual AkmSensor::Status init(const uint8_t p_id, const uint8_t s_id); /** * Process abstraction for starting sensor operation. @@ -142,7 +142,7 @@ private: Ticker ticker; AnalogIn* ain; - MCP342X* mcp3428; + AnalogIn* ain2; float interval; };
diff -r 427bdb7bf31b -r c8f8946129b6 akmsensor.h --- a/akmsensor.h Tue Dec 19 12:18:13 2017 -0800 +++ b/akmsensor.h Tue May 01 21:31:15 2018 +0000 @@ -8,6 +8,7 @@ // Sampling rate for ADC #define SENSOR_SAMPLING_RATE 0.1 // 10Hz +//#define SENSOR_SAMPLING_RATE 0.025 // 40Hz /** * Abstract base class for general AKM sensors. @@ -89,7 +90,7 @@ /** * Primary process for interfacing a sensor with the AKDP. When implemented - * in sensor class, it will transfer commands between the the sensor control + * in sensor class, it will transfer commands between the sensor control * class and AkmSensorManager. * * @param in Command message to be processed by sensor.
diff -r 427bdb7bf31b -r c8f8946129b6 akmsensormanager.cpp --- a/akmsensormanager.cpp Tue Dec 19 12:18:13 2017 -0800 +++ b/akmsensormanager.cpp Tue May 01 21:31:15 2018 +0000 @@ -222,7 +222,9 @@ // } } -void AkmSensorManager::dummyCallbackForCommandReceived(){} +void AkmSensorManager::commandReceivedCallback(){ + VERBOSE("#commandReceivedCallback: Command Received\r\n"); +} // For commands received via BLE @@ -238,11 +240,11 @@ eventCommandReceived = true; // Set flag VERBOSE("#Parsed message. %s\r\n", buf); } - t.attach(callback(this, &AkmSensorManager::dummyCallbackForCommandReceived),0); // wake-up from ble.waitForEvent + t.attach(callback(this, &AkmSensorManager::commandReceivedCallback),0); // wake-up from ble.waitForEvent return status; } - +/* int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { const int WAIT_ADC_MS = 1; @@ -260,37 +262,39 @@ return data.value; } - +*/ uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) { - MSG("#GetID\r\n"); - - I2C i2c(I2C_SDA, I2C_SCL); // establish I2C to read ID - - // ADC - MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); // ADC to convert voltage - mcp342x.setConversionMode(MCP342X::ONE_SHOT); // Set to single sample - MCP342X::AdcChannel ch; + VERBOSE("#AkmSensorManager: GetID\r\n"); - if (pin == ANALOG_SENSOR_ID) { // Primary ID - ch = MCP342X::ADC_CH1; - } else { // pin == ANALOG_SENSOR_ID_SUB - ch = MCP342X::ADC_CH2; // Secondary ID - } - - int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); - MSG("#12bit ADC Val = %d.\r\n", val); + AnalogIn ain(pin); + double value = 0; + uint8_t ID = 0; + + value = ain.read(); + + MSG("#ADC ID Raw Value: %.5f\r\n", value); + + // Scale reading + // Range of reading is 0.0 to 1.0 + value = 2.0 * (3.0 * value); +// value = (1/0.5555556) * (3.0 * value); // multiply by 1/(voltage divider) and 3.0V + + MSG("#ADC ID Reading: %.4f\r\n", value); - // Voltage boundaries for ID voltage divider system - const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V - const int16_t VAL_MIN = -2048; // Corresponds to 0V - - // Convert voltage to ID value - uint8_t value = (uint8_t)((val - VAL_MIN)/(double)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5); - MSG("#ID = %d.\r\n", value); - - return value; + if(bits == 4){ + // Convert reading into a 4-bit ID (i.e. 0-15) + ID = (uint8_t)((value/3.0)*16 + 0.5); + MSG("#4-bit ID = %d.\r\n", ID); + } + else if(bits == 5){ + // Convert reading into a 5-bit ID (i.e. 0-15) + ID = (uint8_t)((value/3.0)*32 + 0.5); + MSG("#5-bit ID = %d.\r\n", ID); + } + + return ID; } @@ -580,7 +584,7 @@ return SUCCESS; } -char* AkmSensorManager::my_strcat(char* str1, char* str2) +char* AkmSensorManager::stringConcat(char* str1, char* str2) { int num1; char* str; @@ -594,8 +598,8 @@ char* AkmSensorManager::getSensorName(){ char* name = ""; // for(int i=0; i<sensorNum; i++){ - name = my_strcat(name, (char *)sensor[0]->getSensorName()); -// if( sensorNum > (i+1) )name = my_strcat(name, "+"); + name = stringConcat(name, (char *)sensor[0]->getSensorName()); +// if( sensorNum > (i+1) )name = stringConcat(name, "+"); // } MSG("#Sensor Name='%s'.\r\n",name); return name;
diff -r 427bdb7bf31b -r c8f8946129b6 akmsensormanager.h --- a/akmsensormanager.h Tue Dec 19 12:18:13 2017 -0800 +++ b/akmsensormanager.h Tue May 01 21:31:15 2018 +0000 @@ -8,7 +8,6 @@ #include "Message.h" #include "ble/BLE.h" #include "SerialNano.h" -#include "mcp342x.h" #include "akmakd.h" #include "ble/services/UARTService.h" #include "akmhallswitch.h" @@ -124,7 +123,11 @@ * @param bits Number of bits of precision of value. * @return Returns the integer ID or SubID value. */ - uint8_t getId(PinName pin, uint8_t bits); + static uint8_t getId(PinName pin, uint8_t bits); + + /** + * Detect if data is ready to be read. + */ void detectDRDY(); private: @@ -153,9 +156,9 @@ bool checkAkmSensor(); void dataOut(char* str); - int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s); - void dummyCallbackForCommandReceived(); - char* my_strcat(char* str1, char* str2); +// int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s); + void commandReceivedCallback(); + char* stringConcat(char* str1, char* str2); void attachInterrupt(); void detachInterrupt(); };
diff -r 427bdb7bf31b -r c8f8946129b6 ap1017ctrl.cpp --- a/ap1017ctrl.cpp Tue Dec 19 12:18:13 2017 -0800 +++ b/ap1017ctrl.cpp Tue May 01 21:31:15 2018 +0000 @@ -26,12 +26,11 @@ inputA = new DigitalOut(DIGITAL_D0); inputB = new DigitalOut(DIGITAL_D1); - i2cMotor = new I2C(I2C_SDA0, I2C_SCL0); - i2cMotor->frequency(I2C_SPEED); + enable = new DigitalOut(DIGITAL_D9); switch (subId) { // template for multiple drivers case SUB_ID_AP1017: - ap1017 = new AP1017(inputA, inputB, i2cMotor); // Instantiate AP1017 + ap1017 = new AP1017(inputA, inputB, enable); // Instantiate AP1017 sensorName = "AP1017"; MSG("#AP1017 found.\r\n"); break;
diff -r 427bdb7bf31b -r c8f8946129b6 ap1017ctrl.h --- a/ap1017ctrl.h Tue Dec 19 12:18:13 2017 -0800 +++ b/ap1017ctrl.h Tue May 01 21:31:15 2018 +0000 @@ -91,14 +91,13 @@ void pwmOnPulse(); private: - //bool event; uint8_t primaryId; uint8_t subId; const char* sensorName; DigitalOut* inputA; DigitalOut* inputB; - I2C* i2cMotor; + DigitalOut* enable; AP1017* ap1017; Ticker pwm;