ergwrthsgfhrtxhs

Dependencies:   mbed weelio PID

Committer:
mitch092
Date:
Sat Nov 23 00:55:46 2019 +0000
Revision:
21:b0e9b4d19d4d
Parent:
20:babcf777607b
Child:
22:5e7356dc1d71
sdd;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:69566eea0fba 1 /**
cparata 0:69566eea0fba 2 ******************************************************************************
cparata 0:69566eea0fba 3 * @file main.cpp
cparata 8:8f495e604424 4 * @author CLab
cparata 8:8f495e604424 5 * @version V1.0.0
cparata 8:8f495e604424 6 * @date 2-December-2016
mitch092 18:9b9ec0b35b45 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A1
cparata 0:69566eea0fba 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:69566eea0fba 9 ******************************************************************************
cparata 0:69566eea0fba 10 * @attention
cparata 0:69566eea0fba 11 *
cparata 0:69566eea0fba 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 0:69566eea0fba 13 *
cparata 0:69566eea0fba 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:69566eea0fba 15 * are permitted provided that the following conditions are met:
cparata 0:69566eea0fba 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:69566eea0fba 17 * this list of conditions and the following disclaimer.
cparata 0:69566eea0fba 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:69566eea0fba 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:69566eea0fba 20 * and/or other materials provided with the distribution.
cparata 0:69566eea0fba 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:69566eea0fba 22 * may be used to endorse or promote products derived from this software
cparata 0:69566eea0fba 23 * without specific prior written permission.
cparata 0:69566eea0fba 24 *
cparata 0:69566eea0fba 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:69566eea0fba 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:69566eea0fba 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:69566eea0fba 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:69566eea0fba 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:69566eea0fba 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:69566eea0fba 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:69566eea0fba 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:69566eea0fba 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:69566eea0fba 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:69566eea0fba 35 *
cparata 0:69566eea0fba 36 ******************************************************************************
mitch092 18:9b9ec0b35b45 37 */
cparata 0:69566eea0fba 38
cparata 0:69566eea0fba 39 /* Includes */
cparata 0:69566eea0fba 40 #include "mbed.h"
davide.aliprandi@st.com 13:fc873da5b445 41 #include "XNucleoIKS01A2.h"
mitch092 18:9b9ec0b35b45 42 #include <iostream>
mitch092 18:9b9ec0b35b45 43 #include <cmath>
cparata 0:69566eea0fba 44
mitch092 21:b0e9b4d19d4d 45 const float MAX_SPEED = 0.0014f;
mitch092 21:b0e9b4d19d4d 46 const float MIN_SPEED = 0.1f;
mitch092 21:b0e9b4d19d4d 47
mitch092 21:b0e9b4d19d4d 48 const float MAX_DEGREES = 5.0f;
mitch092 21:b0e9b4d19d4d 49 const float MIN_DEGREES = 0.0f;
mitch092 21:b0e9b4d19d4d 50
mitch092 21:b0e9b4d19d4d 51
mitch092 21:b0e9b4d19d4d 52
mitch092 21:b0e9b4d19d4d 53 float degrees_to_period(float degrees)
mitch092 21:b0e9b4d19d4d 54 {
mitch092 21:b0e9b4d19d4d 55 return (((MAX_SPEED - MIN_SPEED)/(MAX_DEGREES)) * std::abs(degrees) + MIN_SPEED);
mitch092 21:b0e9b4d19d4d 56 }
mitch092 20:babcf777607b 57
mitch092 19:29d3c7caea66 58
mitch092 19:29d3c7caea66 59
mitch092 19:29d3c7caea66 60 struct Vec3 {
mitch092 19:29d3c7caea66 61 float x, y, z;
mitch092 19:29d3c7caea66 62 };
mitch092 19:29d3c7caea66 63
cparata 0:69566eea0fba 64 /* Instantiate the expansion board */
davide.aliprandi@st.com 13:fc873da5b445 65 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
cparata 0:69566eea0fba 66
cparata 0:69566eea0fba 67 /* Retrieve the composing elements of the expansion board */
davide.aliprandi@st.com 13:fc873da5b445 68 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:69566eea0fba 69 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
davide.aliprandi@st.com 13:fc873da5b445 70 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:69566eea0fba 71
mitch092 18:9b9ec0b35b45 72
mitch092 18:9b9ec0b35b45 73
mitch092 18:9b9ec0b35b45 74
mitch092 21:b0e9b4d19d4d 75 PwmOut M1_step(D3);
mitch092 19:29d3c7caea66 76 DigitalOut M1_dir(D2);
mitch092 18:9b9ec0b35b45 77
mitch092 21:b0e9b4d19d4d 78 PwmOut M2_step(D5);
mitch092 19:29d3c7caea66 79 DigitalOut M2_dir(D4);
mitch092 18:9b9ec0b35b45 80
cparata 0:69566eea0fba 81
mitch092 21:b0e9b4d19d4d 82 Serial out(USBTX, USBRX);
mitch092 21:b0e9b4d19d4d 83
mitch092 21:b0e9b4d19d4d 84
cparata 0:69566eea0fba 85
mitch092 18:9b9ec0b35b45 86
mitch092 18:9b9ec0b35b45 87 Vec3 get_accel()
mitch092 18:9b9ec0b35b45 88 {
mitch092 18:9b9ec0b35b45 89 Vec3 vec3;
mitch092 18:9b9ec0b35b45 90 int32_t axes[3];
mitch092 18:9b9ec0b35b45 91 acc_gyro->get_x_axes(axes);
cparata 0:69566eea0fba 92
mitch092 18:9b9ec0b35b45 93 vec3.x = axes[0]/1000.0f + 0.013f;
mitch092 18:9b9ec0b35b45 94 vec3.y = axes[1]/1000.0f + 0.004f;
mitch092 18:9b9ec0b35b45 95 vec3.z = axes[2]/1000.0f - 0.032f;
mitch092 18:9b9ec0b35b45 96
mitch092 18:9b9ec0b35b45 97 return vec3;
mitch092 18:9b9ec0b35b45 98 }
cparata 0:69566eea0fba 99
mitch092 18:9b9ec0b35b45 100 void print_vec3(const char* str, const Vec3& vec3)
mitch092 18:9b9ec0b35b45 101 {
mitch092 18:9b9ec0b35b45 102 std::cout << str << vec3.x << " " << vec3.y << " " << vec3.z << "\r\n";
cparata 0:69566eea0fba 103 }
cparata 0:69566eea0fba 104
mitch092 20:babcf777607b 105 float rad_to_deg(float radians)
mitch092 20:babcf777607b 106 {
mitch092 20:babcf777607b 107 return radians * 57.2957795131f;
mitch092 20:babcf777607b 108 }
mitch092 19:29d3c7caea66 109
mitch092 19:29d3c7caea66 110
mitch092 19:29d3c7caea66 111
mitch092 19:29d3c7caea66 112
mitch092 18:9b9ec0b35b45 113 int main()
mitch092 18:9b9ec0b35b45 114 {
mitch092 18:9b9ec0b35b45 115 /* Enable all sensors */
mitch092 18:9b9ec0b35b45 116 magnetometer->enable();
mitch092 18:9b9ec0b35b45 117 accelerometer->enable();
mitch092 18:9b9ec0b35b45 118 acc_gyro->enable_x();
mitch092 18:9b9ec0b35b45 119 acc_gyro->enable_g();
mitch092 18:9b9ec0b35b45 120
mitch092 20:babcf777607b 121 Vec3 vec3;
mitch092 18:9b9ec0b35b45 122
mitch092 19:29d3c7caea66 123 M1_dir = 1;
mitch092 19:29d3c7caea66 124 M2_dir = 0;
mitch092 21:b0e9b4d19d4d 125
mitch092 21:b0e9b4d19d4d 126 M1_step = 0.5f;
mitch092 21:b0e9b4d19d4d 127 M2_step = 0.5f;
mitch092 19:29d3c7caea66 128
mitch092 21:b0e9b4d19d4d 129 float angle;
mitch092 21:b0e9b4d19d4d 130 float period;
mitch092 20:babcf777607b 131
mitch092 18:9b9ec0b35b45 132
mitch092 18:9b9ec0b35b45 133 for(;;) {
mitch092 19:29d3c7caea66 134 vec3 = get_accel();
mitch092 21:b0e9b4d19d4d 135 angle = rad_to_deg(atan(vec3.y / sqrtf(vec3.x*vec3.x + vec3.z*vec3.z)));
mitch092 21:b0e9b4d19d4d 136 period = (degrees_to_period(angle));
mitch092 19:29d3c7caea66 137
cparata 0:69566eea0fba 138
mitch092 19:29d3c7caea66 139
mitch092 21:b0e9b4d19d4d 140 //Change direction.
mitch092 21:b0e9b4d19d4d 141 if(angle > 0) {
mitch092 21:b0e9b4d19d4d 142 M1_dir = 0;
mitch092 21:b0e9b4d19d4d 143 M2_dir = 1;
mitch092 21:b0e9b4d19d4d 144 } else {
mitch092 21:b0e9b4d19d4d 145 M1_dir = 1;
mitch092 21:b0e9b4d19d4d 146 M2_dir = 0;
mitch092 21:b0e9b4d19d4d 147 }
mitch092 21:b0e9b4d19d4d 148
mitch092 21:b0e9b4d19d4d 149 //Control stepper motor.
mitch092 21:b0e9b4d19d4d 150 out.printf("%f %f\r\n", period, angle);
mitch092 21:b0e9b4d19d4d 151 M1_step.period(period);
mitch092 21:b0e9b4d19d4d 152 M2_step.period(period);
mitch092 20:babcf777607b 153 }
mitch092 20:babcf777607b 154 }