Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 /* Includes ------------------------------------------------------------------*/ 00003 00004 /* mbed specific header files. */ 00005 #include "mbed.h" 00006 00007 /* Helper header files. */ 00008 #include "DevSPI.h" 00009 00010 /* Component specific header files. */ 00011 #include "L6474.h" 00012 00013 00014 /* Variables -----------------------------------------------------------------*/ 00015 00016 /* Initialization parameters. */ 00017 L6474_init_t init = { 00018 160, /* Acceleration rate in pps^2. Range: (0..+inf). */ 00019 160, /* Deceleration rate in pps^2. Range: (0..+inf). */ 00020 1600, /* Maximum speed in pps. Range: (30..10000]. */ 00021 800, /* Minimum speed in pps. Range: [30..10000). */ 00022 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ 00023 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ 00024 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ 00025 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ 00026 L6474_STEP_SEL_1_16, /* Step selection (STEP_SEL field of STEP_MODE register). */ 00027 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ 00028 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ 00029 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ 00030 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ 00031 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ 00032 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ 00033 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ 00034 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ 00035 L6474_ALARM_EN_OVERCURRENT | 00036 L6474_ALARM_EN_THERMAL_SHUTDOWN | 00037 L6474_ALARM_EN_THERMAL_WARNING | 00038 L6474_ALARM_EN_UNDERVOLTAGE | 00039 L6474_ALARM_EN_SW_TURN_ON | 00040 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ 00041 }; 00042 00043 /* Motor Control Component. */ 00044 L6474 *motor; 00045 00046 00047 /* Functions -----------------------------------------------------------------*/ 00048 00049 /** 00050 * @brief This is an example of user handler for the flag interrupt. 00051 * Empty parts can be implemented by the user upon needs. 00052 * @param None. 00053 * @retval None. 00054 * @note If needed, implement it, and then attach and enable it: 00055 * + motor->AttachFlagIRQ(&FlagIRQHandler); 00056 * + motor->EnableFlagIRQ(); 00057 * To disable it: 00058 * + motor->DisbleFlagIRQ(); 00059 */ 00060 void FlagIRQHandler(void) 00061 { 00062 /* Set ISR flag. */ 00063 motor->isr_flag = TRUE; 00064 00065 /* Get the value of the status register. */ 00066 unsigned int status = motor->get_status(); 00067 00068 /* Check HIZ flag: if set, power brigdes are disabled. */ 00069 if ((status & L6474_STATUS_HIZ) == L6474_STATUS_HIZ) 00070 { /* HIZ state. Action to be customized. */ } 00071 00072 /* Check direction. */ 00073 if ((status & L6474_STATUS_DIR) == L6474_STATUS_DIR) 00074 { /* Forward direction is set. Action to be customized. */ } 00075 else 00076 { /* Backward direction is set. Action to be customized. */ } 00077 00078 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ 00079 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ 00080 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) 00081 { /* Command received by SPI can't be performed. Action to be customized. */ } 00082 00083 /* Check WRONG_CMD flag: if set, the command does not exist. */ 00084 if ((status & L6474_STATUS_WRONG_CMD) == L6474_STATUS_WRONG_CMD) 00085 { /* The command received by SPI does not exist. Action to be customized. */ } 00086 00087 /* Check UVLO flag: if not set, there is an undervoltage lock-out. */ 00088 if ((status & L6474_STATUS_UVLO) == 0) 00089 { /* Undervoltage lock-out. Action to be customized. */ } 00090 00091 /* Check TH_WRN flag: if not set, the thermal warning threshold is reached. */ 00092 if ((status & L6474_STATUS_TH_WRN) == 0) 00093 { /* Thermal warning threshold is reached. Action to be customized. */ } 00094 00095 /* Check TH_SHD flag: if not set, the thermal shut down threshold is reached. */ 00096 if ((status & L6474_STATUS_TH_SD) == 0) 00097 { /* Thermal shut down threshold is reached. Action to be customized. */ } 00098 00099 /* Check OCD flag: if not set, there is an overcurrent detection. */ 00100 if ((status & L6474_STATUS_OCD) == 0) 00101 { /* Overcurrent detection. Action to be customized. */ } 00102 00103 /* Reset ISR flag. */ 00104 motor->isr_flag = FALSE; 00105 } 00106 00107 /** 00108 * @brief This is an example of user handler for the errors. 00109 * @param error error-code. 00110 * @retval None 00111 * @note If needed, implement it, and then attach it: 00112 * + motor->AttachErrorHandler(&ErrorHandler); 00113 */ 00114 void ErrorHandler(uint16_t error) 00115 { 00116 /* Printing to the console. */ 00117 printf("Error: %d.\r\n", error); 00118 00119 /* Aborting the program. */ 00120 exit(EXIT_FAILURE); 00121 } 00122 00123 00124 /* Main ----------------------------------------------------------------------*/ 00125 00126 int main() 00127 { 00128 /*----- Initialization. -----*/ 00129 00130 /* Initializing SPI bus. */ 00131 DevSPI dev_spi(D11, D12, D13); 00132 00133 /* Initializing Motor Control Component. */ 00134 motor = new L6474(D2, D8, D7, D9, D10, dev_spi); 00135 if (motor->init(&init) != COMPONENT_OK) { 00136 exit(EXIT_FAILURE); 00137 } 00138 00139 /* Attaching and enabling the user handler for the flag interrupt. */ 00140 motor->attach_flag_irq(&FlagIRQHandler); 00141 motor->enable_flag_irq(); 00142 00143 /* Printing to the console. */ 00144 printf("Stepper Motor Control with Joystick Example\r\n\n"); 00145 00146 int speed; 00147 //////////////////////////////////////////////////////////////////////////////// 00148 int a,b,c,d,e,f,k; 00149 int i; 00150 int x,y; 00151 float u,w; 00152 00153 //DigitalIn botao_A(D2); 00154 DigitalIn botao_B(D3); 00155 DigitalIn botao_C(D4); 00156 DigitalIn botao_D(D5); 00157 DigitalIn botao_E(D6); 00158 //DigitalIn botao_F(D7); 00159 //DigitalIn botao_K(D8); 00160 00161 AnalogIn eixo_X(A0); 00162 AnalogIn eixo_Y(A1); 00163 00164 DigitalOut myled(LED1); 00165 00166 Serial pc(USBTX, USBRX); 00167 00168 pc.baud(9600); 00169 00170 a=b=c=d=e=f=k=0; //a=0,b=0,c=0,d=0,e=0,f=0,k=0; 00171 //////////////////////////////////////////////////////////////////////////////// 00172 00173 /*----- Changing step mode to full step mode. -----*/ 00174 00175 /* Printing to the console. */ 00176 printf("--> Changing step mode to full step mode.\r\n"); 00177 00178 /* Selecting full step mode. */ 00179 if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) { 00180 printf(" Step Mode not allowed.\r\n"); 00181 } 00182 00183 /* Setting speed and acceleration to be consistent with full step mode. */ 00184 motor->set_max_speed(1000); 00185 motor->set_min_speed(50); 00186 motor->set_acceleration(10); 00187 motor->set_deceleration(10); 00188 00189 } 00190 ///////////////////////////////////////////////////////////////////////////////////// 00191 00192 ------------------------------------------------------------------------------*/ 00193 00194 00195 //cls(); 00196 pc.printf("\f\n\r\t\t\t\t\t\tHello Analog World!!!"); 00197 pc.printf("\n\rStart\tA=%d,\tB=%d,\tC=%d,\tD=%d,\tE=%d,\tF=%d,\tK=%d,\tEixos<int>\tEixos<float>\tEixos<status>",a,b,c,d,e,f,k); 00198 pc.printf("\n\rBotao\tA\tB\tC\tD\tE\tF\tK\tx\ty\tX\tY\n"); 00199 00200 00201 while(1) { 00202 00203 if (botao_A == 0) { // Botão usuário pressionado 00204 a++; 00205 } 00206 if (botao_B == 0) { // Botão externo pressionado 00207 b++; 00208 } 00209 if (botao_C == 0) { // Botão usuário pressionado 00210 c++; 00211 } 00212 if (botao_D == 0) { // Botão usuário pressionado 00213 d++; 00214 } 00215 if (botao_E == 0) { // Botão usuário pressionado 00216 e++; 00217 } 00218 if (botao_F == 0) { // Botão usuário pressionado 00219 f++; 00220 } 00221 if (botao_K == 0) { // Botão usuário pressionado 00222 k++; 00223 } 00224 //x=(int)eixo_X.read(); 00225 u=eixo_X.read(); 00226 //x=(int)u; 00227 x=eixo_X.read()*255; //Converte uma leitura do ADC em float para formato int e armazena na variável y. 00228 00229 //w=eixo_Y.read(); 00230 00231 //y=eixo_Y.read()*255; //Converte uma leitura do ADC em float para formato int e armazena na variável y. 00232 00233 pc.printf("\rValor\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%X \t%X \t%2.3f\t%2.3f",a,b,c,d,e,f,k,x,y,eixo_X.read(),eixo_Y.read()); 00234 00235 //Detecta a faixa em que o cursor do pot se encontra: 00236 if (u>0.505){pc.printf(" X FWD "); 00237 /* Setting the speed. */ 00238 motor->set_max_speed(x); 00239 /* Requesting to run Forward. */ 00240 motor->run(StepperMotor::FWD);} //pot > 0.505V (comanda acionamento X proporcional para FRENTE); 00241 else{if (u<0.495) 00242 {pc.printf(" X BWD "); 00243 /* Setting the speed. */ 00244 motor->set_max_speed(x); 00245 /* Requesting to run Backward. */ 00246 motor->run(StepperMotor::BWD);} //pot < 0.495V (comanda acionamento X proporcional para TRAS); 00247 else{pc.printf(" X STOP"); //0.495V < pot < 0.505V (comanda acionamento X para permanecer PARADO). 00248 /* Requesting to immediatly stop. */ 00249 motor->hard_stop();} 00250 }; 00251 00252 wait(0.2); 00253 } 00254 } 00255
Generated on Mon Aug 15 2022 08:17:44 by
1.7.2