Team DIANA / Mbed OS DC_Serial

Dependencies:   X_NUCLEO_IHM04A1 arm_linear_can_2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "L6206.h"
00003 #include "BDCMotor.h"
00004 
00005 #define TX PC_10
00006 #define RX PC_11
00007 
00008 
00009 Serial SERIAL(D1, D0)
00010 
00011 static volatile uint16_t gLastError;
00012 static volatile uint8_t gStep = 0;
00013 
00014 L6206_init_t init =
00015 {
00016     L6206_CONF_PARAM_PARALLE_BRIDGES,                                                    
00017     {L6206_CONF_PARAM_FREQ_PWM1A, L6206_CONF_PARAM_FREQ_PWM2A, L6206_CONF_PARAM_FREQ_PWM1B, L6206_CONF_PARAM_FREQ_PWM2B},
00018     {100,100,100,100},
00019     {FORWARD,BACKWARD,FORWARD,BACKWARD},
00020     {INACTIVE,INACTIVE,INACTIVE,INACTIVE},
00021     {FALSE,FALSE}
00022 };
00023 
00024 //  Motor definition
00025 L6206 *LinAct;
00026 L6206 *EndEff;
00027 
00028 int speed_elbow = 0;
00029 int speed_ee = 0;
00030 
00031 /*********************************/
00032 /*      Interrupt Handlers       */
00033 /*********************************/
00034 
00035 //  Error Handler (called by the library when it reports an error)
00036 void my_error_handler(uint16_t error)
00037 {
00038   /* Backup error number */
00039   gLastError = error;
00040   
00041   /* Enter your own code here */
00042 }
00043 
00044 //  Flag Handler (overcurrent and thermal alarms reporting)
00045 void my_flag_irq_handler(void)
00046 {
00047     /* Get the state of bridge A */
00048     uint16_t bridgeState  = EndEff->get_bridge_status(0);
00049 
00050     if (bridgeState == 0) 
00051     {
00052         if ((EndEff->get_device_state(0) != INACTIVE)||
00053             (EndEff->get_device_state(1) != INACTIVE)) 
00054         {
00055             /* Bridge A was disabling due to overcurrent or over temperature */
00056             /* When at least on of its  motor was running */
00057             my_error_handler(0XBAD0);
00058         }
00059     }
00060   
00061     /* Get the state of bridge B */
00062     bridgeState  = LinAct->get_bridge_status(1);
00063   
00064     if (bridgeState == 0)  
00065     {
00066         if ((LinAct->get_device_state(2) != INACTIVE)||
00067             (LinAct->get_device_state(3) != INACTIVE)) 
00068         {
00069             /* Bridge A was disabling due to overcurrent or over temperature */
00070             /* When at least on of its  motor was running */
00071             my_error_handler(0XBAD1);
00072         }
00073     }  
00074 }
00075 
00076 /*****************************************************************************/
00077 
00078 void rx()
00079 {
00080         
00081 }
00082 
00083 /*****************************/
00084 /*            MAIN           */
00085 /*****************************/
00086 
00087 int main()
00088 {
00089 
00090 //  Motor Initialization   
00091 #ifdef TARGET_STM32F429
00092     LinAct = new L6206(PB_14, PB_15, PA_8, PA_9, PC_6, PC_7);     // EN_A, EN_B, IN1_A, IN2_A, IN1_B, IN2_B
00093     EndEff = new L6206(PB_14, PB_15, PA_8, PA_9, PC_6, PC_7);     // EN_A, EN_B, IN1_A, IN2_A, IN1_B, IN2_B
00094 #else
00095     //LinAct = new L6206(PB_14, PB_15, PA_8, PA_9, PC_6, PC_7);
00096     LinAct = new L6206(D2, A4, D5, D4, A0, A1);
00097     EndEff = new L6206(D2, A4, D5, D4, A0, A1);
00098 #endif
00099 
00100     if (LinAct->init(&init) != COMPONENT_OK) 
00101     {
00102         printf("ERROR: vvMotor Init\n\r");
00103         exit(EXIT_FAILURE);
00104     }
00105     if (EndEff->init(&init) != COMPONENT_OK) 
00106     {
00107         printf("ERROR: vvMotor Init\n\r");
00108         exit(EXIT_FAILURE);
00109     }
00110     
00111     LinAct->attach_flag_interrupt(my_flag_irq_handler);
00112     LinAct->attach_error_handler(my_error_handler);
00113     EndEff->attach_flag_interrupt(my_flag_irq_handler);
00114     EndEff->attach_error_handler(my_error_handler);
00115     printf("DONE: Motor Init\n\r");
00116     
00117     LinAct->set_dual_full_bridge_config(PARALLELING_NONE___1_BIDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B);
00118     EndEff->set_dual_full_bridge_config(PARALLELING_NONE___1_BIDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B);
00119 
00120     while(true)
00121     {            
00122         EndEff->set_speed(0, speed_ee);
00123         if(speed_ee < 0)
00124             EndEff->run(0, BDCMotor::BWD);
00125         else if(speed_ee > 0)
00126             EndEff->run(0, BDCMotor::FWD);
00127         else if(speed_ee == 0)
00128             EndEff->hard_stop(0);
00129             
00130         LinAct->set_speed(1, speed_elbow);
00131         if(speed_elbow < 0)
00132             LinAct->run(1, BDCMotor::BWD);
00133         else if(speed_elbow > 0)
00134             LinAct->run(1, BDCMotor::FWD);
00135         else if(speed_elbow == 0)
00136             LinAct->hard_stop(1);
00137           
00138         osDelay(100);
00139     }
00140 }