![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Use IQS62X sensor and move motor by detected angle
Dependencies: DRV8830 IQS62x IQSDisplayTerminal UIT_ACM1602NI mbed
Fork of Nucleo_ACM1602_I2C_DC by
main.cpp@4:83f7df775d46, 2017-09-05 (annotated)
- Committer:
- 8mona
- Date:
- Tue Sep 05 15:05:38 2017 +0000
- Revision:
- 4:83f7df775d46
- Parent:
- 3:6474ab60854e
- Child:
- 5:2b9614aa1171
Add S-Curve acceleration temporally
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:132eb47d57fb | 1 | //------------------------------------------------------------ |
MikamiUitOpen | 0:132eb47d57fb | 2 | // Demo program for LCD ACM1602NI using I2C interface |
MikamiUitOpen | 0:132eb47d57fb | 3 | // Pullup resistors for SDA and SCL: 4.7 kΩ |
MikamiUitOpen | 1:d5e9bd9b38ad | 4 | // 2016/04/01, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:132eb47d57fb | 5 | //------------------------------------------------------------ |
MikamiUitOpen | 0:132eb47d57fb | 6 | |
MikamiUitOpen | 0:132eb47d57fb | 7 | #include "ACM1602NI.hpp" |
8mona | 3:6474ab60854e | 8 | #include "DRV8830.h" |
8mona | 3:6474ab60854e | 9 | |
8mona | 2:ea066749e515 | 10 | //Cycle |
8mona | 4:83f7df775d46 | 11 | #define ON_DURATION 8*2 //On time [*100ms] |
8mona | 4:83f7df775d46 | 12 | #define SWITCH_PERIOD 30*2 //Cycle time[*100ms] |
8mona | 4:83f7df775d46 | 13 | #define WAIT_DELAY 5*2 //Delay time [*100ms] |
8mona | 3:6474ab60854e | 14 | #define TOTAL_TIMES 30000 //total times n |
8mona | 4:83f7df775d46 | 15 | #define ACCEL_SIZE 6 |
8mona | 4:83f7df775d46 | 16 | #define DECEL_SIZE 8 |
8mona | 3:6474ab60854e | 17 | |
8mona | 3:6474ab60854e | 18 | |
8mona | 3:6474ab60854e | 19 | const float spd_table[] = {1.0,0.7,0.5}; |
8mona | 4:83f7df775d46 | 20 | const float vol_accel[ACCEL_SIZE] = {0,0.3,0.6,0.8,0.95,1}; |
8mona | 4:83f7df775d46 | 21 | const float vol_decel[DECEL_SIZE] = {1,0.9,0.75,0.55,0.4,0.25,0.15,0}; |
8mona | 3:6474ab60854e | 22 | static int sp_index=0; |
8mona | 2:ea066749e515 | 23 | |
8mona | 2:ea066749e515 | 24 | |
MikamiUitOpen | 0:132eb47d57fb | 25 | using namespace Mikami; |
MikamiUitOpen | 0:132eb47d57fb | 26 | |
MikamiUitOpen | 0:132eb47d57fb | 27 | Acm1602Ni lcd_; // Default, OK |
8mona | 3:6474ab60854e | 28 | |
MikamiUitOpen | 0:132eb47d57fb | 29 | //Acm1602Ni lcd_(D14, D15); // OK |
MikamiUitOpen | 0:132eb47d57fb | 30 | //Acm1602Ni lcd_(D14, D15, 200000); // OK |
MikamiUitOpen | 0:132eb47d57fb | 31 | //Acm1602Ni lcd_(D14, D15, 200000, true, true); // OK |
MikamiUitOpen | 0:132eb47d57fb | 32 | //Acm1602Ni lcd_(PB_3, PB_10); // OK |
MikamiUitOpen | 0:132eb47d57fb | 33 | //Acm1602Ni lcd_(PC_9, PA_8); // OK |
MikamiUitOpen | 0:132eb47d57fb | 34 | //Acm1602Ni lcd_(PB_4, PA_8); // OK |
MikamiUitOpen | 0:132eb47d57fb | 35 | |
MikamiUitOpen | 0:132eb47d57fb | 36 | Ticker timer_; |
8mona | 3:6474ab60854e | 37 | I2C i2c(D14, D15); |
8mona | 3:6474ab60854e | 38 | DRV8830 motor(i2c, DRV8830ADDR_NN); |
8mona | 3:6474ab60854e | 39 | //DigitalOut Relay1(D2); |
8mona | 2:ea066749e515 | 40 | InterruptIn button1(USER_BUTTON); |
8mona | 3:6474ab60854e | 41 | static float motor_speed; |
MikamiUitOpen | 0:132eb47d57fb | 42 | |
8mona | 3:6474ab60854e | 43 | |
MikamiUitOpen | 0:132eb47d57fb | 44 | // Display elapsed time in minutes and seconds |
MikamiUitOpen | 0:132eb47d57fb | 45 | void TimerIsr() |
MikamiUitOpen | 0:132eb47d57fb | 46 | { |
8mona | 3:6474ab60854e | 47 | |
8mona | 3:6474ab60854e | 48 | //For LED Time-Sec display |
MikamiUitOpen | 0:132eb47d57fb | 49 | static int k = 0; |
8mona | 2:ea066749e515 | 50 | static char ctext[4]="---"; |
8mona | 2:ea066749e515 | 51 | div_t d_Cycle = div (k, SWITCH_PERIOD); |
8mona | 2:ea066749e515 | 52 | |
8mona | 2:ea066749e515 | 53 | //for Current time |
8mona | 2:ea066749e515 | 54 | div_t d_sec = div(k,600); //60s * 10n |
8mona | 2:ea066749e515 | 55 | int t_min = d_sec.quot; |
8mona | 2:ea066749e515 | 56 | div_t d_min = div(t_min,60); //1min=60s |
8mona | 2:ea066749e515 | 57 | int t_hr = d_min.quot; |
8mona | 2:ea066749e515 | 58 | |
8mona | 2:ea066749e515 | 59 | //for Current time |
8mona | 2:ea066749e515 | 60 | div_t df_sec = div(TOTAL_TIMES*SWITCH_PERIOD,600); //60s * 10n |
8mona | 2:ea066749e515 | 61 | int tf_min = df_sec.quot; |
8mona | 2:ea066749e515 | 62 | div_t df_min = div(tf_min,60); //1min=60s |
8mona | 2:ea066749e515 | 63 | int tf_hr = df_min.quot; |
8mona | 3:6474ab60854e | 64 | |
8mona | 3:6474ab60854e | 65 | |
8mona | 3:6474ab60854e | 66 | //Motor activation |
8mona | 4:83f7df775d46 | 67 | static int mode = 1; |
8mona | 2:ea066749e515 | 68 | |
8mona | 4:83f7df775d46 | 69 | if(WAIT_DELAY <= d_Cycle.rem && d_Cycle.rem < (WAIT_DELAY+ON_DURATION) ) |
8mona | 4:83f7df775d46 | 70 | { |
8mona | 4:83f7df775d46 | 71 | int accel_index = d_Cycle.rem - WAIT_DELAY; |
8mona | 4:83f7df775d46 | 72 | if (accel_index < ACCEL_SIZE) |
8mona | 4:83f7df775d46 | 73 | { |
8mona | 4:83f7df775d46 | 74 | motor_speed=vol_accel[accel_index] * spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 75 | } |
8mona | 4:83f7df775d46 | 76 | else |
8mona | 4:83f7df775d46 | 77 | { |
8mona | 4:83f7df775d46 | 78 | motor_speed=spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 79 | } |
8mona | 4:83f7df775d46 | 80 | strcpy(ctext," CW"); |
8mona | 4:83f7df775d46 | 81 | //ctext="CW"; |
8mona | 4:83f7df775d46 | 82 | |
8mona | 4:83f7df775d46 | 83 | } |
8mona | 4:83f7df775d46 | 84 | |
8mona | 4:83f7df775d46 | 85 | else if ( (WAIT_DELAY+ON_DURATION)<= d_Cycle.rem && d_Cycle.rem <(2*WAIT_DELAY+ON_DURATION) ) |
8mona | 3:6474ab60854e | 86 | { |
8mona | 4:83f7df775d46 | 87 | //wait_ms(20); |
8mona | 4:83f7df775d46 | 88 | int accel_index = d_Cycle.rem - (WAIT_DELAY+ON_DURATION); |
8mona | 4:83f7df775d46 | 89 | if (accel_index< DECEL_SIZE) |
8mona | 4:83f7df775d46 | 90 | { |
8mona | 4:83f7df775d46 | 91 | motor_speed=vol_decel[accel_index] * spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 92 | } |
8mona | 4:83f7df775d46 | 93 | else |
8mona | 4:83f7df775d46 | 94 | { |
8mona | 4:83f7df775d46 | 95 | motor_speed=0; |
8mona | 4:83f7df775d46 | 96 | } |
8mona | 4:83f7df775d46 | 97 | strcpy(ctext,"OFF"); |
8mona | 4:83f7df775d46 | 98 | } |
8mona | 4:83f7df775d46 | 99 | |
8mona | 4:83f7df775d46 | 100 | else if ( (2*WAIT_DELAY+ON_DURATION) <= d_Cycle.rem && d_Cycle.rem <(2*WAIT_DELAY+2* ON_DURATION) ) |
8mona | 4:83f7df775d46 | 101 | { |
8mona | 4:83f7df775d46 | 102 | int accel_index = d_Cycle.rem - (2*WAIT_DELAY+ON_DURATION); |
8mona | 4:83f7df775d46 | 103 | if (accel_index < ACCEL_SIZE) |
8mona | 4:83f7df775d46 | 104 | { |
8mona | 4:83f7df775d46 | 105 | motor_speed= - vol_accel[accel_index] * spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 106 | } |
8mona | 4:83f7df775d46 | 107 | else |
8mona | 4:83f7df775d46 | 108 | { |
8mona | 4:83f7df775d46 | 109 | motor_speed= -spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 110 | } |
8mona | 3:6474ab60854e | 111 | strcpy(ctext," CW"); |
8mona | 3:6474ab60854e | 112 | //ctext="CW"; |
8mona | 3:6474ab60854e | 113 | } |
8mona | 3:6474ab60854e | 114 | |
8mona | 4:83f7df775d46 | 115 | else if ( (2*WAIT_DELAY+2* ON_DURATION) <= d_Cycle.rem ) |
8mona | 2:ea066749e515 | 116 | { |
8mona | 3:6474ab60854e | 117 | //wait_ms(20); |
8mona | 4:83f7df775d46 | 118 | int accel_index = d_Cycle.rem - ((2*WAIT_DELAY+2* ON_DURATION)); |
8mona | 4:83f7df775d46 | 119 | if (accel_index< DECEL_SIZE) |
8mona | 4:83f7df775d46 | 120 | { |
8mona | 4:83f7df775d46 | 121 | motor_speed= -vol_decel[accel_index] * spd_table[sp_index]; |
8mona | 4:83f7df775d46 | 122 | } |
8mona | 4:83f7df775d46 | 123 | else |
8mona | 4:83f7df775d46 | 124 | { |
8mona | 4:83f7df775d46 | 125 | motor_speed=0; |
8mona | 4:83f7df775d46 | 126 | } |
8mona | 2:ea066749e515 | 127 | strcpy(ctext,"OFF"); |
8mona | 2:ea066749e515 | 128 | } |
8mona | 2:ea066749e515 | 129 | |
8mona | 2:ea066749e515 | 130 | |
8mona | 2:ea066749e515 | 131 | if(d_Cycle.quot==TOTAL_TIMES) |
8mona | 2:ea066749e515 | 132 | { |
8mona | 2:ea066749e515 | 133 | timer_.detach(); |
8mona | 2:ea066749e515 | 134 | } |
8mona | 3:6474ab60854e | 135 | |
8mona | 2:ea066749e515 | 136 | |
8mona | 2:ea066749e515 | 137 | |
MikamiUitOpen | 1:d5e9bd9b38ad | 138 | /* |
MikamiUitOpen | 0:132eb47d57fb | 139 | char str[20]; |
8mona | 2:ea066749e515 | 140 | sprintf(str, "%d'%2d\"", msec.quot, msec.rem); |
MikamiUitOpen | 0:132eb47d57fb | 141 | lcd_.WriteStringXY(str, 0, 1); |
MikamiUitOpen | 1:d5e9bd9b38ad | 142 | */ |
8mona | 2:ea066749e515 | 143 | |
8mona | 2:ea066749e515 | 144 | //1 Row |
8mona | 2:ea066749e515 | 145 | //lcd_.WriteStringXY("#",0,0); |
8mona | 3:6474ab60854e | 146 | |
8mona | 4:83f7df775d46 | 147 | lcd_.WriteValueXY("%1.1f", motor_speed, 0,0); |
8mona | 4:83f7df775d46 | 148 | lcd_.WriteValue("%5d/", d_Cycle.quot); |
8mona | 2:ea066749e515 | 149 | lcd_.WriteValue("%0d",TOTAL_TIMES); |
8mona | 4:83f7df775d46 | 150 | lcd_.WriteValue("V%0d",motor_speed); |
8mona | 3:6474ab60854e | 151 | |
8mona | 2:ea066749e515 | 152 | |
8mona | 2:ea066749e515 | 153 | //2 Row |
8mona | 2:ea066749e515 | 154 | lcd_.WriteValueXY("%03dh", t_hr, 0, 1); |
8mona | 2:ea066749e515 | 155 | lcd_.WriteValue("%02dm", d_min.rem); |
8mona | 2:ea066749e515 | 156 | lcd_.WriteValue("%03ds/", d_sec.rem); |
8mona | 2:ea066749e515 | 157 | lcd_.WriteValue("%03dh", tf_hr); |
8mona | 2:ea066749e515 | 158 | lcd_.WriteValue("%02dm", df_min.rem); |
8mona | 3:6474ab60854e | 159 | |
8mona | 2:ea066749e515 | 160 | |
8mona | 2:ea066749e515 | 161 | k++; |
MikamiUitOpen | 0:132eb47d57fb | 162 | } |
MikamiUitOpen | 0:132eb47d57fb | 163 | |
8mona | 2:ea066749e515 | 164 | |
8mona | 2:ea066749e515 | 165 | |
8mona | 2:ea066749e515 | 166 | void flip() { |
8mona | 2:ea066749e515 | 167 | static bool b = false; |
8mona | 4:83f7df775d46 | 168 | |
8mona | 2:ea066749e515 | 169 | if(b==false) |
8mona | 2:ea066749e515 | 170 | { |
8mona | 4:83f7df775d46 | 171 | timer_.attach(&TimerIsr, 0.05); |
8mona | 2:ea066749e515 | 172 | } |
8mona | 2:ea066749e515 | 173 | |
8mona | 2:ea066749e515 | 174 | else |
8mona | 2:ea066749e515 | 175 | { |
8mona | 2:ea066749e515 | 176 | timer_.detach(); |
8mona | 3:6474ab60854e | 177 | //Relay1=0; |
8mona | 3:6474ab60854e | 178 | sp_index++; |
8mona | 3:6474ab60854e | 179 | if (sp_index == 3) |
8mona | 3:6474ab60854e | 180 | { |
8mona | 3:6474ab60854e | 181 | sp_index = 0; |
8mona | 3:6474ab60854e | 182 | } |
8mona | 2:ea066749e515 | 183 | } |
8mona | 2:ea066749e515 | 184 | b=!b; |
8mona | 2:ea066749e515 | 185 | |
8mona | 2:ea066749e515 | 186 | } |
8mona | 2:ea066749e515 | 187 | |
8mona | 2:ea066749e515 | 188 | |
MikamiUitOpen | 0:132eb47d57fb | 189 | int main() |
MikamiUitOpen | 0:132eb47d57fb | 190 | { |
8mona | 3:6474ab60854e | 191 | //motor.speed(0); |
8mona | 3:6474ab60854e | 192 | // Check error and reset |
8mona | 3:6474ab60854e | 193 | |
8mona | 2:ea066749e515 | 194 | |
8mona | 3:6474ab60854e | 195 | |
8mona | 2:ea066749e515 | 196 | //LCD_cont=0; |
8mona | 3:6474ab60854e | 197 | //if (lcd_.IsConnected()) printf("\r\nConnected"); |
8mona | 3:6474ab60854e | 198 | //else printf("\r\nDisconnected"); |
MikamiUitOpen | 0:132eb47d57fb | 199 | |
MikamiUitOpen | 0:132eb47d57fb | 200 | TimerIsr(); |
8mona | 2:ea066749e515 | 201 | //timer_.attach(&TimerIsr, 0.1); |
8mona | 2:ea066749e515 | 202 | button1.fall(&flip); |
MikamiUitOpen | 0:132eb47d57fb | 203 | |
8mona | 3:6474ab60854e | 204 | bool status = motor.status(); |
8mona | 3:6474ab60854e | 205 | if (status & DRV8830_F_FAULT){ |
8mona | 3:6474ab60854e | 206 | motor.reset(); |
8mona | 3:6474ab60854e | 207 | } |
8mona | 3:6474ab60854e | 208 | |
8mona | 3:6474ab60854e | 209 | while (true) { |
8mona | 3:6474ab60854e | 210 | motor.speed(motor_speed); |
8mona | 3:6474ab60854e | 211 | } |
MikamiUitOpen | 0:132eb47d57fb | 212 | } |
MikamiUitOpen | 0:132eb47d57fb | 213 |