ergwrthsgfhrtxhs

Dependencies:   mbed weelio PID

Committer:
block011
Date:
Fri Dec 06 00:49:17 2019 +0000
Revision:
23:550b68587eb5
Parent:
22:5e7356dc1d71
Child:
24:2c7787fc4fde
Test commit

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"
block011 23:550b68587eb5 42 #include "PID.h"
mitch092 18:9b9ec0b35b45 43 #include <iostream>
mitch092 18:9b9ec0b35b45 44 #include <cmath>
cparata 0:69566eea0fba 45
block011 23:550b68587eb5 46 const float MAX_SPEED = 0.00075f;
block011 23:550b68587eb5 47 const float MIN_SPEED = 0.01000f;
mitch092 22:5e7356dc1d71 48
mitch092 22:5e7356dc1d71 49
block011 23:550b68587eb5 50 const float MAX_DEGREES = 3.0f;
mitch092 22:5e7356dc1d71 51 const float MIN_DEGREES = 0.0f;
mitch092 21:b0e9b4d19d4d 52
block011 23:550b68587eb5 53 float Acc_angle_error = 0;
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
block011 23:550b68587eb5 84 float unnormalize(float t){
block011 23:550b68587eb5 85 return (MIN_SPEED - (t * ( MIN_SPEED - MAX_SPEED)));
block011 23:550b68587eb5 86 }
mitch092 18:9b9ec0b35b45 87
mitch092 21:b0e9b4d19d4d 88 PwmOut M1_step(D3);
mitch092 19:29d3c7caea66 89 DigitalOut M1_dir(D2);
mitch092 18:9b9ec0b35b45 90
mitch092 21:b0e9b4d19d4d 91 PwmOut M2_step(D5);
mitch092 19:29d3c7caea66 92 DigitalOut M2_dir(D4);
mitch092 18:9b9ec0b35b45 93
cparata 0:69566eea0fba 94
mitch092 21:b0e9b4d19d4d 95 Serial out(USBTX, USBRX);
mitch092 21:b0e9b4d19d4d 96
mitch092 21:b0e9b4d19d4d 97
cparata 0:69566eea0fba 98
mitch092 18:9b9ec0b35b45 99
mitch092 18:9b9ec0b35b45 100 Vec3 get_accel()
mitch092 18:9b9ec0b35b45 101 {
mitch092 18:9b9ec0b35b45 102 Vec3 vec3;
mitch092 18:9b9ec0b35b45 103 int32_t axes[3];
mitch092 18:9b9ec0b35b45 104 acc_gyro->get_x_axes(axes);
cparata 0:69566eea0fba 105
mitch092 18:9b9ec0b35b45 106 vec3.x = axes[0]/1000.0f + 0.013f;
mitch092 18:9b9ec0b35b45 107 vec3.y = axes[1]/1000.0f + 0.004f;
mitch092 18:9b9ec0b35b45 108 vec3.z = axes[2]/1000.0f - 0.032f;
mitch092 18:9b9ec0b35b45 109
mitch092 18:9b9ec0b35b45 110 return vec3;
mitch092 18:9b9ec0b35b45 111 }
cparata 0:69566eea0fba 112
mitch092 18:9b9ec0b35b45 113 void print_vec3(const char* str, const Vec3& vec3)
mitch092 18:9b9ec0b35b45 114 {
mitch092 18:9b9ec0b35b45 115 std::cout << str << vec3.x << " " << vec3.y << " " << vec3.z << "\r\n";
cparata 0:69566eea0fba 116 }
cparata 0:69566eea0fba 117
mitch092 20:babcf777607b 118 float rad_to_deg(float radians)
mitch092 20:babcf777607b 119 {
mitch092 20:babcf777607b 120 return radians * 57.2957795131f;
mitch092 20:babcf777607b 121 }
mitch092 19:29d3c7caea66 122
block011 23:550b68587eb5 123 void calc_accel_error() {
block011 23:550b68587eb5 124 Vec3 vec3;
block011 23:550b68587eb5 125
block011 23:550b68587eb5 126 for(int i = 0 ; i < 200; i++){
block011 23:550b68587eb5 127 vec3 = get_accel();
block011 23:550b68587eb5 128 Acc_angle_error += rad_to_deg(atan(vec3.y / sqrtf(vec3.x*vec3.x + vec3.z*vec3.z)));
block011 23:550b68587eb5 129
block011 23:550b68587eb5 130 }
block011 23:550b68587eb5 131 Acc_angle_error /= 200;
block011 23:550b68587eb5 132
block011 23:550b68587eb5 133 }
mitch092 19:29d3c7caea66 134
mitch092 19:29d3c7caea66 135
mitch092 19:29d3c7caea66 136
mitch092 18:9b9ec0b35b45 137 int main()
mitch092 18:9b9ec0b35b45 138 {
mitch092 18:9b9ec0b35b45 139 magnetometer->enable();
mitch092 18:9b9ec0b35b45 140 accelerometer->enable();
mitch092 18:9b9ec0b35b45 141 acc_gyro->enable_x();
mitch092 18:9b9ec0b35b45 142 acc_gyro->enable_g();
block011 23:550b68587eb5 143
block011 23:550b68587eb5 144 calc_accel_error();
mitch092 20:babcf777607b 145 Vec3 vec3;
mitch092 18:9b9ec0b35b45 146
mitch092 19:29d3c7caea66 147 M1_dir = 1;
mitch092 19:29d3c7caea66 148 M2_dir = 0;
block011 23:550b68587eb5 149 float p;
mitch092 21:b0e9b4d19d4d 150 M1_step = 0.5f;
mitch092 21:b0e9b4d19d4d 151 M2_step = 0.5f;
block011 23:550b68587eb5 152 PID Lpid(1.0, 0.006, 0.003, 0.05);
block011 23:550b68587eb5 153 Lpid.setInputLimits(-45.0,45.0);
block011 23:550b68587eb5 154 Lpid.setOutputLimits(0.0,1.0);
block011 23:550b68587eb5 155 Lpid.setSetPoint(-1.0);
block011 23:550b68587eb5 156 Lpid.setMode(AUTO_MODE);
block011 23:550b68587eb5 157 PID Rpid(1.0, 0.006, 0.003, 0.05);
block011 23:550b68587eb5 158 Rpid.setInputLimits(-45.0,45.0);
block011 23:550b68587eb5 159 Rpid.setOutputLimits(0.0,1.0);
block011 23:550b68587eb5 160 Rpid.setSetPoint(-1.0);
block011 23:550b68587eb5 161 Rpid.setMode(AUTO_MODE);
mitch092 19:29d3c7caea66 162
mitch092 21:b0e9b4d19d4d 163 float angle;
mitch092 21:b0e9b4d19d4d 164 float period;
block011 23:550b68587eb5 165 float prev = 0;
block011 23:550b68587eb5 166 float L = 0, R = 0;
mitch092 18:9b9ec0b35b45 167 for(;;) {
mitch092 19:29d3c7caea66 168 vec3 = get_accel();
block011 23:550b68587eb5 169 int mean = 0;
block011 23:550b68587eb5 170 for (int i = 0; i < 5; i++) {
block011 23:550b68587eb5 171 angle = rad_to_deg(atan(vec3.y / sqrtf(vec3.x*vec3.x + vec3.z*vec3.z))) - Acc_angle_error;
block011 23:550b68587eb5 172 mean += angle;
block011 23:550b68587eb5 173 wait(.01);
block011 23:550b68587eb5 174 }
block011 23:550b68587eb5 175 mean /= 5;
block011 23:550b68587eb5 176 angle = mean;
block011 23:550b68587eb5 177
mitch092 21:b0e9b4d19d4d 178 period = (degrees_to_period(angle));
block011 23:550b68587eb5 179 Lpid.setProcessValue(angle);
block011 23:550b68587eb5 180 Rpid.setProcessValue(angle * -1);
block011 23:550b68587eb5 181 L = Lpid.compute();
block011 23:550b68587eb5 182 R = Rpid.compute();
block011 23:550b68587eb5 183
block011 23:550b68587eb5 184 p = abs( L-R);
block011 23:550b68587eb5 185 //Change direction.
block011 23:550b68587eb5 186 if(angle < 0) {
mitch092 21:b0e9b4d19d4d 187 M1_dir = 0;
mitch092 21:b0e9b4d19d4d 188 M2_dir = 1;
mitch092 21:b0e9b4d19d4d 189 } else {
mitch092 21:b0e9b4d19d4d 190 M1_dir = 1;
mitch092 21:b0e9b4d19d4d 191 M2_dir = 0;
mitch092 21:b0e9b4d19d4d 192 }
mitch092 22:5e7356dc1d71 193
block011 23:550b68587eb5 194 M1_step.period(unnormalize(p));
block011 23:550b68587eb5 195 M2_step.period(unnormalize(p));
block011 23:550b68587eb5 196
block011 23:550b68587eb5 197 prev = angle;
mitch092 21:b0e9b4d19d4d 198
block011 23:550b68587eb5 199 out.printf("%f %f %f %f\r\n", p, L, R , angle);
block011 23:550b68587eb5 200
block011 23:550b68587eb5 201
mitch092 20:babcf777607b 202 }
mitch092 20:babcf777607b 203 }