Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmanalogsensor.cpp@37:c76d2edf3426, 2017-05-24 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Wed May 24 20:23:22 2017 +0000
- Revision:
- 37:c76d2edf3426
- Parent:
- 29:b488d2c89fba
- Child:
- 40:42e48427e4b7
Added CQ-32xx, CZ-31xx current sensors.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 11:cef8dc1cf010 | 1 | #include "akmanalogsensor.h" |
masahikofukasawa | 11:cef8dc1cf010 | 2 | #include "debug.h" |
masahikofukasawa | 11:cef8dc1cf010 | 3 | |
masahikofukasawa | 11:cef8dc1cf010 | 4 | #define WAIT_ADC_MS 1 |
masahikofukasawa | 11:cef8dc1cf010 | 5 | |
masahikofukasawa | 11:cef8dc1cf010 | 6 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 7 | * Constructor. |
masahikofukasawa | 11:cef8dc1cf010 | 8 | * |
masahikofukasawa | 11:cef8dc1cf010 | 9 | */ |
masahikofukasawa | 27:41aa9fb23a2f | 10 | AkmAnalogSensor::AkmAnalogSensor() : AkmSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 11 | ain = NULL; |
masahikofukasawa | 11:cef8dc1cf010 | 12 | mcp3428 = NULL; |
masahikofukasawa | 11:cef8dc1cf010 | 13 | } |
masahikofukasawa | 11:cef8dc1cf010 | 14 | |
masahikofukasawa | 11:cef8dc1cf010 | 15 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 16 | * Destructor. |
masahikofukasawa | 11:cef8dc1cf010 | 17 | * |
masahikofukasawa | 11:cef8dc1cf010 | 18 | */ |
masahikofukasawa | 11:cef8dc1cf010 | 19 | AkmAnalogSensor::~AkmAnalogSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 20 | if(ain) delete ain; |
masahikofukasawa | 11:cef8dc1cf010 | 21 | if(mcp3428) delete mcp3428; |
masahikofukasawa | 11:cef8dc1cf010 | 22 | } |
masahikofukasawa | 11:cef8dc1cf010 | 23 | |
masahikofukasawa | 11:cef8dc1cf010 | 24 | AkmSensor::Status AkmAnalogSensor::init(const uint8_t id, const uint8_t subid){ |
masahikofukasawa | 11:cef8dc1cf010 | 25 | primaryId = id; |
masahikofukasawa | 11:cef8dc1cf010 | 26 | subId = subid; |
masahikofukasawa | 11:cef8dc1cf010 | 27 | |
masahikofukasawa | 37:c76d2edf3426 | 28 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ430L) sensorName = "EQ-430L"; |
masahikofukasawa | 37:c76d2edf3426 | 29 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ431L) sensorName = "EQ-431L"; |
masahikofukasawa | 37:c76d2edf3426 | 30 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ432L) sensorName = "EQ-432L"; |
masahikofukasawa | 37:c76d2edf3426 | 31 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ433L) sensorName = "EQ-433L"; |
masahikofukasawa | 37:c76d2edf3426 | 32 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3200) sensorName = "CQ-3200"; |
masahikofukasawa | 37:c76d2edf3426 | 33 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3201) sensorName = "CQ-3201"; |
masahikofukasawa | 37:c76d2edf3426 | 34 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3202) sensorName = "CQ-3202"; |
masahikofukasawa | 37:c76d2edf3426 | 35 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3203) sensorName = "CQ-3203"; |
masahikofukasawa | 37:c76d2edf3426 | 36 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3204) sensorName = "CQ-3204"; |
masahikofukasawa | 37:c76d2edf3426 | 37 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320A) sensorName = "CQ-320A"; |
masahikofukasawa | 37:c76d2edf3426 | 38 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320B) sensorName = "CQ-320B"; |
masahikofukasawa | 37:c76d2edf3426 | 39 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3300) sensorName = "CQ-3300"; |
masahikofukasawa | 37:c76d2edf3426 | 40 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3301) sensorName = "CQ-3301"; |
masahikofukasawa | 37:c76d2edf3426 | 41 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3302) sensorName = "CQ-3302"; |
masahikofukasawa | 37:c76d2edf3426 | 42 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3303) sensorName = "CQ-3303"; |
masahikofukasawa | 37:c76d2edf3426 | 43 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330A) sensorName = "CQ-330A"; |
masahikofukasawa | 37:c76d2edf3426 | 44 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330B) sensorName = "CQ-330B"; |
masahikofukasawa | 37:c76d2edf3426 | 45 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330E) sensorName = "CQ-330E"; |
masahikofukasawa | 37:c76d2edf3426 | 46 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330F) sensorName = "CQ-330F"; |
masahikofukasawa | 37:c76d2edf3426 | 47 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330G) sensorName = "CQ-330G"; |
masahikofukasawa | 37:c76d2edf3426 | 48 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330H) sensorName = "CQ-330H"; |
masahikofukasawa | 37:c76d2edf3426 | 49 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330J) sensorName = "CQ-330J"; |
masahikofukasawa | 37:c76d2edf3426 | 50 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3813) sensorName = "CZ-3813"; |
masahikofukasawa | 37:c76d2edf3426 | 51 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3814) sensorName = "CZ-3814"; |
masahikofukasawa | 37:c76d2edf3426 | 52 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3815) sensorName = "CZ-3815"; |
masahikofukasawa | 37:c76d2edf3426 | 53 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_EM3242) sensorName = "EM3242"; |
masahikofukasawa | 37:c76d2edf3426 | 54 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_AK9710) sensorName = "AK9710"; |
masahikofukasawa | 37:c76d2edf3426 | 55 | else return AkmSensor::ERROR; |
masahikofukasawa | 37:c76d2edf3426 | 56 | |
masahikofukasawa | 37:c76d2edf3426 | 57 | MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default |
masahikofukasawa | 37:c76d2edf3426 | 58 | if( (primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId < 0x08) || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V){ |
masahikofukasawa | 37:c76d2edf3426 | 59 | channel = MCP342X::ADC_CH4; // 0-3.0V out |
masahikofukasawa | 37:c76d2edf3426 | 60 | } |
masahikofukasawa | 13:d008249f0359 | 61 | |
masahikofukasawa | 37:c76d2edf3426 | 62 | // 16bit ADC |
masahikofukasawa | 37:c76d2edf3426 | 63 | I2C* i2c = new I2C(I2C_SDA,I2C_SCL); |
masahikofukasawa | 37:c76d2edf3426 | 64 | i2c->frequency(I2C_SPEED_100KHZ); |
masahikofukasawa | 37:c76d2edf3426 | 65 | |
masahikofukasawa | 37:c76d2edf3426 | 66 | // ADC setting |
masahikofukasawa | 37:c76d2edf3426 | 67 | mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); |
masahikofukasawa | 37:c76d2edf3426 | 68 | mcp3428->setChannel(channel); |
masahikofukasawa | 37:c76d2edf3426 | 69 | mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); |
masahikofukasawa | 37:c76d2edf3426 | 70 | mcp3428->setConversionMode(MCP342X::CONTINUOUS); |
masahikofukasawa | 11:cef8dc1cf010 | 71 | |
masahikofukasawa | 11:cef8dc1cf010 | 72 | interval = SENSOR_SAMPLING_RATE; // 10Hz |
masahikofukasawa | 11:cef8dc1cf010 | 73 | |
masahikofukasawa | 11:cef8dc1cf010 | 74 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 75 | } |
masahikofukasawa | 11:cef8dc1cf010 | 76 | |
masahikofukasawa | 11:cef8dc1cf010 | 77 | AkmSensor::Status AkmAnalogSensor::startSensor(){ |
masahikofukasawa | 29:b488d2c89fba | 78 | ticker.attach(callback(this, &AkmSensor::setEvent), interval); |
masahikofukasawa | 11:cef8dc1cf010 | 79 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 80 | } |
masahikofukasawa | 11:cef8dc1cf010 | 81 | |
masahikofukasawa | 11:cef8dc1cf010 | 82 | AkmSensor::Status AkmAnalogSensor::startSensor(const float sec){ |
masahikofukasawa | 11:cef8dc1cf010 | 83 | interval = sec; |
masahikofukasawa | 29:b488d2c89fba | 84 | ticker.attach(callback(this, &AkmSensor::setEvent), interval); |
masahikofukasawa | 29:b488d2c89fba | 85 | MSG("#Start sensor %s.\r\n",sensorName); |
masahikofukasawa | 11:cef8dc1cf010 | 86 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 87 | } |
masahikofukasawa | 11:cef8dc1cf010 | 88 | |
masahikofukasawa | 11:cef8dc1cf010 | 89 | AkmSensor::Status AkmAnalogSensor::stopSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 90 | ticker.detach(); |
masahikofukasawa | 11:cef8dc1cf010 | 91 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 92 | } |
masahikofukasawa | 11:cef8dc1cf010 | 93 | |
masahikofukasawa | 11:cef8dc1cf010 | 94 | AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ |
masahikofukasawa | 29:b488d2c89fba | 95 | AkmSensor::clearEvent(); |
masahikofukasawa | 11:cef8dc1cf010 | 96 | |
masahikofukasawa | 11:cef8dc1cf010 | 97 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 11:cef8dc1cf010 | 98 | |
masahikofukasawa | 27:41aa9fb23a2f | 99 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || |
masahikofukasawa | 11:cef8dc1cf010 | 100 | primaryId == AKM_PRIMARY_ID_MISC_ANALOG || |
masahikofukasawa | 37:c76d2edf3426 | 101 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V || |
masahikofukasawa | 37:c76d2edf3426 | 102 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V ){ |
masahikofukasawa | 11:cef8dc1cf010 | 103 | MCP342X::Data data; |
masahikofukasawa | 11:cef8dc1cf010 | 104 | do { |
masahikofukasawa | 11:cef8dc1cf010 | 105 | mcp3428->getData(&data); |
masahikofukasawa | 11:cef8dc1cf010 | 106 | wait_ms(WAIT_ADC_MS); |
masahikofukasawa | 11:cef8dc1cf010 | 107 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
masahikofukasawa | 11:cef8dc1cf010 | 108 | |
masahikofukasawa | 11:cef8dc1cf010 | 109 | msg->setArgument( 0, (char)( (data.value&0xFF00) >> 8) ); |
masahikofukasawa | 11:cef8dc1cf010 | 110 | msg->setArgument( 1, (char)( data.value & 0x00FF) ); |
masahikofukasawa | 11:cef8dc1cf010 | 111 | }else{ |
masahikofukasawa | 11:cef8dc1cf010 | 112 | msg->setArgument( 0, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 113 | msg->setArgument( 1, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 114 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 115 | } |
masahikofukasawa | 11:cef8dc1cf010 | 116 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 117 | } |
masahikofukasawa | 11:cef8dc1cf010 | 118 | |
masahikofukasawa | 11:cef8dc1cf010 | 119 | AkmSensor::Status AkmAnalogSensor::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 15:1238993fd75f | 120 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 121 | } |