Thomas Byrne / Mbed 2 deprecated HelloWorld_IHM02A1

Dependencies:   mbed tinyshell X_NUCLEO_IHM02A1

main.cpp

Committer:
tom_astranis
Date:
2021-04-01
Revision:
28:19b25daa7777
Parent:
27:2abcf13d90a3
Child:
29:a510e875936d

File content as of revision 28:19b25daa7777:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  Davide Aliprandi, STMicroelectronics
 * @version V1.0.0
 * @date    November 4th, 2015
 * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM02A1
 *          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"

/* Expansion Board specific header files. */
#include "XNucleoIHM02A1.h"

/* Tinyshell: https://os.mbed.com/users/murilopontes/code/tinyshell/ */
#include "tinysh.h"

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

/* Motor specs */
#define MOTOR_SUPPLY_VOLTAGE_V      12.0
#define STEPS_PER_REV               200.0
#define MAX_PHASE_CURRENT_A         0.67
#define PHASE_RES_OHMS              12.0
#define MOTOR_INITIAL_SPEED_SPS     1000.0
#define MOTOR_ACCEL_SPS2            200.0
#define MOTOR_MAX_SPEED_SPS         2000.0
#define FULL_STEP_TH_SPS            602.7
#define DUMMY_KVAL_V                3.06
#define BEMF_ICPT_SPS               61.52
#define START_SLOPE                 392.1569e-6
#define FINAL_SLOPE                 643.1372e-6
#define OCD_TH_MA                   600.0
#define STALL_TH_MA                 1000.0

/* Behavioral stuff */
#define STATUS_LOG_RATE_HZ 1
#define MIN_LOOP_TIME_MS 1
#define HARD_STOP_WAIT_MS 100

#define CMD_BUFFER_SIZE 256
#define OUTPUT_BUFFER_SIZE 1024

/* Number of steps. */
#define STEPS_1 (20000 * 1)   /* 1 revolution given a 200 step motor and 100:1 gearbox, with full stepping */
#define STEPS_2 (STEPS_1 * 2)
#define SPEED_SPS 10000 // steps per second

/* Number of movements per revolution. */
#define MPR_1 4

/* Delay in milliseconds. */
#define DELAY_1 1000
#define DELAY_2 2000
#define DELAY_3 5000


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

/* Motor Control Expansion Board. */
XNucleoIHM02A1 *x_nucleo_ihm02a1;

/* Initialization parameters of the motors connected to the expansion board. */
L6470_init_t init[L6470DAISYCHAINSIZE] = {
    /* First Motor. */
    {
        MOTOR_SUPPLY_VOLTAGE_V,                  /* Motor supply voltage in V. */
        STEPS_PER_REV,                           /* Min number of steps per revolution for the motor. */
        MAX_PHASE_CURRENT_A,                     /* Max motor phase voltage in A. */
        MAX_PHASE_CURRENT_A * PHASE_RES_OHMS,    /* Max motor phase voltage in V. (12 ohms per phase) */
        MOTOR_INITIAL_SPEED_SPS,                 /* Motor initial speed [step/s]. */
        MOTOR_ACCEL_SPS2,                        /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        MOTOR_ACCEL_SPS2,                        /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        MOTOR_MAX_SPEED_SPS,                     /* Motor maximum speed [step/s]. */
        0.0,                                     /* Motor minimum speed [step/s]. */
        FULL_STEP_TH_SPS,                        /* Motor full-step speed threshold [step/s]. */
        DUMMY_KVAL_V,                            /* Holding kval [V]. */
        DUMMY_KVAL_V,                            /* Constant speed kval [V]. */
        DUMMY_KVAL_V,                            /* Acceleration starting kval [V]. */
        DUMMY_KVAL_V,                            /* Deceleration starting kval [V]. */
        BEMF_ICPT_SPS,                           /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        START_SLOPE,                             /* Start slope [s/step]. */
        FINAL_SLOPE,                             /* Acceleration final slope [s/step]. */
        FINAL_SLOPE,                             /* Deceleration final slope [s/step]. */
        0,                                       /* Thermal compensation factor (range [0, 15]). */
        OCD_TH_MA,                               /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        STALL_TH_MA,                             /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_FULL,            /* Step mode selection. */
        0xFF,                                    /* Alarm conditions enable. */
        0x2E88                                   /* Ic configuration. */
    },

    /* Second Motor. */
    {
        MOTOR_SUPPLY_VOLTAGE_V,                  /* Motor supply voltage in V. */
        STEPS_PER_REV,                           /* Min number of steps per revolution for the motor. */
        MAX_PHASE_CURRENT_A,                     /* Max motor phase voltage in A. */
        MAX_PHASE_CURRENT_A * PHASE_RES_OHMS,    /* Max motor phase voltage in V. (12 ohms per phase) */
        MOTOR_INITIAL_SPEED_SPS,                 /* Motor initial speed [step/s]. */
        MOTOR_ACCEL_SPS2,                        /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        MOTOR_ACCEL_SPS2,                        /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        MOTOR_MAX_SPEED_SPS,                     /* Motor maximum speed [step/s]. */
        0.0,                                     /* Motor minimum speed [step/s]. */
        FULL_STEP_TH_SPS,                        /* Motor full-step speed threshold [step/s]. */
        DUMMY_KVAL_V,                            /* Holding kval [V]. */
        DUMMY_KVAL_V,                            /* Constant speed kval [V]. */
        DUMMY_KVAL_V,                            /* Acceleration starting kval [V]. */
        DUMMY_KVAL_V,                            /* Deceleration starting kval [V]. */
        BEMF_ICPT_SPS,                           /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        START_SLOPE,                             /* Start slope [s/step]. */
        FINAL_SLOPE,                             /* Acceleration final slope [s/step]. */
        FINAL_SLOPE,                             /* Deceleration final slope [s/step]. */
        0,                                       /* Thermal compensation factor (range [0, 15]). */
        OCD_TH_MA,                               /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        STALL_TH_MA,                             /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_FULL,            /* Step mode selection. */
        0xFF,                                    /* Alarm conditions enable. */
        0x2E88                                   /* Ic configuration. */
    }
};

/* Serial Port for console */
Serial pc(USBTX, USBRX);

/* Nasty globals haha */
bool limit_flag = false;
bool status_flag = false;
char cmd_buffer[CMD_BUFFER_SIZE];
char output_buffer[OUTPUT_BUFFER_SIZE];

/* Tinyshell command handler functions */
void print_status(int argc, char **argv)
{
    status_flag = true;
}

// parent cmd (0 for top) cmd input name, help string, usage string, 
//   function to launch, arg when called, next (0 at init), child (0 at init)
tinysh_cmd_t print_status_cmd = {0, "status", "status command", "[args]", print_status,0,0,0};

/* mandatory tiny shell output function */
void tinysh_char_out(unsigned char c)
{
    pc.putc(c);
}

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

int main()
{
    /*----- Initialization. -----*/
    unsigned int status_bytes[L6470DAISYCHAINSIZE];
    
    /* Set up tinyshell */
    pc.baud(115200);
    pc.printf("Motor controller ready\r\n");
    tinysh_set_prompt("$ ");
    
    // Add all tinyshell commands here
    tinysh_add_command(&print_status_cmd);

    /* Initializing SPI bus. */
#ifdef TARGET_STM32F429
    DevSPI dev_spi(D11, D12, D13);
#else
    DevSPI dev_spi(D11, D12, D3);
#endif

    /* Initializing Motor Control Expansion Board. */
    x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);

    /* Building a list of motor control components. */
    L6470 **motors = x_nucleo_ihm02a1->get_components();

    // Hello world: just run the damn motor forever
    printf("Running.\r\n");
    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
        motors[m]->prepare_run(StepperMotor::BWD, SPEED_SPS);
    }
    x_nucleo_ihm02a1->perform_prepared_actions();
    
    
    // Main loop
    while(1) {
        /* 1: Check for hardware flags ----------------------------------------*/
        if (limit_flag) {
            // Hard stop 
            for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
                motors[m]->prepare_hard_stop();
            }
            x_nucleo_ihm02a1->perform_prepared_actions();
            
            wait_ms(HARD_STOP_WAIT_MS);
            
            // High Z
            for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
                motors[m]->prepare_hard_hiz();
            }
            x_nucleo_ihm02a1->perform_prepared_actions();
        
            printf("Reached limit.\r\n");
            limit_flag = false;
        }
        
        /* 2: Fetch new chars for Tinyshell ----------------------------------*/
        tinysh_char_in(pc.getc());
        
        /* 3: Handle Commands ------------------------------------------------*/
        if (status_flag) {
            // Fetch and parse the status register for each motor
            for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
                status_bytes[m] = motors[m]->get_status();
                printf("Status reg %d: 0x%02X\r\n", m, status_bytes[m]);
                printf("    STEP-CLOCK MODE:       ");
                printf(status_bytes[m] & 0x8000 ? "SET\r\n" : "NOT SET\r\n");
                printf("    STEP_LOSS_B:           ");
                printf(status_bytes[m] & 0x4000 ? "SET\r\n" : "NOT SET\r\n");
                printf("    STEP_LOSS_A:           ");
                printf(status_bytes[m] & 0x2000 ? "SET\r\n" : "NOT SET\r\n");
                printf("    OVERCURRENT DETECT:    ");
                printf(status_bytes[m] & 0x1000 ? "SET\r\n" : "NOT SET\r\n");
                printf("    THERMAL SHUTDOWN:      ");
                printf(status_bytes[m] & 0x0800 ? "SET\r\n" : "NOT SET\r\n");
                printf("    THERMAL WARN:          ");
                printf(status_bytes[m] & 0x0400 ? "SET\r\n" : "NOT SET\r\n");
                printf("    UNDERVOLTAGE LOCKOUT:  ");
                printf(status_bytes[m] & 0x0200 ? "SET\r\n" : "NOT SET\r\n");
                printf("    WRONG_CMD:             ");
                printf(status_bytes[m] & 0x0100 ? "SET\r\n" : "NOT SET\r\n");
                printf("    NOTPERF_CMD:           ");
                printf(status_bytes[m] & 0x0080 ? "SET\r\n" : "NOT SET\r\n");
                printf("    MOTOR_STATUS:          ");
                if ((status_bytes[m] && 0x0060) >> 5 == 0x00) printf("STOPPED\r\n");
                if ((status_bytes[m] && 0x0060) >> 5 == 0x01) printf("ACCELERATING\r\n");
                if ((status_bytes[m] && 0x0060) >> 5 == 0x10) printf("DECELERATING\r\n");
                if ((status_bytes[m] && 0x0060) >> 5 == 0x11) printf("CONSTANT SPEED\r\n");
                printf("    DIRECTION:             ");
                printf(status_bytes[m] & 0x0010 ? "FWD\r\n" : "REV\r\n");
                printf("    SWITCH TURN-ON EVENT:  ");
                printf(status_bytes[m] & 0x0008 ? "SET\r\n" : "NOT SET\r\n");
                printf("    SWITCH STATUS:         ");
                printf(status_bytes[m] & 0x0004 ? "CLOSED\r\n" : "OPEN\r\n");
                printf("    BUSY:                  ");
                printf(status_bytes[m] & 0x0002 ? "SET\r\n" : "NOT SET\r\n");
                printf("    HI_Z:                  ");
                printf(status_bytes[m] & 0x0001 ? "SET\r\n" : "NOT SET\r\n");
                printf("\n\n");
            }
        }
        
    } // end main loop
}


//    /* Printing to the console. */
//    printf("--> Setting home position.\r\n");
//
//    /* Setting the home position. */
//    motors[0]->set_home();

//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Getting the current position. */
//    int position = motors[0]->get_position();
//
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);

//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Moving forward %d steps.\r\n", STEPS_1);
//
//    /* Moving. */
//    motors[0]->move(StepperMotor::FWD, STEPS_1);
//
//    /* Waiting while active. */
//    motors[0]->wait_while_active();
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Printing to the console. */
//    printf("--> Marking the current position.\r\n");
//
//    /* Marking the current position. */
//    motors[0]->set_mark();
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Moving backward %d steps.\r\n", STEPS_2);
//
//    /* Moving. */
//    motors[0]->move(StepperMotor::BWD, STEPS_2);
//
//    /* Waiting while active. */
//    motors[0]->wait_while_active();
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Going to marked position.\r\n");
//
//    /* Going to marked position. */
//    motors[0]->go_mark();
//    
//    /* Waiting while active. */
//    motors[0]->wait_while_active();
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Going to home position.\r\n");
//
//    /* Going to home position. */
//    motors[0]->go_home();
//    
//    /* Waiting while active. */
//    motors[0]->wait_while_active();
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Halving the microsteps.\r\n");
//
//    /* Halving the microsteps. */
//    init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel -  1 : init[0].step_sel);
//    if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) {
//        printf("    Step Mode not allowed.\r\n");
//    }
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Setting home position.\r\n");
//
//    /* Setting the home position. */
//    motors[0]->set_home();
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//    /* Printing to the console. */
//    printf("--> Moving forward %d steps.\r\n", STEPS_1);
//
//    /* Moving. */
//    motors[0]->move(StepperMotor::FWD, STEPS_1);
//
//    /* Waiting while active. */
//    motors[0]->wait_while_active();
//
//    /* Getting the current position. */
//    position = motors[0]->get_position();
//    
//    /* Printing to the console. */
//    printf("--> Getting the current position: %d\r\n", position);
//
//    /* Printing to the console. */
//    printf("--> Marking the current position.\r\n");
//
//    /* Marking the current position. */
//    motors[0]->set_mark();
//
//    /* Waiting. */
//    wait_ms(DELAY_2);
//
//
//    /*----- Running together for a certain amount of time. -----*/
//
//    /* Printing to the console. */
//    printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
//
//    /* Preparing each motor to perform a run at a specified speed. */
//    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
//        motors[m]->prepare_run(StepperMotor::BWD, 400);
//    }
//
//    /* Performing the action on each motor at the same time. */
//    x_nucleo_ihm02a1->perform_prepared_actions();
//
//    /* Waiting. */
//    wait_ms(DELAY_3);
//
//
//    /*----- Increasing the speed while running. -----*/
//
//    /* Preparing each motor to perform a run at a specified speed. */
//    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
//        motors[m]->prepare_get_speed();
//    }
//
//    /* Performing the action on each motor at the same time. */
//    uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions();
//
//    /* Printing to the console. */
//    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
//
//    /* Printing to the console. */
//    printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
//
//    /* Preparing each motor to perform a run at a specified speed. */
//    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
//        motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
//    }
//
//    /* Performing the action on each motor at the same time. */
//    results = x_nucleo_ihm02a1->perform_prepared_actions();
//
//    /* Waiting. */
//    wait_ms(DELAY_3);
//
//    /* Preparing each motor to perform a run at a specified speed. */
//    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
//        motors[m]->prepare_get_speed();
//    }
//
//    /* Performing the action on each motor at the same time. */
//    results = x_nucleo_ihm02a1->perform_prepared_actions();
//
//    /* Printing to the console. */
//    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
//
//    /* Waiting. */
//    wait_ms(DELAY_1);
//
//
//    /*----- Doing a full revolution on each motor, one after the other. -----*/
//
//    /* Printing to the console. */
//    printf("--> Doing a full revolution on each motor, one after the other.\r\n");
//
//    /* Doing a full revolution on each motor, one after the other. */
//    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
//        for (int i = 0; i < MPR_1; i++) {
//            /* Computing the number of steps. */
//            int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
//
//            /* Moving. */
//            motors[m]->move(StepperMotor::FWD, steps);
//            
//            /* Waiting while active. */
//            motors[m]->wait_while_active();
//
//            /* Waiting. */
//            wait_ms(DELAY_1);
//        }
//    }
//
//    /* Waiting. */
//    wait_ms(DELAY_2);
//
//}