ergwrthsgfhrtxhs

Dependencies:   mbed weelio PID

Committer:
mitch092
Date:
Wed Nov 27 19:23:04 2019 +0000
Revision:
22:5e7356dc1d71
Parent:
21:b0e9b4d19d4d
Child:
23:550b68587eb5
yup mmmhmmm

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 22:5e7356dc1d71 45 const float MAX_SPEED = 0.0003f;
mitch092 22:5e7356dc1d71 46 const float MIN_SPEED = 0.024095f;
mitch092 22:5e7356dc1d71 47 //const float MIN_SPEED = 0.5f;
mitch092 22:5e7356dc1d71 48
mitch092 22:5e7356dc1d71 49
mitch092 22:5e7356dc1d71 50 const float MAX_DEGREES = 10.0f;
mitch092 22:5e7356dc1d71 51 const float MIN_DEGREES = 0.0f;
mitch092 21:b0e9b4d19d4d 52
mitch092 22:5e7356dc1d71 53
mitch092 22:5e7356dc1d71 54
mitch092 22:5e7356dc1d71 55 float clamp(float degrees)
mitch092 22:5e7356dc1d71 56 {
mitch092 22:5e7356dc1d71 57 if(degrees < MIN_DEGREES) return MIN_DEGREES;
mitch092 22:5e7356dc1d71 58 else if (degrees >= MAX_DEGREES) return MAX_DEGREES;
mitch092 22:5e7356dc1d71 59 else return degrees;
mitch092 22:5e7356dc1d71 60 }
mitch092 21:b0e9b4d19d4d 61
mitch092 21:b0e9b4d19d4d 62
mitch092 21:b0e9b4d19d4d 63
mitch092 21:b0e9b4d19d4d 64 float degrees_to_period(float degrees)
mitch092 21:b0e9b4d19d4d 65 {
mitch092 22:5e7356dc1d71 66 return (((MAX_SPEED - MIN_SPEED)/(MAX_DEGREES)) * clamp(std::abs(degrees)) + MIN_SPEED);
mitch092 21:b0e9b4d19d4d 67 }
mitch092 20:babcf777607b 68
mitch092 19:29d3c7caea66 69
mitch092 19:29d3c7caea66 70
mitch092 19:29d3c7caea66 71 struct Vec3 {
mitch092 19:29d3c7caea66 72 float x, y, z;
mitch092 19:29d3c7caea66 73 };
mitch092 19:29d3c7caea66 74
cparata 0:69566eea0fba 75 /* Instantiate the expansion board */
davide.aliprandi@st.com 13:fc873da5b445 76 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
cparata 0:69566eea0fba 77
cparata 0:69566eea0fba 78 /* Retrieve the composing elements of the expansion board */
davide.aliprandi@st.com 13:fc873da5b445 79 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:69566eea0fba 80 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
davide.aliprandi@st.com 13:fc873da5b445 81 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:69566eea0fba 82
mitch092 18:9b9ec0b35b45 83
mitch092 18:9b9ec0b35b45 84
mitch092 18:9b9ec0b35b45 85
mitch092 21:b0e9b4d19d4d 86 PwmOut M1_step(D3);
mitch092 19:29d3c7caea66 87 DigitalOut M1_dir(D2);
mitch092 18:9b9ec0b35b45 88
mitch092 21:b0e9b4d19d4d 89 PwmOut M2_step(D5);
mitch092 19:29d3c7caea66 90 DigitalOut M2_dir(D4);
mitch092 18:9b9ec0b35b45 91
cparata 0:69566eea0fba 92
mitch092 21:b0e9b4d19d4d 93 Serial out(USBTX, USBRX);
mitch092 21:b0e9b4d19d4d 94
mitch092 21:b0e9b4d19d4d 95
cparata 0:69566eea0fba 96
mitch092 18:9b9ec0b35b45 97
mitch092 18:9b9ec0b35b45 98 Vec3 get_accel()
mitch092 18:9b9ec0b35b45 99 {
mitch092 18:9b9ec0b35b45 100 Vec3 vec3;
mitch092 18:9b9ec0b35b45 101 int32_t axes[3];
mitch092 18:9b9ec0b35b45 102 acc_gyro->get_x_axes(axes);
cparata 0:69566eea0fba 103
mitch092 18:9b9ec0b35b45 104 vec3.x = axes[0]/1000.0f + 0.013f;
mitch092 18:9b9ec0b35b45 105 vec3.y = axes[1]/1000.0f + 0.004f;
mitch092 18:9b9ec0b35b45 106 vec3.z = axes[2]/1000.0f - 0.032f;
mitch092 18:9b9ec0b35b45 107
mitch092 18:9b9ec0b35b45 108 return vec3;
mitch092 18:9b9ec0b35b45 109 }
cparata 0:69566eea0fba 110
mitch092 18:9b9ec0b35b45 111 void print_vec3(const char* str, const Vec3& vec3)
mitch092 18:9b9ec0b35b45 112 {
mitch092 18:9b9ec0b35b45 113 std::cout << str << vec3.x << " " << vec3.y << " " << vec3.z << "\r\n";
cparata 0:69566eea0fba 114 }
cparata 0:69566eea0fba 115
mitch092 20:babcf777607b 116 float rad_to_deg(float radians)
mitch092 20:babcf777607b 117 {
mitch092 20:babcf777607b 118 return radians * 57.2957795131f;
mitch092 20:babcf777607b 119 }
mitch092 19:29d3c7caea66 120
mitch092 19:29d3c7caea66 121
mitch092 19:29d3c7caea66 122
mitch092 19:29d3c7caea66 123
mitch092 18:9b9ec0b35b45 124 int main()
mitch092 18:9b9ec0b35b45 125 {
mitch092 18:9b9ec0b35b45 126 /* Enable all sensors */
mitch092 18:9b9ec0b35b45 127 magnetometer->enable();
mitch092 18:9b9ec0b35b45 128 accelerometer->enable();
mitch092 18:9b9ec0b35b45 129 acc_gyro->enable_x();
mitch092 18:9b9ec0b35b45 130 acc_gyro->enable_g();
mitch092 18:9b9ec0b35b45 131
mitch092 20:babcf777607b 132 Vec3 vec3;
mitch092 18:9b9ec0b35b45 133
mitch092 19:29d3c7caea66 134 M1_dir = 1;
mitch092 19:29d3c7caea66 135 M2_dir = 0;
mitch092 22:5e7356dc1d71 136
mitch092 21:b0e9b4d19d4d 137 M1_step = 0.5f;
mitch092 21:b0e9b4d19d4d 138 M2_step = 0.5f;
mitch092 19:29d3c7caea66 139
mitch092 21:b0e9b4d19d4d 140 float angle;
mitch092 21:b0e9b4d19d4d 141 float period;
mitch092 22:5e7356dc1d71 142
mitch092 22:5e7356dc1d71 143 float test = 1.05f;
mitch092 18:9b9ec0b35b45 144
mitch092 18:9b9ec0b35b45 145 for(;;) {
mitch092 19:29d3c7caea66 146 vec3 = get_accel();
mitch092 21:b0e9b4d19d4d 147 angle = rad_to_deg(atan(vec3.y / sqrtf(vec3.x*vec3.x + vec3.z*vec3.z)));
mitch092 21:b0e9b4d19d4d 148 period = (degrees_to_period(angle));
mitch092 19:29d3c7caea66 149
cparata 0:69566eea0fba 150
mitch092 19:29d3c7caea66 151
mitch092 21:b0e9b4d19d4d 152 //Change direction.
mitch092 21:b0e9b4d19d4d 153 if(angle > 0) {
mitch092 21:b0e9b4d19d4d 154 M1_dir = 0;
mitch092 21:b0e9b4d19d4d 155 M2_dir = 1;
mitch092 21:b0e9b4d19d4d 156 } else {
mitch092 21:b0e9b4d19d4d 157 M1_dir = 1;
mitch092 21:b0e9b4d19d4d 158 M2_dir = 0;
mitch092 21:b0e9b4d19d4d 159 }
mitch092 22:5e7356dc1d71 160
mitch092 21:b0e9b4d19d4d 161
mitch092 21:b0e9b4d19d4d 162 //Control stepper motor.
mitch092 22:5e7356dc1d71 163 out.printf("%f %f %f\r\n", period, angle, degrees_to_period(MAX_DEGREES/test));
mitch092 22:5e7356dc1d71 164 //if(angle < 2.0f || angle > -2.0f)
mitch092 21:b0e9b4d19d4d 165 M1_step.period(period);
mitch092 21:b0e9b4d19d4d 166 M2_step.period(period);
mitch092 22:5e7356dc1d71 167 //M1_step.period(0.0003);
mitch092 22:5e7356dc1d71 168 //M2_step.period(0.0003);
mitch092 22:5e7356dc1d71 169 //M1_step.period(degrees_to_period(MAX_DEGREES/test));
mitch092 22:5e7356dc1d71 170 //M2_step.period(degrees_to_period(MAX_DEGREES/test));
mitch092 20:babcf777607b 171 }
mitch092 20:babcf777607b 172 }