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 #include "mbed.h" 00002 #include "AccelStepper.h" 00003 #include "PinNames.h" 00004 #include "stdio.h" 00005 //#include <avr/wdt.h> 00006 00007 Serial pc(USBTX, USBRX); 00008 00009 00010 //Pins 00011 #define STP1_PIN 2 //AnalogOut STP_PIN(nome do pino da placa) ##DESCOMENTAR ESSE BLOCO## 00012 #define DIR1_PIN 3 // AnalogOut DIR_PIN(nome do pino da placa) 00013 /*#define ENA_PIN 2 // DigitalOut ENA_PIN(nome do pino da placa) 00014 #define HOM_PIN 12*/ // DigitalIn HOM_PIN(nome do pino da placa) 00015 00016 //AnalogOut STP_PIN(PTB0); 00017 //AnalogOut DIR_PIN(PTB1); 00018 DigitalOut ENA_PIN(P1_13); 00019 DigitalIn HOM_PIN(P0_28); 00020 00021 //Max 00022 #define MAX_ACCEL_mms2 500.0 00023 #define MAX_SPEED_mms 75.0 00024 #define MAX_POS_mm 100.0 00025 00026 //Stepper Motor parameters 00027 #define STEPS_by_REV 400.0 00028 #define mm_by_REV 25.0 00029 00030 //Home Parameters 00031 #define HOME_SPEED_mms 10.0 00032 #define HOME_INCREMENT_mm 1.0 00033 #define HOME_PRESS_POSITION_mm 2.0 00034 00035 //Consts 00036 const unsigned int MAX_ACCEL_STEPS2 = (MAX_ACCEL_mms2 * STEPS_by_REV) / mm_by_REV; 00037 unsigned int MAX_SPEED_STEPS = (MAX_SPEED_mms * STEPS_by_REV) / mm_by_REV; 00038 const unsigned int HOME_SPEED_STEPS = (HOME_SPEED_mms * STEPS_by_REV) / mm_by_REV; 00039 const unsigned int HOME_INCREMENT_STEP = (HOME_INCREMENT_mm * STEPS_by_REV) / mm_by_REV; 00040 const unsigned int HOME_PRESS_POSITION_STEP = (HOME_PRESS_POSITION_mm * STEPS_by_REV) / mm_by_REV; 00041 00042 //vars 00043 unsigned int MaxPos_step = (MAX_POS_mm * STEPS_by_REV) / mm_by_REV; 00044 bool CycleOn = true; 00045 bool HomeDone = false; 00046 00047 void MotorGoHome(AccelStepper &stpr) 00048 { 00049 int CurrentPos = 0; 00050 pc.printf("Finding Home..."); 00051 stpr.setCurrentPosition(0); 00052 stpr.setMaxSpeed(HOME_SPEED_STEPS); 00053 while (HOM_PIN == HIGH) 00054 { 00055 CurrentPos = CurrentPos - HOME_INCREMENT_STEP; 00056 stpr.runToNewPosition(CurrentPos); 00057 } 00058 stpr.setCurrentPosition(0); 00059 stpr.runToNewPosition(HOME_PRESS_POSITION_STEP); 00060 stpr.setCurrentPosition(0); 00061 HomeDone = true; 00062 pc.printf("Finding Home..."); 00063 } 00064 00065 void setup(AccelStepper &stpr) 00066 { 00067 pc.baud(250000); 00068 //pinMode(10 , OUTPUT); 00069 //pinMode(STP_PIN , OUTPUT); 00070 //pinMode(DIR_PIN , OUTPUT); 00071 //pinMode(ENA_PIN , OUTPUT); 00072 ENA_PIN = HIGH; 00073 stpr.setMaxSpeed(MAX_SPEED_STEPS); 00074 stpr.setAcceleration(MAX_ACCEL_STEPS2); 00075 MotorGoHome(stpr); 00076 stpr.setMaxSpeed(MAX_SPEED_STEPS); 00077 stpr.setAcceleration(MAX_ACCEL_STEPS2); 00078 //wdt_enable(WDTO_2S); 00079 } 00080 00081 int main(){ 00082 00083 AccelStepper stepper(2, P1_2, P0_23); 00084 00085 setup(stepper); 00086 00087 while(1){ 00088 //wdt_reset(); 00089 if (CycleOn) 00090 { 00091 if (stepper.currentPosition() == 0) 00092 { 00093 wait(500); 00094 stepper.moveTo(MaxPos_step); 00095 } 00096 else if (stepper.currentPosition() == MaxPos_step) 00097 { 00098 wait(500); 00099 stepper.moveTo(0); 00100 } 00101 } 00102 //analogWrite(10, map(stepper.currentPosition(), 0, MaxPos_step, 0, 250)); 00103 stepper.run(); 00104 if((HOM_PIN == LOW) && (HomeDone)) while(1); 00105 00106 if (pc.readable()) 00107 { 00108 char Option = pc.getc(); 00109 if (Option == 'P') 00110 { 00111 //float NewPos = serial.parsefloat(); 00112 //stepper.moveTo((NewPos * STEPS_by_REV) / mm_by_REV); 00113 } 00114 else if (Option == 'C') 00115 { 00116 CycleOn = !CycleOn ; 00117 } 00118 else if (Option == 'S') 00119 { 00120 //float NewSpeed = parsefloat(); //pc.?? 00121 //stepper.setMaxSpeed((NewSpeed * STEPS_by_REV) / mm_by_REV); 00122 } 00123 else if (Option == 'H') 00124 { 00125 MotorGoHome(stepper); 00126 } 00127 } 00128 } 00129 return 0; 00130 } 00131
Generated on Tue Jul 26 2022 00:10:10 by
1.7.2