Theis Rotating Platform Demo code

Dependencies:   X_NUCLEO_IHM01A1_Demo_Code mbed

Fork of Demo_IHM01A1_3-Motors by Arkadi Rafalovich

main.cpp

Committer:
Arkadi
Date:
2016-02-25
Revision:
21:ed054abddfe4
Parent:
17:aae1446c67f4
Child:
23:f9d35a756e79

File content as of revision 21:ed054abddfe4:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  Davide Aliprandi, STMicroelectronics
 * @version V1.0.0
 * @date    October 14th, 2015
 * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
 *          Motor Control Expansion Board: control of 2 motors.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2015 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 ------------------------------------------------------------------*/

/* mbed specific header files. */
#include "mbed.h"

/* Helper header files. */
#include "DevSPI.h"

/* Component specific header files. */
#include "l6474_class.h"


/* Definitions ---------------------------------------------------------------*/

/* Number of steps to move. */
#define STEPS 3200


/* Variables -----------------------------------------------------------------*/

/* Motor Control Component. */
L6474 *motor1;
L6474 *motor2;
L6474 *motor3;

/* Main ----------------------------------------------------------------------*/

int main()
{
    /*----- Initialization. -----*/

    /* Initializing SPI bus. */
    DevSPI dev_spi(D11, D12, D13);

    /* Initializing Motor Control Components. */
    motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
    motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
    motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
    if (motor1->Init() != COMPONENT_OK)
        exit(EXIT_FAILURE);
    if (motor2->Init() != COMPONENT_OK)
        exit(EXIT_FAILURE);
    if (motor3->Init() != COMPONENT_OK)
        exit(EXIT_FAILURE);



    /*----- Changing motor setting. -----*/
    
    /* Setting High Impedance State to update L6474's registers. */
    motor1->SoftHiZ();
    motor2->SoftHiZ();
    motor3->SoftHiZ();
    // Disabling motor
    motor1->Disable();
    motor2->Disable();
    motor3->Disable();
    /* Changing step mode. */
    motor1->SetStepMode(STEP_MODE_1_16);
    motor2->SetStepMode(STEP_MODE_1_16);
    motor3->SetStepMode(STEP_MODE_1_16);
    /* Increasing the torque regulation current to 500mA. */
    motor1->SetParameter(L6474_TVAL, 1250); // Limit 2.0A
    motor2->SetParameter(L6474_TVAL, 1700); // Limit 1.7A
    motor3->SetParameter(L6474_TVAL, 850);  // Limit 1.0A
    
    /* Max speed to 2400 step/s. */
    motor1->SetMaxSpeed(1000);
    motor2->SetMaxSpeed(1000);
    motor3->SetMaxSpeed(1000);

    /* Min speed to 200 step/s. */
    motor1->SetMinSpeed(100);
    motor2->SetMinSpeed(100);
    motor3->SetMinSpeed(100);
    
    /* set accelerations */
    motor1->SetAcceleration(500);
    motor2->SetAcceleration(500);
    motor3->SetAcceleration(500);
    motor1->SetDeceleration(500);
    motor2->SetDeceleration(500);
    motor3->SetDeceleration(500);
    
    // Enabling motor
    motor1->Enable();
    motor2->Enable();
    motor3->Enable();
    

    /* Printing to the console. */
    printf("Motor Control Application Example for 3 Motors\r\n\n");

    /*----- Moving. -----*/
    
    /* Moving N steps in the forward direction. */
    motor1->Move(StepperMotor::FWD, STEPS);
    motor2->Move(StepperMotor::FWD, STEPS);
    motor3->Move(StepperMotor::FWD, STEPS);
    /* Waiting while the motor is active. */
    motor1->WaitWhileActive();
    motor2->WaitWhileActive();
    motor3->WaitWhileActive();
 
     /* Waiting 2 seconds. */
    wait_ms(2000);
 
    
    /* Moving N steps in the backward direction. */
    motor1->Move(StepperMotor::BWD, STEPS);
    motor2->Move(StepperMotor::BWD, STEPS);
    motor3->Move(StepperMotor::BWD, STEPS);
    /* Waiting while the motor is active. */
    motor1->WaitWhileActive();
    motor2->WaitWhileActive();
    motor3->WaitWhileActive();
    
    /* Waiting 2 seconds. */
    wait_ms(2000);

    /* Infinite Loop. */
    while(1)
    {
            /*----- Moving. -----*/
    
    /* Moving N steps in the forward direction. */
    motor1->Move(StepperMotor::FWD, STEPS);
    motor2->Move(StepperMotor::FWD, STEPS);
    motor3->Move(StepperMotor::FWD, STEPS);
    /* Waiting while the motor is active. */
    motor1->WaitWhileActive();
    motor2->WaitWhileActive();
    motor3->WaitWhileActive();
    
    /* Moving N steps in the backward direction. */
    motor1->Move(StepperMotor::BWD, STEPS);
    motor2->Move(StepperMotor::BWD, STEPS);
    motor3->Move(StepperMotor::BWD, STEPS);
    /* Waiting while the motor is active. */
    motor1->WaitWhileActive();
    motor2->WaitWhileActive();
    motor3->WaitWhileActive();
    
    /* Waiting 2 seconds. */
    wait_ms(2000);
    }
}