prime funzioni per i motori

Dependencies:   X_NUCLEO_IHM12A1 mbed

Fork of HelloWorld_IHM12A1 by ST

Committer:
cpecorella
Date:
Wed Feb 15 10:30:38 2017 +0000
Revision:
5:9d031bbc6a8e
Parent:
2:1e4061cedf1d
prime funzioni per i motori

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manu_L 0:773e2a2be16f 1
Manu_L 0:773e2a2be16f 2 /* Includes ------------------------------------------------------------------*/
Manu_L 0:773e2a2be16f 3
Manu_L 0:773e2a2be16f 4 /* mbed specific header files. */
Manu_L 0:773e2a2be16f 5 #include "mbed.h"
Manu_L 0:773e2a2be16f 6
Manu_L 0:773e2a2be16f 7 /* Component specific header files. */
Manu_L 0:773e2a2be16f 8 #include "stspin240_250_class.h"
Manu_L 0:773e2a2be16f 9
Manu_L 0:773e2a2be16f 10 /* Variables -----------------------------------------------------------------*/
Manu_L 0:773e2a2be16f 11
Manu_L 0:773e2a2be16f 12 /* Initialization parameters of the motor connected to the expansion board. */
Manu_L 0:773e2a2be16f 13 Stspin240_250_Init_t initDeviceParameters =
Manu_L 0:773e2a2be16f 14 {
Manu_L 0:773e2a2be16f 15 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */
Manu_L 0:773e2a2be16f 16 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */
Manu_L 0:773e2a2be16f 17 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */
Manu_L 0:773e2a2be16f 18 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */
Manu_L 0:773e2a2be16f 19 TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */
Manu_L 0:773e2a2be16f 20 };
Manu_L 0:773e2a2be16f 21
Manu_L 0:773e2a2be16f 22 /* Motor Control Component. */
Manu_L 0:773e2a2be16f 23 STSPIN240_250 *motor;
Manu_L 0:773e2a2be16f 24
Manu_L 0:773e2a2be16f 25 /* Functions -----------------------------------------------------------------*/
Manu_L 0:773e2a2be16f 26
Manu_L 0:773e2a2be16f 27 /**
Manu_L 0:773e2a2be16f 28 * @brief This is an example of error handler.
Manu_L 0:773e2a2be16f 29 * @param[in] error Number of the error
Manu_L 0:773e2a2be16f 30 * @retval None
Manu_L 0:773e2a2be16f 31 * @note If needed, implement it, and then attach it:
Manu_L 0:773e2a2be16f 32 * + motor->AttachErrorHandler(&myErrorHandler);
Manu_L 0:773e2a2be16f 33 */
Manu_L 0:773e2a2be16f 34 void myErrorHandler(uint16_t error)
Manu_L 0:773e2a2be16f 35 {
Manu_L 0:773e2a2be16f 36 /* Printing to the console. */
Manu_L 0:773e2a2be16f 37 printf("Error %d detected\r\n\n", error);
Manu_L 0:773e2a2be16f 38
Manu_L 0:773e2a2be16f 39 /* Infinite loop */
Manu_L 0:773e2a2be16f 40 while(1)
Manu_L 0:773e2a2be16f 41 {
Manu_L 0:773e2a2be16f 42 }
Manu_L 0:773e2a2be16f 43 }
Manu_L 0:773e2a2be16f 44
Manu_L 0:773e2a2be16f 45 /**
Manu_L 0:773e2a2be16f 46 * @brief This is an example of user handler for the flag interrupt.
Manu_L 0:773e2a2be16f 47 * @param None
Manu_L 0:773e2a2be16f 48 * @retval None
Manu_L 0:773e2a2be16f 49 * @note If needed, implement it, and then attach and enable it:
Manu_L 0:773e2a2be16f 50 * + motor->AttachFlagIRQ(&myFlagIRQHandler);
Manu_L 0:773e2a2be16f 51 * + motor->EnableFlagIRQ();
Manu_L 0:773e2a2be16f 52 * To disable it:
Manu_L 0:773e2a2be16f 53 * + motor->DisbleFlagIRQ();
Manu_L 0:773e2a2be16f 54 */
Manu_L 0:773e2a2be16f 55 void myFlagIRQHandler(void)
Manu_L 0:773e2a2be16f 56 {
Manu_L 0:773e2a2be16f 57 /* Code to be customised */
Manu_L 0:773e2a2be16f 58 /************************/
Manu_L 0:773e2a2be16f 59
Manu_L 0:773e2a2be16f 60 printf(" WARNING: \"FLAG\" interrupt triggered.\r\n");
Manu_L 0:773e2a2be16f 61
Manu_L 0:773e2a2be16f 62 /* Get the state of bridge A */
Manu_L 0:773e2a2be16f 63 uint16_t bridgeState = motor->GetBridgeStatus(0);
Manu_L 0:773e2a2be16f 64
Manu_L 0:773e2a2be16f 65 if (bridgeState == 0)
Manu_L 0:773e2a2be16f 66 {
Manu_L 0:773e2a2be16f 67 if (motor->GetDeviceState(0) != INACTIVE)
Manu_L 0:773e2a2be16f 68 {
Manu_L 0:773e2a2be16f 69 /* Bridges were disabled due to overcurrent or over temperature */
Manu_L 0:773e2a2be16f 70 /* When motor was running */
Manu_L 0:773e2a2be16f 71 myErrorHandler(0XBAD0);
Manu_L 0:773e2a2be16f 72 }
Manu_L 0:773e2a2be16f 73 }
Manu_L 0:773e2a2be16f 74 }
cpecorella 5:9d031bbc6a8e 75 void go (float speed,bool direction);
cpecorella 5:9d031bbc6a8e 76 void turn(bool direction);
cpecorella 5:9d031bbc6a8e 77 void stop();
Manu_L 0:773e2a2be16f 78
Manu_L 0:773e2a2be16f 79 /* Main ----------------------------------------------------------------------*/
Manu_L 0:773e2a2be16f 80
Manu_L 0:773e2a2be16f 81 int main()
Manu_L 0:773e2a2be16f 82 {
Manu_L 0:773e2a2be16f 83
Manu_L 0:773e2a2be16f 84 /* Printing to the console. */
Manu_L 0:773e2a2be16f 85 printf("STARTING MAIN PROGRAM\r\n");
Manu_L 0:773e2a2be16f 86
Manu_L 0:773e2a2be16f 87 //----- Initialization
cpecorella 5:9d031bbc6a8e 88
Manu_L 0:773e2a2be16f 89 motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0);
cpecorella 5:9d031bbc6a8e 90
Manu_L 0:773e2a2be16f 91 if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE);
Manu_L 0:773e2a2be16f 92
Manu_L 0:773e2a2be16f 93 /* Set dual bridge enabled as two motors are used*/
Manu_L 2:1e4061cedf1d 94 motor->SetDualFullBridgeConfig(1);
Manu_L 0:773e2a2be16f 95
Manu_L 0:773e2a2be16f 96 /* Attaching and enabling an interrupt handler. */
Manu_L 0:773e2a2be16f 97 motor->AttachFlagIRQ(&myFlagIRQHandler);
Manu_L 0:773e2a2be16f 98 motor->EnableFlagIRQ();
Manu_L 0:773e2a2be16f 99
Manu_L 0:773e2a2be16f 100 /* Attaching an error handler */
Manu_L 0:773e2a2be16f 101 motor->AttachErrorHandler(&myErrorHandler);
Manu_L 0:773e2a2be16f 102
Manu_L 0:773e2a2be16f 103 /* Printing to the console. */
Manu_L 0:773e2a2be16f 104 printf("Motor Control Application Example for 2 brush DC motors\r\n");
Manu_L 0:773e2a2be16f 105
Manu_L 0:773e2a2be16f 106 /* Set PWM Frequency of Ref to 15000 Hz */
Manu_L 0:773e2a2be16f 107 motor->SetRefPwmFreq(0, 15000);
Manu_L 0:773e2a2be16f 108
Manu_L 0:773e2a2be16f 109 /* Set PWM duty cycle of Ref to 60% */
cpecorella 5:9d031bbc6a8e 110 motor->SetRefPwmDc(0, 60);
Manu_L 0:773e2a2be16f 111
Manu_L 0:773e2a2be16f 112 /* Set PWM Frequency of bridge A inputs to 10000 Hz */
cpecorella 5:9d031bbc6a8e 113 motor->SetBridgeInputPwmFreq(0,10);
Manu_L 0:773e2a2be16f 114
Manu_L 0:773e2a2be16f 115 /* Set PWM Frequency of bridge B inputs to 10000 Hz */
Manu_L 0:773e2a2be16f 116 motor->SetBridgeInputPwmFreq(1,10000);
Manu_L 0:773e2a2be16f 117
Manu_L 0:773e2a2be16f 118 /* Infinite Loop. */
Manu_L 0:773e2a2be16f 119 printf("--> Infinite Loop...\r\n");
cpecorella 5:9d031bbc6a8e 120
cpecorella 5:9d031bbc6a8e 121
Manu_L 0:773e2a2be16f 122 while (1)
Manu_L 0:773e2a2be16f 123 {
cpecorella 5:9d031bbc6a8e 124 go(100,1);
cpecorella 5:9d031bbc6a8e 125
cpecorella 5:9d031bbc6a8e 126
cpecorella 5:9d031bbc6a8e 127
cpecorella 5:9d031bbc6a8e 128 }
cpecorella 5:9d031bbc6a8e 129 }
cpecorella 5:9d031bbc6a8e 130
cpecorella 5:9d031bbc6a8e 131 void go (float speed,bool direction){
cpecorella 5:9d031bbc6a8e 132 /* direction 1 forward, 0 back*/
cpecorella 5:9d031bbc6a8e 133 motor->SetSpeed(0,speed);
cpecorella 5:9d031bbc6a8e 134 motor->SetSpeed(1,speed);
cpecorella 5:9d031bbc6a8e 135
cpecorella 5:9d031bbc6a8e 136 if (direction){
cpecorella 5:9d031bbc6a8e 137 motor->Run(0,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 138 motor->Run(1,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 139 }
cpecorella 5:9d031bbc6a8e 140 else{
cpecorella 5:9d031bbc6a8e 141 motor->Run(0,BDCMotor::BWD);
cpecorella 5:9d031bbc6a8e 142 motor->Run(1,BDCMotor::BWD);
cpecorella 5:9d031bbc6a8e 143 }
cpecorella 5:9d031bbc6a8e 144 }
cpecorella 5:9d031bbc6a8e 145
cpecorella 5:9d031bbc6a8e 146 void stop(){
cpecorella 5:9d031bbc6a8e 147 motor->SetSpeed(0,0);
cpecorella 5:9d031bbc6a8e 148 motor->SetSpeed(1,0);
Manu_L 0:773e2a2be16f 149 }
Manu_L 0:773e2a2be16f 150
cpecorella 5:9d031bbc6a8e 151 void turn(bool direction){
cpecorella 5:9d031bbc6a8e 152 /*dir 1 left - 0 right */
cpecorella 5:9d031bbc6a8e 153 if (direction){
cpecorella 5:9d031bbc6a8e 154 motor->SetSpeed(0,20);
cpecorella 5:9d031bbc6a8e 155 motor->SetSpeed(1,70);
cpecorella 5:9d031bbc6a8e 156
cpecorella 5:9d031bbc6a8e 157 motor->Run(0,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 158 motor->Run(1,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 159
cpecorella 5:9d031bbc6a8e 160 wait_ms(1100);
cpecorella 5:9d031bbc6a8e 161 }
cpecorella 5:9d031bbc6a8e 162 else {
cpecorella 5:9d031bbc6a8e 163 motor->SetSpeed(1,20);
cpecorella 5:9d031bbc6a8e 164 motor->SetSpeed(0,70);
cpecorella 5:9d031bbc6a8e 165
cpecorella 5:9d031bbc6a8e 166 motor->Run(0,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 167 motor->Run(1,BDCMotor::FWD);
cpecorella 5:9d031bbc6a8e 168
cpecorella 5:9d031bbc6a8e 169 wait_ms(1100);
cpecorella 5:9d031bbc6a8e 170 }
Manu_L 0:773e2a2be16f 171 }