Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
ak7401ctrl.cpp@29:b488d2c89fba, 2017-03-17 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Mar 17 23:29:20 2017 +0000
- Revision:
- 29:b488d2c89fba
- Parent:
- 27:41aa9fb23a2f
- Child:
- 30:5a241d9b3262
Modified for multi sensor demo.
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 | 27:41aa9fb23a2f | 8 | Ak7401Ctrl::Ak7401Ctrl() : AkmSensor(){ |
masahikofukasawa | 10:5c69b067d88a | 9 | ak7401 = NULL; |
masahikofukasawa | 10:5c69b067d88a | 10 | } |
masahikofukasawa | 10:5c69b067d88a | 11 | |
masahikofukasawa | 10:5c69b067d88a | 12 | /** |
masahikofukasawa | 10:5c69b067d88a | 13 | * Destructor. |
masahikofukasawa | 10:5c69b067d88a | 14 | * |
masahikofukasawa | 10:5c69b067d88a | 15 | */ |
masahikofukasawa | 10:5c69b067d88a | 16 | Ak7401Ctrl::~Ak7401Ctrl(){ |
masahikofukasawa | 10:5c69b067d88a | 17 | if (ak7401) delete ak7401; |
masahikofukasawa | 10:5c69b067d88a | 18 | } |
masahikofukasawa | 10:5c69b067d88a | 19 | |
masahikofukasawa | 10:5c69b067d88a | 20 | AkmSensor::Status Ak7401Ctrl::init(const uint8_t id, const uint8_t subid){ |
masahikofukasawa | 10:5c69b067d88a | 21 | primaryId = id; |
masahikofukasawa | 10:5c69b067d88a | 22 | subId = subid; |
masahikofukasawa | 10:5c69b067d88a | 23 | |
masahikofukasawa | 10:5c69b067d88a | 24 | if(subId == SUB_ID_AK7401){ |
masahikofukasawa | 10:5c69b067d88a | 25 | SPI* mSpi; |
masahikofukasawa | 10:5c69b067d88a | 26 | DigitalOut* mCs; |
masahikofukasawa | 10:5c69b067d88a | 27 | AK7401::Status status = AK7401::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 28 | mSpi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK); |
masahikofukasawa | 10:5c69b067d88a | 29 | mSpi->format(8,1); // 8bit, Mode=1 |
masahikofukasawa | 10:5c69b067d88a | 30 | mSpi->frequency(1000000); |
masahikofukasawa | 10:5c69b067d88a | 31 | mCs = new DigitalOut(SPI_CS); |
masahikofukasawa | 10:5c69b067d88a | 32 | ak7401 = new AK7401(); |
masahikofukasawa | 10:5c69b067d88a | 33 | ak7401->begin(mSpi, mCs); |
masahikofukasawa | 13:d008249f0359 | 34 | sensorName = "AK7401"; |
masahikofukasawa | 10:5c69b067d88a | 35 | |
masahikofukasawa | 10:5c69b067d88a | 36 | status = ak7401->setOperationMode(AK7401::AK7401_USER_MODE); |
masahikofukasawa | 10:5c69b067d88a | 37 | if( status != AK7401::SUCCESS ){ |
masahikofukasawa | 14:21e177fc308a | 38 | MSG("#AK7401 set USER mode failed.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 39 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 40 | } |
masahikofukasawa | 14:21e177fc308a | 41 | |
masahikofukasawa | 14:21e177fc308a | 42 | // check connection |
masahikofukasawa | 14:21e177fc308a | 43 | status = ak7401->checkConnection(); |
masahikofukasawa | 14:21e177fc308a | 44 | if( status != AK7401::SUCCESS ){ |
masahikofukasawa | 14:21e177fc308a | 45 | MSG("#AK7401 check connection failed.\r\n"); |
masahikofukasawa | 14:21e177fc308a | 46 | return AkmSensor::ERROR; |
masahikofukasawa | 14:21e177fc308a | 47 | } |
masahikofukasawa | 14:21e177fc308a | 48 | |
masahikofukasawa | 14:21e177fc308a | 49 | // change rotation direction to CW from CCW |
masahikofukasawa | 10:5c69b067d88a | 50 | char data[2] = {0x00,0x07}; // set clockwise rotation |
masahikofukasawa | 10:5c69b067d88a | 51 | status = ak7401->writeEEPROM(0x05,data); // set clockwise |
masahikofukasawa | 10:5c69b067d88a | 52 | if( status != AK7401::SUCCESS ){ |
masahikofukasawa | 14:21e177fc308a | 53 | MSG("#AK7401 set rotation failed.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 54 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 55 | } |
masahikofukasawa | 10:5c69b067d88a | 56 | |
masahikofukasawa | 10:5c69b067d88a | 57 | status = ak7401->setOperationMode(AK7401::AK7401_NORMAL_MODE); |
masahikofukasawa | 10:5c69b067d88a | 58 | if( status != AK7401::SUCCESS ){ |
masahikofukasawa | 14:21e177fc308a | 59 | MSG("#AK7401 set NORMAL mode failed.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 60 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 61 | } |
masahikofukasawa | 14:21e177fc308a | 62 | |
masahikofukasawa | 14:21e177fc308a | 63 | MSG("#AK7401 init succeed.\r\n"); |
masahikofukasawa | 11:cef8dc1cf010 | 64 | |
masahikofukasawa | 11:cef8dc1cf010 | 65 | interval = SENSOR_SAMPLING_RATE; // 10Hz |
masahikofukasawa | 10:5c69b067d88a | 66 | } |
masahikofukasawa | 10:5c69b067d88a | 67 | else{ |
masahikofukasawa | 10:5c69b067d88a | 68 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 69 | } |
masahikofukasawa | 10:5c69b067d88a | 70 | |
masahikofukasawa | 10:5c69b067d88a | 71 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 72 | } |
masahikofukasawa | 10:5c69b067d88a | 73 | |
masahikofukasawa | 10:5c69b067d88a | 74 | AkmSensor::Status Ak7401Ctrl::startSensor(){ |
masahikofukasawa | 29:b488d2c89fba | 75 | ticker.attach(callback(this, &base::setEvent), interval); |
masahikofukasawa | 10:5c69b067d88a | 76 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 77 | } |
masahikofukasawa | 10:5c69b067d88a | 78 | |
masahikofukasawa | 10:5c69b067d88a | 79 | AkmSensor::Status Ak7401Ctrl::startSensor(const float sec){ |
masahikofukasawa | 10:5c69b067d88a | 80 | interval = sec; |
masahikofukasawa | 29:b488d2c89fba | 81 | ticker.attach(callback(this, &base::setEvent), interval); |
masahikofukasawa | 29:b488d2c89fba | 82 | MSG("#Start sensor %s.\r\n",sensorName); |
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 | 29:b488d2c89fba | 88 | AkmSensor::clearEvent(); |
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 | 29:b488d2c89fba | 93 | AkmSensor::clearEvent(); |
masahikofukasawa | 29:b488d2c89fba | 94 | |
masahikofukasawa | 10:5c69b067d88a | 95 | char angle[2] = {0x00,0x00}; |
masahikofukasawa | 10:5c69b067d88a | 96 | AK7401::Status status = ak7401->readAngle(angle); |
masahikofukasawa | 10:5c69b067d88a | 97 | |
masahikofukasawa | 10:5c69b067d88a | 98 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 10:5c69b067d88a | 99 | msg->setArgument( 0, status ); |
masahikofukasawa | 10:5c69b067d88a | 100 | msg->setArgument( 1, angle[0] ); |
masahikofukasawa | 10:5c69b067d88a | 101 | msg->setArgument( 2, angle[1] ); |
masahikofukasawa | 10:5c69b067d88a | 102 | |
masahikofukasawa | 10:5c69b067d88a | 103 | if( status != SUCCESS){ |
masahikofukasawa | 10:5c69b067d88a | 104 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 105 | } |
masahikofukasawa | 10:5c69b067d88a | 106 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 107 | } |
masahikofukasawa | 10:5c69b067d88a | 108 | |
masahikofukasawa | 10:5c69b067d88a | 109 | AkmSensor::Status Ak7401Ctrl::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 10:5c69b067d88a | 110 | AkmSensor::Status status = AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 111 | Message::Command cmd = in->getCommand(); |
masahikofukasawa | 10:5c69b067d88a | 112 | switch(cmd){ |
masahikofukasawa | 10:5c69b067d88a | 113 | case Message::CMD_ANGLE_ZERO_RESET: |
masahikofukasawa | 10:5c69b067d88a | 114 | { |
masahikofukasawa | 14:21e177fc308a | 115 | if( ak7401->setAngleZero() != AK7401::SUCCESS ){ |
masahikofukasawa | 14:21e177fc308a | 116 | MSG("#Error set angle zero\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 117 | status = AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 118 | } |
masahikofukasawa | 10:5c69b067d88a | 119 | if( status == AkmSensor::ERROR ){ |
masahikofukasawa | 10:5c69b067d88a | 120 | out->setArgument(0,1); |
masahikofukasawa | 10:5c69b067d88a | 121 | }else{ |
masahikofukasawa | 10:5c69b067d88a | 122 | out->setArgument(0,0); |
masahikofukasawa | 10:5c69b067d88a | 123 | } |
masahikofukasawa | 10:5c69b067d88a | 124 | break; |
masahikofukasawa | 10:5c69b067d88a | 125 | } |
masahikofukasawa | 10:5c69b067d88a | 126 | default: |
masahikofukasawa | 10:5c69b067d88a | 127 | { |
masahikofukasawa | 11:cef8dc1cf010 | 128 | MSG("#Error no command.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 129 | status = AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 130 | break; |
masahikofukasawa | 10:5c69b067d88a | 131 | } |
masahikofukasawa | 10:5c69b067d88a | 132 | } |
masahikofukasawa | 10:5c69b067d88a | 133 | |
masahikofukasawa | 10:5c69b067d88a | 134 | return status; |
masahikofukasawa | 10:5c69b067d88a | 135 | } |
masahikofukasawa | 10:5c69b067d88a | 136 | |
masahikofukasawa | 13:d008249f0359 | 137 |