prime funzioni per i motori
Dependencies: X_NUCLEO_IHM12A1 mbed
Fork of HelloWorld_IHM12A1 by
Revision 5:9d031bbc6a8e, committed 2017-02-15
- Comitter:
- cpecorella
- Date:
- Wed Feb 15 10:30:38 2017 +0000
- Parent:
- 4:d6077c2bfddd
- Commit message:
- prime funzioni per i motori
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r d6077c2bfddd -r 9d031bbc6a8e main.cpp --- a/main.cpp Wed Nov 23 09:19:34 2016 +0000 +++ b/main.cpp Wed Feb 15 10:30:38 2017 +0000 @@ -1,40 +1,3 @@ -/** - ****************************************************************************** - * @file main.cpp - * @author IPC Rennes - * @version V1.0.0 - * @date April 25th, 2016 - * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM12A1 - * Motor Control Expansion Board: control of 2 Brush DC motors. - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ /* Includes ------------------------------------------------------------------*/ @@ -109,26 +72,22 @@ } } } +void go (float speed,bool direction); +void turn(bool direction); +void stop(); /* Main ----------------------------------------------------------------------*/ int main() { - uint8_t demoStep = 0; /* Printing to the console. */ printf("STARTING MAIN PROGRAM\r\n"); //----- Initialization - - /* Initializing Motor Control Component. */ - #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8) - motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A2); - #elif (defined TARGET_NUCLEO_L152RE) - motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A3); - #else + motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0); - #endif + if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); /* Set dual bridge enabled as two motors are used*/ @@ -148,125 +107,65 @@ motor->SetRefPwmFreq(0, 15000); /* Set PWM duty cycle of Ref to 60% */ - motor->SetRefPwmDc(0, 60); + motor->SetRefPwmDc(0, 60); /* Set PWM Frequency of bridge A inputs to 10000 Hz */ - motor->SetBridgeInputPwmFreq(0,10000); + motor->SetBridgeInputPwmFreq(0,10); /* Set PWM Frequency of bridge B inputs to 10000 Hz */ motor->SetBridgeInputPwmFreq(1,10000); /* Infinite Loop. */ printf("--> Infinite Loop...\r\n"); + + while (1) { - switch (demoStep) - { - case 0: - printf("STEP 0: Motor(0) FWD Speed=100%% - Motor(1) Inactive\r\n"); - /* Set speed of motor 0 to 100 % */ - motor->SetSpeed(0,100); - /* start motor 0 to run forward*/ - /* if chip is in standby mode */ - /* it is automatically awakened */ - motor->Run(0, BDCMotor::FWD); - break; - case 1: - printf("STEP 1: Motor(0) FWD Speed=75%% - Motor(1) BWD Speed=100%%\r\n"); - /* Set speed of motor 0 to 75 % */ - motor->SetSpeed(0,75); - /* Set speed of motor 1 to 100 % */ - motor->SetSpeed(1,100); - /* start motor 1 to run backward */ - motor->Run(1, BDCMotor::BWD); - break; - case 2: - printf("STEP 2: Motor(0) FWD Speed=50%% - Motor(1) BWD Speed=75%%\r\n"); - /* Set speed of motor 0 to 50 % */ - motor->SetSpeed(0,50); - /* Set speed of motor 1 to 75% */ - motor->SetSpeed(1,75); - break; - case 3: - printf("STEP 3: Motor(0) FWD Speed=25%% - Motor(1) BWD Speed=50%%\r\n"); - /* Set speed of motor 0 to 25 % */ - motor->SetSpeed(0,25); - /* Set speed of motor 1 to 50% */ - motor->SetSpeed(1,50); - break; - case 4: - printf("STEP 4: Motor(0) Stopped - Motor(1) BWD Speed=25%%\r\n"); - /* Stop Motor 0 */ - motor->HardStop(0); - /* Set speed of motor 1 to 25% */ - motor->SetSpeed(1,25); - break; - case 5: - printf("STEP 5: Motor(0) BWD Speed=25%% - Motor(1) Stopped\r\n"); - /* Set speed of motor 0 to 25 % */ - motor->SetSpeed(0,25); - /* start motor 0 to run backward */ - motor->Run(0, BDCMotor::BWD); - /* Stop Motor 1 */ - motor->HardStop(1); - break; - case 6: - printf("STEP 6: Motor(0) BWD Speed=50%% - Motor(1) FWD Speed=25%%\r\n"); - /* Set speed of motor 0 to 50 % */ - motor->SetSpeed(0,50); - /* Set speed of motor 1 to 25 % */ - motor->SetSpeed(1,25); - /* start motor 1 to run backward */ - motor->Run(1, BDCMotor::FWD); - break; - case 7: - printf("STEP 7: Motor(0) BWD Speed=75%% - Motor(1) FWD Speed=50%%\r\n"); - /* Set speed of motor 0 to 75 % */ - motor->SetSpeed(0,75); - /* Set speed of motor 1 to 50 % */ - motor->SetSpeed(1,50); - break; - case 8: - printf("STEP 8: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=75%%\r\n"); - /* Set speed of motor 0 to 100 % */ - motor->SetSpeed(0,100); - /* Set speed of motor 1 to 75 % */ - motor->SetSpeed(1,75); - break; - case 9: - printf("STEP 9: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n"); - /* Set speed of motor 1 to 100 % */ - motor->SetSpeed(1,100); - break; - case 10: - printf("STEP 10\r\n: Stop both motors and disable bridges\r\n"); - /* Stop both motors and disable bridge */ - motor->HardHiZ(0); - motor->HardHiZ(1); - break; - case 11: - printf("STEP 11: Motor(0) FWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n"); - /* Start both motors to go forward*/ - motor->Run(0,BDCMotor::FWD); - motor->Run(1,BDCMotor::FWD); - break; - case 12: - default: - printf("STEP 12: Stop both motors and enter standby mode\r\n"); - /* Stop both motors and put chip in standby mode */ - motor->Reset(); - break; + go(100,1); + + + + } +} + +void go (float speed,bool direction){ + /* direction 1 forward, 0 back*/ + motor->SetSpeed(0,speed); + motor->SetSpeed(1,speed); + + if (direction){ + motor->Run(0,BDCMotor::FWD); + motor->Run(1,BDCMotor::FWD); + } + else{ + motor->Run(0,BDCMotor::BWD); + motor->Run(1,BDCMotor::BWD); + } +} + +void stop(){ + motor->SetSpeed(0,0); + motor->SetSpeed(1,0); } - - /* Wait for 5 seconds */ - wait_ms(5000); - /* Increment demostep*/ - demoStep++; - if (demoStep > 12) - { - demoStep = 0; +void turn(bool direction){ + /*dir 1 left - 0 right */ + if (direction){ + motor->SetSpeed(0,20); + motor->SetSpeed(1,70); + + motor->Run(0,BDCMotor::FWD); + motor->Run(1,BDCMotor::FWD); + + wait_ms(1100); + } + else { + motor->SetSpeed(1,20); + motor->SetSpeed(0,70); + + motor->Run(0,BDCMotor::FWD); + motor->Run(1,BDCMotor::FWD); + + wait_ms(1100); + } } - } -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/