Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

Committer:
masahikofukasawa
Date:
Fri Jul 22 22:54:11 2016 +0000
Revision:
11:cef8dc1cf010
Child:
13:d008249f0359
RevD7_004

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 11:cef8dc1cf010 1 #include "ak7451ctrl.h"
masahikofukasawa 11:cef8dc1cf010 2 #include "debug.h"
masahikofukasawa 11:cef8dc1cf010 3
masahikofukasawa 11:cef8dc1cf010 4 /**
masahikofukasawa 11:cef8dc1cf010 5 * Constructor.
masahikofukasawa 11:cef8dc1cf010 6 *
masahikofukasawa 11:cef8dc1cf010 7 */
masahikofukasawa 11:cef8dc1cf010 8 Ak7451Ctrl::Ak7451Ctrl(){
masahikofukasawa 11:cef8dc1cf010 9 ak7451 = NULL;
masahikofukasawa 11:cef8dc1cf010 10 event = false;
masahikofukasawa 11:cef8dc1cf010 11 }
masahikofukasawa 11:cef8dc1cf010 12
masahikofukasawa 11:cef8dc1cf010 13 /**
masahikofukasawa 11:cef8dc1cf010 14 * Destructor.
masahikofukasawa 11:cef8dc1cf010 15 *
masahikofukasawa 11:cef8dc1cf010 16 */
masahikofukasawa 11:cef8dc1cf010 17 Ak7451Ctrl::~Ak7451Ctrl(){
masahikofukasawa 11:cef8dc1cf010 18 if (ak7451) delete ak7451;
masahikofukasawa 11:cef8dc1cf010 19 }
masahikofukasawa 11:cef8dc1cf010 20
masahikofukasawa 11:cef8dc1cf010 21 AkmSensor::Status Ak7451Ctrl::init(const uint8_t id, const uint8_t subid){
masahikofukasawa 11:cef8dc1cf010 22 primaryId = id;
masahikofukasawa 11:cef8dc1cf010 23 subId = subid;
masahikofukasawa 11:cef8dc1cf010 24
masahikofukasawa 11:cef8dc1cf010 25 if(subId == SUB_ID_AK7451){
masahikofukasawa 11:cef8dc1cf010 26 SPI* mSpi;
masahikofukasawa 11:cef8dc1cf010 27 DigitalOut* mCs;
masahikofukasawa 11:cef8dc1cf010 28 AK7451::Status status = AK7451::ERROR;
masahikofukasawa 11:cef8dc1cf010 29 mSpi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK);
masahikofukasawa 11:cef8dc1cf010 30 mSpi->format(8,1); // 8bit, Mode=1
masahikofukasawa 11:cef8dc1cf010 31 mSpi->frequency(1000000);
masahikofukasawa 11:cef8dc1cf010 32 mCs = new DigitalOut(SPI_CS);
masahikofukasawa 11:cef8dc1cf010 33 ak7451 = new AK7451();
masahikofukasawa 11:cef8dc1cf010 34 ak7451->begin(mSpi, mCs);
masahikofukasawa 11:cef8dc1cf010 35
masahikofukasawa 11:cef8dc1cf010 36 status = ak7451->setOperationMode(AK7451::AK7451_USER_MODE);
masahikofukasawa 11:cef8dc1cf010 37 if( status != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 38 MSG("#AK7451 user mode failed.\r\n");
masahikofukasawa 11:cef8dc1cf010 39 return AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 40 }
masahikofukasawa 11:cef8dc1cf010 41 MSG("#AK7451 user mode succeed.\r\n");
masahikofukasawa 11:cef8dc1cf010 42
masahikofukasawa 11:cef8dc1cf010 43 char data[2] = {0x02,0x00}; // set clockwise rotation
masahikofukasawa 11:cef8dc1cf010 44 status = ak7451->writeEEPROM(0x07,data); // set clockwise
masahikofukasawa 11:cef8dc1cf010 45 if( status != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 46 MSG("#AK7451 write EEPROM failed.\r\n");
masahikofukasawa 11:cef8dc1cf010 47 return AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 48 }
masahikofukasawa 11:cef8dc1cf010 49 MSG("#AK7451 write EEPROM succeed.\r\n");
masahikofukasawa 11:cef8dc1cf010 50
masahikofukasawa 11:cef8dc1cf010 51 status = ak7451->setOperationMode(AK7451::AK7451_NORMAL_MODE);
masahikofukasawa 11:cef8dc1cf010 52 if( status != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 53 MSG("#AK7451 normal mode failed.\r\n");
masahikofukasawa 11:cef8dc1cf010 54 return AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 55 }
masahikofukasawa 11:cef8dc1cf010 56 MSG("#AK7451 normal mode succeed.\r\n");
masahikofukasawa 11:cef8dc1cf010 57
masahikofukasawa 11:cef8dc1cf010 58 interval = SENSOR_SAMPLING_RATE; // 10Hz
masahikofukasawa 11:cef8dc1cf010 59 }
masahikofukasawa 11:cef8dc1cf010 60 else{
masahikofukasawa 11:cef8dc1cf010 61 return AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 62 }
masahikofukasawa 11:cef8dc1cf010 63
masahikofukasawa 11:cef8dc1cf010 64 return AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 65 }
masahikofukasawa 11:cef8dc1cf010 66
masahikofukasawa 11:cef8dc1cf010 67 void Ak7451Ctrl::eventCallback(){
masahikofukasawa 11:cef8dc1cf010 68 event = true;
masahikofukasawa 11:cef8dc1cf010 69 }
masahikofukasawa 11:cef8dc1cf010 70
masahikofukasawa 11:cef8dc1cf010 71 bool Ak7451Ctrl::isEvent(){
masahikofukasawa 11:cef8dc1cf010 72 return event;
masahikofukasawa 11:cef8dc1cf010 73 }
masahikofukasawa 11:cef8dc1cf010 74
masahikofukasawa 11:cef8dc1cf010 75 AkmSensor::Status Ak7451Ctrl::startSensor(){
masahikofukasawa 11:cef8dc1cf010 76 ticker.attach(this, &Ak7451Ctrl::eventCallback, interval);
masahikofukasawa 11:cef8dc1cf010 77 return AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 78 }
masahikofukasawa 11:cef8dc1cf010 79
masahikofukasawa 11:cef8dc1cf010 80 AkmSensor::Status Ak7451Ctrl::startSensor(const float sec){
masahikofukasawa 11:cef8dc1cf010 81 interval = sec;
masahikofukasawa 11:cef8dc1cf010 82 ticker.attach(this, &Ak7451Ctrl::eventCallback, interval);
masahikofukasawa 11:cef8dc1cf010 83 return AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 84 }
masahikofukasawa 11:cef8dc1cf010 85
masahikofukasawa 11:cef8dc1cf010 86 AkmSensor::Status Ak7451Ctrl::stopSensor(){
masahikofukasawa 11:cef8dc1cf010 87 ticker.detach();
masahikofukasawa 11:cef8dc1cf010 88 event = false;
masahikofukasawa 11:cef8dc1cf010 89 return AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 90 }
masahikofukasawa 11:cef8dc1cf010 91
masahikofukasawa 11:cef8dc1cf010 92 AkmSensor::Status Ak7451Ctrl::readSensorData(Message* msg){
masahikofukasawa 11:cef8dc1cf010 93 event = false;
masahikofukasawa 11:cef8dc1cf010 94 char angle[2] = {0x00,0x00};
masahikofukasawa 11:cef8dc1cf010 95 AK7451::Status status = ak7451->readAngle(angle);
masahikofukasawa 11:cef8dc1cf010 96
masahikofukasawa 11:cef8dc1cf010 97 msg->setCommand(Message::CMD_START_MEASUREMENT);
masahikofukasawa 11:cef8dc1cf010 98 msg->setArgument( 0, status );
masahikofukasawa 11:cef8dc1cf010 99 msg->setArgument( 1, angle[0] );
masahikofukasawa 11:cef8dc1cf010 100 msg->setArgument( 2, angle[1] );
masahikofukasawa 11:cef8dc1cf010 101
masahikofukasawa 11:cef8dc1cf010 102 if( status != SUCCESS){
masahikofukasawa 11:cef8dc1cf010 103 return AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 104 }
masahikofukasawa 11:cef8dc1cf010 105 return AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 106 }
masahikofukasawa 11:cef8dc1cf010 107
masahikofukasawa 11:cef8dc1cf010 108 AkmSensor::Status Ak7451Ctrl::requestCommand(Message* in, Message* out){
masahikofukasawa 11:cef8dc1cf010 109 AkmSensor::Status status = AkmSensor::SUCCESS;
masahikofukasawa 11:cef8dc1cf010 110
masahikofukasawa 11:cef8dc1cf010 111 Message::Command cmd = in->getCommand();
masahikofukasawa 11:cef8dc1cf010 112
masahikofukasawa 11:cef8dc1cf010 113 switch(cmd){
masahikofukasawa 11:cef8dc1cf010 114 case Message::CMD_ANGLE_ZERO_RESET:
masahikofukasawa 11:cef8dc1cf010 115 {
masahikofukasawa 11:cef8dc1cf010 116 AK7451::Status st;
masahikofukasawa 11:cef8dc1cf010 117 if( Ak7451Ctrl::stopSensor() != AkmSensor::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 118 MSG("#Error stop sensor\r\n");
masahikofukasawa 11:cef8dc1cf010 119 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 120 }
masahikofukasawa 11:cef8dc1cf010 121
masahikofukasawa 11:cef8dc1cf010 122 st = ak7451->setOperationMode(AK7451::AK7451_USER_MODE);
masahikofukasawa 11:cef8dc1cf010 123 if( st != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 124 MSG("#Error when set user mode\r\n");
masahikofukasawa 11:cef8dc1cf010 125 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 126 }
masahikofukasawa 11:cef8dc1cf010 127 char temp[2] = {0x00,0x00};
masahikofukasawa 11:cef8dc1cf010 128 st = ak7451->writeRegister(0x06,temp);
masahikofukasawa 11:cef8dc1cf010 129 if( st != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 130 MSG("#Error temp read: code=%d\r\n",st);
masahikofukasawa 11:cef8dc1cf010 131 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 132 }
masahikofukasawa 11:cef8dc1cf010 133 st = ak7451->setAngleZero(); // reset ZP data
masahikofukasawa 11:cef8dc1cf010 134 if( st != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 135 MSG("#Error setAngleZero: code=%d\r\n",st);
masahikofukasawa 11:cef8dc1cf010 136 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 137 }
masahikofukasawa 11:cef8dc1cf010 138 st = ak7451->setOperationMode(AK7451::AK7451_NORMAL_MODE);
masahikofukasawa 11:cef8dc1cf010 139 if( st != AK7451::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 140 MSG("#Error when set normal mode\r\n");
masahikofukasawa 11:cef8dc1cf010 141 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 142 }
masahikofukasawa 11:cef8dc1cf010 143 if( Ak7451Ctrl::startSensor(interval) != AkmSensor::SUCCESS ){
masahikofukasawa 11:cef8dc1cf010 144 MSG("#Error start sensor\r\n");
masahikofukasawa 11:cef8dc1cf010 145 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 146 }
masahikofukasawa 11:cef8dc1cf010 147 if( status == AkmSensor::ERROR ){
masahikofukasawa 11:cef8dc1cf010 148 out->setArgument(0,1);
masahikofukasawa 11:cef8dc1cf010 149 }else{
masahikofukasawa 11:cef8dc1cf010 150 out->setArgument(0,0);
masahikofukasawa 11:cef8dc1cf010 151 }
masahikofukasawa 11:cef8dc1cf010 152 break;
masahikofukasawa 11:cef8dc1cf010 153 }
masahikofukasawa 11:cef8dc1cf010 154 default:
masahikofukasawa 11:cef8dc1cf010 155 {
masahikofukasawa 11:cef8dc1cf010 156 MSG("#Error no command.\r\n");
masahikofukasawa 11:cef8dc1cf010 157 status = AkmSensor::ERROR;
masahikofukasawa 11:cef8dc1cf010 158 break;
masahikofukasawa 11:cef8dc1cf010 159 }
masahikofukasawa 11:cef8dc1cf010 160 }
masahikofukasawa 11:cef8dc1cf010 161
masahikofukasawa 11:cef8dc1cf010 162 return status;
masahikofukasawa 11:cef8dc1cf010 163 }
masahikofukasawa 11:cef8dc1cf010 164