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