Accelerator/magnetometer based toy car control using FRDM k64F

Dependencies:   mbed FXOS8700Q

Committer:
NPALLE
Date:
Wed May 01 19:47:29 2019 +0000
Revision:
0:4b0626534fae
Accelerator/ magnetometer based toy car control

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NPALLE 0:4b0626534fae 1 /* FXOS8700Q Example Program
NPALLE 0:4b0626534fae 2 * Copyright (c) 2014-2015 ARM Limited
NPALLE 0:4b0626534fae 3 *
NPALLE 0:4b0626534fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
NPALLE 0:4b0626534fae 5 * you may not use this file except in compliance with the License.
NPALLE 0:4b0626534fae 6 * You may obtain a copy of the License at
NPALLE 0:4b0626534fae 7 *
NPALLE 0:4b0626534fae 8 * http://www.apache.org/licenses/LICENSE-2.0
NPALLE 0:4b0626534fae 9 *
NPALLE 0:4b0626534fae 10 * Unless required by applicable law or agreed to in writing, software
NPALLE 0:4b0626534fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
NPALLE 0:4b0626534fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NPALLE 0:4b0626534fae 13 * See the License for the specific language governing permissions and
NPALLE 0:4b0626534fae 14 * limitations under the License.
NPALLE 0:4b0626534fae 15 */
NPALLE 0:4b0626534fae 16
NPALLE 0:4b0626534fae 17 #include "mbed.h"
NPALLE 0:4b0626534fae 18 #include "FXOS8700Q.h"
NPALLE 0:4b0626534fae 19
NPALLE 0:4b0626534fae 20 Serial pc(USBTX, USBRX);
NPALLE 0:4b0626534fae 21 I2C i2c(PTE25, PTE24);
NPALLE 0:4b0626534fae 22 //FXOS8700Q fxos(i2c, FXOS8700CQ_SLAVE_ADDR1);
NPALLE 0:4b0626534fae 23 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors
NPALLE 0:4b0626534fae 24 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
NPALLE 0:4b0626534fae 25 //FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR0); // Configured for use with the FRDM-MULTI shield
NPALLE 0:4b0626534fae 26 //FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR0);
NPALLE 0:4b0626534fae 27
NPALLE 0:4b0626534fae 28
NPALLE 0:4b0626534fae 29 int main(void)
NPALLE 0:4b0626534fae 30 {
NPALLE 0:4b0626534fae 31 motion_data_units_t acc_data, mag_data;
NPALLE 0:4b0626534fae 32 motion_data_counts_t acc_raw, mag_raw;
NPALLE 0:4b0626534fae 33 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
NPALLE 0:4b0626534fae 34 int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int;
NPALLE 0:4b0626534fae 35
NPALLE 0:4b0626534fae 36 acc.enable();
NPALLE 0:4b0626534fae 37 mag.enable();
NPALLE 0:4b0626534fae 38 printf("FXOS8700QAccelerometer Who Am I= %X\r\n", acc.whoAmI());
NPALLE 0:4b0626534fae 39 printf("FXOS8700QMagnetometer Who Am I= %X\r\n", acc.whoAmI());
NPALLE 0:4b0626534fae 40 while (true) {
NPALLE 0:4b0626534fae 41 // counts based results
NPALLE 0:4b0626534fae 42 acc.getAxis(acc_raw);
NPALLE 0:4b0626534fae 43 mag.getAxis(mag_raw);
NPALLE 0:4b0626534fae 44 printf("ACC: X=%06dd Y=%06dd Z=%06dd \t MAG: X=%06dd Y=%06dd Z=%06dd\r\n", acc_raw.x, acc_raw.y, acc_raw.z, mag_raw.x, mag_raw.y, mag_raw.z);
NPALLE 0:4b0626534fae 45 acc.getX(raX);
NPALLE 0:4b0626534fae 46 acc.getY(raY);
NPALLE 0:4b0626534fae 47 acc.getZ(raZ);
NPALLE 0:4b0626534fae 48 mag.getX(rmX);
NPALLE 0:4b0626534fae 49 mag.getY(rmY);
NPALLE 0:4b0626534fae 50 mag.getZ(rmZ);
NPALLE 0:4b0626534fae 51 printf("ACC: X=%06dd Y=%06dd Z=%06dd \t MAG: X=%06dd Y=%06dd Z=%06dd\r\n", raX, raY, raZ, rmX, rmY, rmZ);
NPALLE 0:4b0626534fae 52 printf("ACC: X=%06dd Y=%06dd Z=%06dd \t MAG: X=%06dd Y=%06dd Z=%06dd\r\n", acc.getX(tmp_int), acc.getY(tmp_int), acc.getZ(tmp_int), mag.getX(tmp_int), mag.getY(tmp_int), mag.getZ(tmp_int));
NPALLE 0:4b0626534fae 53 // unit based results
NPALLE 0:4b0626534fae 54 acc.getAxis(acc_data);
NPALLE 0:4b0626534fae 55 mag.getAxis(mag_data);
NPALLE 0:4b0626534fae 56 printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", acc_data.x, acc_data.y, acc_data.z, mag_data.x, mag_data.y, mag_data.z);
NPALLE 0:4b0626534fae 57 acc.getX(faX);
NPALLE 0:4b0626534fae 58 acc.getY(faY);
NPALLE 0:4b0626534fae 59 acc.getZ(faZ);
NPALLE 0:4b0626534fae 60 mag.getX(fmX);
NPALLE 0:4b0626534fae 61 mag.getY(fmY);
NPALLE 0:4b0626534fae 62 mag.getZ(fmZ);
NPALLE 0:4b0626534fae 63 printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", faX, faY, faZ, fmX, fmY, fmZ);
NPALLE 0:4b0626534fae 64 printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", acc.getX(tmp_float), acc.getY(tmp_float), acc.getZ(tmp_float), mag.getX(tmp_float), mag.getY(tmp_float), mag.getZ(tmp_float));
NPALLE 0:4b0626534fae 65 puts("");
NPALLE 0:4b0626534fae 66 wait(5.0f);
NPALLE 0:4b0626534fae 67 }
NPALLE 0:4b0626534fae 68 }