d
Dependencies: HCSR04 NetworkSocketAPI Servo X_NUCLEO_53L0A1 X_NUCLEO_IDW01M1v2 X_NUCLEO_IHM01A1 mbed-rtos mbed
Fork of HelloWorld_IHM01A1 by
main.cpp
- Committer:
- d3dfantasy99
- Date:
- 2017-04-29
- Revision:
- 38:2fdc20bbb354
- Parent:
- 35:2b44ed4ec7a0
File content as of revision 38:2fdc20bbb354:
// Importo Librerie
#include "mbed.h"
/*#include "hcsr04.h"
#include "SpwfInterface.h"
#include "TCPSocket.h"
*/#include "DevSPI.h"
#include "L6474.h"
/*#include "XNucleoIHM02A1.h"
/*#include "x_nucleo_53l0a1.h"
#include <stdio.h>
#include "Servo.h"
#include "rtos.h"*/
#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 6000
#define DELAY_4 8000
/* 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
/* Variables -----------------------------------------------------------------*/
/* Initialization parameters. */
L6474_init_t init = {
160, /* Acceleration rate in pps^2. Range: (0..+inf). */
160, /* Deceleration rate in pps^2. Range: (0..+inf). */
1600, /* Maximum speed in pps. Range: (30..10000]. */
800, /* Minimum speed in pps. Range: [30..10000). */
250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
L6474_OCD_TH_750mA, /* 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). */
};
/* Motor Control Component. */
/* Functions -----------------------------------------------------------------*/
/**
* @brief This is an example of user handler for the flag interrupt.
* @param None
* @retval None
* @note If needed, implement it, and then attach and enable it:
* + motor->attach_flag_irq(&flag_irq_handler);
* + motor->enable_flag_irq();
* To disable it:
* + motor->disble_flag_irq();
*/
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;
}
/*
#define VL53L0_I2C_SDA D14 //sensore fotoni
#define VL53L0_I2C_SCL D15 //sensore fotoni
*/
/*DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
static X_NUCLEO_53L0A1 *board=NULL; //sensore fotoni*/
/*
//Inizializzo le schede
Serial pc(USBTX, USBRX); //seriale
SpwfSAInterface wifi(D8, D2, false); //wifi
Servo servomotore(PB_10); //servomotore
PwmOut motori(PA_8);//D13
DigitalOut sinistra(PA_0);
DigitalOut destra(PA_1);
TCPSocket socket(&wifi);
//Variabili Globali
char * ssid = "TekSmartLab"; //ssid wifi
char * seckey = ""; //password wifi
char * ip_socket = "192.168.1.104"; //ip socket master
int porta_socket = 8000; //porta del socket
int stato_socket = 0;
int stato = 0; // 1 = start - 0 = stop (default stop)
int gradi_servo = 45;
char buffer[0];
int controllo_buffer_in_arrivo = 0;
int contatore_buff = 0;
int invio_dati = 0;
int buca_trovata = 0;
int start_servo = 0;
uint32_t distanza_centrale = 0;
uint32_t distanza_destra = 0;
uint32_t distanza_sinistra = 0;
uint32_t test_distanza = 0;
int misure[2];*/
/* Second Motor. */
/*
void controllo_stato_connessione_thread(void const *args) {
while(1){
while(stato_socket == 0) {
/* Initializing Motor Control Component. */
/* Attaching and enabling interrupt handlers. */
/*
board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D10, D9);
board->InitBoard();
DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
board->sensor_centre->GetDistance(&test_distanza);
servomotore = gradi_servo/100.0;
destra.write(0);
sinistra.write(0);
pc.printf("\r\n Rift Searcher 1.0 \r\n");
/* while(!wifi.connect(ssid, seckey, NSAPI_SECURITY_NONE))/*NSAPI_SECURITY_WPA2*/ //{
/* pc.printf("\r\n Connessione alla rete wifi corso... Attendere...\r\n");
wait_ms(2000);
}
//connesso alla rete wifi
pc.printf("\r\n Connesso alla rete Wifi\r\n");
//verifico la connessione al socket
while(socket.connect(ip_socket, porta_socket) != 0){
pc.printf("\r\n Connessione al socket corso... Attendere...\r\n");
wait_ms(2000);
}
//connesso al socket
pc.printf("\r\n Connessione al socket riuscita\r\n");
stato_socket = 1;
}
}
}
void controllo_dati_entrata_thread(void const *args) {
pc.printf("\r\n Controllo dati\r\n");
while(1){
if(stato_socket == 1){
char buffer[1];
if(socket.recv(buffer, sizeof buffer) != 0){
int count = 0;
count = socket.recv(buffer, sizeof buffer);
if(count > 0){
buffer [count]='\0';
if(buffer[0] == '0'){
stato = 0;
pc.printf("\r\n Stato 0\r\n");
}
else if (buffer[0] == '1'){
stato = 1;
pc.printf("\r\n Stato 1 - Start\r\n");
}
else if (buffer[0] == '2'){
stato = 2;
pc.printf("\r\n Stato 1 - Start\r\n");
}
}
}
}
}
}
void misure_invio_dati(void const *args) {
}
void rotazione_passo_passo(void const *args) {
}
*/
/* Attaching and enabling interrupt handlers. */
int main() {
DevSPI dev_spi(D11, D12, D13);
/* Initializing Motor Control Component. */
motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
if (motor->init(&init) != COMPONENT_OK) {
exit(EXIT_FAILURE);
}
motor->attach_flag_irq(&flag_irq_handler);
motor->enable_flag_irq();
/* Printing to the console. */
printf("Motor Control Application Example for 1 Motor\r\n\n");
/*----- Moving. -----*/
/* Printing to the console. */
printf("--> Moving forward %d steps.\r\n", STEPS_1);
/* Moving N steps in the forward direction. */
motor->move(StepperMotor::FWD, STEPS_1);
/* Waiting while the motor is active. */
motor->wait_while_active();
/* Getting current position. */
int position = motor->get_position();
/*
//inizializzo la connessione seriale
//inizializzo thread wifi e buffer
Thread th_wifi(controllo_stato_connessione_thread);
Thread th_buffer (controllo_dati_entrata_thread);
Thread th_data (misure_invio_dati);
Thread th_passo (rotazione_passo_passo);
//Switch sullo stato
while(1) {
switch(stato){
case 0:
destra.write(0);
sinistra.write(0);
/*
char buffer1[1];
buffer1[0] = 'a';
int counta = 0;
pc.printf("\r\nSending Data\r\n");
counta = sizeof buffer1;
buffer1 [counta]='\0';
counta = socket.send(buffer1, sizeof buffer1);
if(counta > 0)
{
printf("%s\r\n", buffer1);
}
*/
/* break;
case 1: //start
motori.period_ms(60);
motori.pulsewidth(0.05);
while(stato == 1){
sinistra.write(1);
wait_ms(60);
destra.write(1);
wait_ms(1500);
destra.write(0);
wait_ms(60);
sinistra.write(0);
wait_ms(1000);
}
break;
case 2: //test servo e misure
int temp = 0;
while(stato == 2){
board->sensor_centre->GetDistance(&distanza_centrale);
if(distanza_centrale > test_distanza + 5){
if(temp < 100){
for(temp = gradi_servo; temp<100; temp = temp + 10) {
board->sensor_centre->GetDistance(&distanza_centrale);
board->sensor_centre->GetDistance(&distanza_destra);
board->sensor_centre->GetDistance(&distanza_sinistra);
printf("Distanza centrale : %ld\n Distanza destra : %ld\n Distanza sinistra : %ld\n", distanza_centrale,distanza_destra,distanza_sinistra);
wait_ms(500);
servomotore = temp/100.0;
wait_ms(0.01);
}
}
else{
for(temp = gradi_servo; temp<100; temp = temp + 10) {
board->sensor_centre->GetDistance(&distanza_centrale);
board->sensor_centre->GetDistance(&distanza_destra);
board->sensor_centre->GetDistance(&distanza_sinistra);
printf("Distanza centrale : %ld\n Distanza destra : %ld\n Distanza sinistra : %ld\n", distanza_centrale,distanza_destra,distanza_sinistra);
wait_ms(500);
servomotore = temp/100.0;
wait_ms(0.01);
}
}
}
break;
}*/
}
