Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- lucasfontenla
- Date:
- 2018-04-25
- Revision:
- 1:b2e395e50a45
- Parent:
- 0:cc562f78f889
- Child:
- 2:73d24891eef6
File content as of revision 1:b2e395e50a45:
/* Includes ------------------------------------------------------------------*/
/* mbed specific header files. */
#include "mbed.h"
#include "rtos.h"
/* Helper header files. */
#include "DevSPI.h"
/* Component specific header files. */
#include "L6474.h"
/* Definitions ---------------------------------------------------------------*/
/* Number of steps. */
#define STEPS_1 (400 * 8) /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
/* Delay in milliseconds. */
#define DELAY_1 1000
#define DELAY_2 2000
#define DELAY_3 2000
#define DELAY_4 2000
/* Speed in pps (Pulses Per Second).
In Full Step mode: 1 pps = 1 step/s).
In 1/N Step Mode: N pps = 1 step/s). */
#define SPEED_1 2400
#define SPEED_2 1200
AnalogIn joyx(A5);
AnalogIn joyy(A4);
DigitalIn bup(D12);
DigitalIn bdown(D13);
/* Variables -----------------------------------------------------------------*/
/* Initialization parameters. */
L6474_init_t init = {
300, /* Acceleration rate in pps^2. Range: (0..+inf). */
300, /* Deceleration rate in pps^2. Range: (0..+inf). */
2400, /* Maximum speed in pps. Range: (30..10000]. */
30, /* Minimum speed in pps. Range: [30..10000). */
250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
L6474_OCD_TH_2250mA, /* Overcurrent threshold (OCD_TH register). */
L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */
L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
L6474_ALARM_EN_OVERCURRENT |
L6474_ALARM_EN_THERMAL_SHUTDOWN |
L6474_ALARM_EN_THERMAL_WARNING |
L6474_ALARM_EN_UNDERVOLTAGE |
L6474_ALARM_EN_SW_TURN_ON |
L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
};
float value_x = 0;
float value_y = 0;
int value_bup = 0;
int value_bdown = 0;
unsigned int z_speed = 2000;
int read = 0;
int previous = 0;
int speed_read;
unsigned int maxSpeed = 2000;
unsigned int minSpeed = 30;
unsigned int speedFWDX = 0;
unsigned int speedBWDX = 0;
unsigned int speedFWDY = 0;
unsigned int speedBWDY = 0;
/* Motor Control Component. */
L6474 *motor;
L6474 *motor2;
L6474 *motor3;
/* Functions -----------------------------------------------------------------*/
void flag_irq_handler(void)
{
/* Set ISR flag. */
motor->isr_flag = TRUE;
/* Get the value of the status register. */
unsigned int status = motor->get_status();
/* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
/* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
}
/* Reset ISR flag. */
motor->isr_flag = FALSE;
}
/* Main ----------------------------------------------------------------------*/
float upper = 0.5;
float lower = 0.42;
int main(){
/*----- Initialization. -----*/
/* Initializing SPI bus. */
DevSPI dev_spi(D11, D12, D13);
// MOTOR 1 INIT ------------------------------------------------------------
motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
if (motor->init(&init) != COMPONENT_OK) {
exit(EXIT_FAILURE);
printf("Motor Error");
}
else {
printf("Motor 1 started");
}
/* Attaching and enabling interrupt handlers. */
motor->attach_flag_irq(&flag_irq_handler);
motor->enable_flag_irq();
/* Printing to the console. */
printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
/* Increasing the torque regulation current to 500[mA]. */
motor->set_parameter(L6474_TVAL, 500);
/* Doubling the microsteps. */
if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) {
printf("Motor 1 Step Mode not allowed.\r\n");
}
// MOTOR 2 INIT ------------------------------------------------------------
motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
if (motor2->init(&init) != COMPONENT_OK) {
exit(EXIT_FAILURE);
printf("Motor 2 Error");
} else {
printf("Motor 2 started");
}
/* Attaching and enabling interrupt handlers. */
motor2->attach_flag_irq(&flag_irq_handler);
motor2->enable_flag_irq();
/* Printing to the console. */
printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
/* Increasing the torque regulation current to 500[mA]. */
motor2->set_parameter(L6474_TVAL, 500);
/* Doubling the microsteps. */
if (!motor2->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) {
printf(" Motor 2 Step Mode not allowed.\r\n");
}
// MOTOR 1 INIT ------------------------------------------------------------
motor3 = new L6474(D2, D8, D5, D6, D10, dev_spi);
if (motor3->init(&init) != COMPONENT_OK) {
exit(EXIT_FAILURE);
printf("Motor 3 Error");
}
else {
printf("Motor 3 started");
}
/* Attaching and enabling interrupt handlers. */
motor3->attach_flag_irq(&flag_irq_handler);
motor3->enable_flag_irq();
/* Printing to the console. */
printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
/* Increasing the torque regulation current to 500[mA]. */
motor3->set_parameter(L6474_TVAL, 500);
/* Doubling the microsteps. */
if (!motor3->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_FULL)) {
printf("Motor 3 Step Mode not allowed.\r\n");
}
//------------------------------------------------------------------------------------------------------------------------
while(1){
value_x = joyx;
value_y = joyy;
wait(0.05);
value_bup = !bup;
value_bdown = !bdown;
//motor1
if(value_x < upper && value_x > lower){
motor->hard_stop();
}
else if(value_x > upper){
speedFWDX = (int)((float)maxSpeed-(float)minSpeed)*(value_x-upper)/((float)1-upper)+(float)minSpeed;
motor->set_min_speed(speedFWDX);
motor->run(StepperMotor::FWD);
printf("upper X %d\r\n",motor->get_speed());
}
else if(value_x < lower){
speedBWDX = (int)((float)maxSpeed - ((float)maxSpeed-(float)minSpeed)*value_x/lower);
motor->set_min_speed(speedBWDX);
motor->run(StepperMotor::BWD);
printf("lower X %d\r\n", motor->get_speed());
}
//motor2
if(value_y < upper && value_y > lower){
motor2->hard_stop();
}
else if(value_y > upper){
speedFWDY = (int)((float)maxSpeed-(float)minSpeed)*(value_y-upper)/((float)1-upper)+(float)minSpeed;
motor2->set_min_speed(speedFWDY);
motor2->run(StepperMotor::FWD);
printf("upper Y %d\r\n",motor2->get_speed());
}
else if(value_y < lower){
speedBWDY = (int)((float)maxSpeed - ((float)maxSpeed-(float)minSpeed)*value_y/lower);
motor2->set_min_speed(speedBWDY);
motor2->run(StepperMotor::BWD);
printf("lower Y %d\r\n", motor2->get_speed());
}
//motor 3
if(value_bup && value_bdown){
motor3->hard_stop();
}
else if(!value_bup && !value_bdown){
motor3->hard_stop();
}
else if(value_bup && !value_bdown){
motor3->set_min_speed(z_speed);
motor3->run(StepperMotor::FWD);
printf("upper Z %d\r\n",motor3->get_speed());
}
else if(value_bdown && !value_bup){
motor3->set_min_speed(z_speed);
motor3->run(StepperMotor::BWD);
printf("lower Z %d\r\n", motor3->get_speed());
}
}
}