tr

Dependencies:   mbed CANMsg

Committer:
nerit
Date:
Thu Jul 12 06:07:14 2018 +0000
Revision:
7:c9fd242538d9
Parent:
6:3fca0ca1949e
Child:
8:0e643ea7834f
Quinconce Update and V5 pcb application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nerit 1:e88bf5011af6 1
nerit 1:e88bf5011af6 2 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 3 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 4 // FIRMWARE SEMINATRICE MODULA
nerit 0:1e09cd7d66b4 5 // VERSIONE PER SCHEDA DI CONTROLLO CON DRIVER INTEGRATI
nerit 0:1e09cd7d66b4 6 // V4 - ATTENZIONE - LA VERSIONE V4 HA IL DRIVER STEPPER LV8727
nerit 0:1e09cd7d66b4 7 // IL MOTORE DC E' GESTITO CON IL DRIVER VNH3SP30-E E CON LA LETTURA
nerit 0:1e09cd7d66b4 8 // DELLA CORRENTE ASSORBITA TRAMITE IL CONVERTITORE MLX91210-CAS102 CON 50A FONDOSCALA
nerit 0:1e09cd7d66b4 9 // CHE FORNISCE UNA TENSIONE DI USCITA PARI A 40mV/A
nerit 0:1e09cd7d66b4 10 // FIRST RELEASE OF BOARD DEC 2017
nerit 0:1e09cd7d66b4 11 // FIRST RELEASE OF FIRMWARE JAN 2018
nerit 0:1e09cd7d66b4 12 //
nerit 0:1e09cd7d66b4 13 // THIS RELEASE: 09 JULY 2018
nerit 0:1e09cd7d66b4 14 //
nerit 0:1e09cd7d66b4 15 // APPLICATION: MODULA CON DISTRIBUTORE ZUCCA OPPURE RISO E PUO' FUNZIONARE ANCHE CON SENSORE A 25 FORI SUL DISCO O
nerit 0:1e09cd7d66b4 16 // ENCODER MOTORE SETTANDO GLI APPOSITI FLAGS
nerit 0:1e09cd7d66b4 17 //
nerit 0:1e09cd7d66b4 18 // 29 05 2018 - INSERITO SECONDO ENCODER VIRTUALE PER LA GESTIONE DEL SINCRONISMO TRA TAMBURO E RUOTA DI SEMINA
nerit 0:1e09cd7d66b4 19 // IN PRATICA IL PRIMO ENCODER è SINCRONO CON IL SEGNALE DEI BECCHI E VIENE AZZERATO DA QUESTI, MENTRE
nerit 0:1e09cd7d66b4 20 // IL SECONDO E' INCREMENTATO IN SINCRONO CON IL PRIMO MA AZZERATO DALLA FASE. IL SUO VALORE E' POI DIVISO
nerit 0:1e09cd7d66b4 21 // PER IL RAPPORTO RUOTE E LA CORREZIONE AGISCE SULLA VELOCITA' DEL TAMBURO PER MANTENERE LA FASE DEL SECONDO
nerit 0:1e09cd7d66b4 22 // ENCODER
nerit 0:1e09cd7d66b4 23 // 05 06 2018 - INSERITO IL CONTROLLO DI GESTIONE DEL QUINCONCE SENZA ENCODER
nerit 0:1e09cd7d66b4 24 // 09 06 2018 - INSERITO CONTROLLO DI FASE CON ENCODER MASTER PER QUINCONCE - DATO SCAMBIATO IN CAN
nerit 0:1e09cd7d66b4 25 //
nerit 0:1e09cd7d66b4 26 /********************
nerit 0:1e09cd7d66b4 27 IL FIRMWARE SI COMPONE DI 7 FILES:
nerit 0:1e09cd7d66b4 28 - main.cpp
nerit 0:1e09cd7d66b4 29 - main.hpp
nerit 0:1e09cd7d66b4 30 - iodefinition.hpp
nerit 0:1e09cd7d66b4 31 - canbus.hpp
nerit 0:1e09cd7d66b4 32 - parameters.hpp
nerit 0:1e09cd7d66b4 33 - timeandtick.hpp
nerit 0:1e09cd7d66b4 34 - variables.hpp
nerit 0:1e09cd7d66b4 35 ED UTILIZZA LE LIBRERIE STANDARD MBED PIU' UNA LIBRERIA MODIFICATA E DEDICATA PER IL CAN
nerit 0:1e09cd7d66b4 36 *********************
nerit 0:1e09cd7d66b4 37 LA MACCHINA UTILIZZA SEMPRE 2 SOLI SENSORI; UNO PER SENTIRE LE CELLE DI CARICO SEME ED UNO PER SENTIRE I BECCHI DI SEMINA.
nerit 0:1e09cd7d66b4 38 GLI AZIONAMENTI SONO COMPOSTI DA DUE MOTORI; UN DC PER IL CONTROLLO DELLA RUOTA DI SEMINA ED UNO STEPPER PER IL CONTROLLO DEL TAMBURO
nerit 0:1e09cd7d66b4 39 UN SENSORE AGGIUNTIVO SULL'ELEMENTO MASTER RILEVA LA VELOCITA' DI AVANZAMENTO
nerit 0:1e09cd7d66b4 40 UN SENSORE AGGIUNTIVO SULLA RUOTA DI SEMINA RILEVA LA ROTAZIONE DELLA RUOTA STESSA ATTRAVERSO FORI PRESENTI SUL DISCO DI SEMINA
nerit 0:1e09cd7d66b4 41 *********************
nerit 0:1e09cd7d66b4 42 LA LOGICA GENERALE PREVEDE CHE IL DC DELLA RUOTA DI SEMINA VENGA COMANDATO IN FUNZIONE DELLA VELOCITA' LETTA DAL SENSORE DI AVANZAMAENTO DEL MASTER
nerit 0:1e09cd7d66b4 43 IL PROBLEMA PRINCIPALE E' CHE QUANDO I BECCHI SONO INSERITI NEL TERRENO NON VI E' RETROAZIONE REALE SULLA VELOCITA' DI ROTAZIONE DELLA RUOTA STESSA
nerit 0:1e09cd7d66b4 44 PROPRIO PERCHE' L'AVANZAMANETO NEL TERRENO IMPRIME UNA VELOCITA' PROPRIA AL BECCO E QUINDI ANCHE ALLA RUOTA.
nerit 0:1e09cd7d66b4 45 PER OVVIARE A QUESTO PROBLEMA SI E' INSERITO UN CONTROLLO DI CORRENTE ASSORBITA DAL DC; SE E' BASSA DEVO ACCELERARE, SE E' ALTA DEVO RALLENTARE
nerit 0:1e09cd7d66b4 46 IL VALORE DI RIFERIMENTO DELL'ANALOGICA DI INGRESSO VIENE AGGIORNATO OGNI VOLTA CHE LA RUOTA DI SEMINA E' FERMA
nerit 0:1e09cd7d66b4 47 IL TAMBURO SEGUE LA RUOTA DI SEMINA RILEVANDONE LA VELOCITA' E RICALCOLANDO LA PROPRIA IN FUNZIONE DELLA REALE VELOCITA' DI ROTAZIONE DELLA RUOTA DI SEMINA
nerit 0:1e09cd7d66b4 48 LA FASE VIENE DETERMINATA DAL PASSAGGIO DEI BECCHI SUL SENSORE RELATIVO.
nerit 0:1e09cd7d66b4 49 IL PROBLEMA PRINCIPALE NEL MANTENERE LA FASE DEL TAMBURO E' DATO DAL FATTO CHE LA SINCRONIZZAZIONE DELLA FASE SOLO SULL'IMPULSO DEL BECCO NON E' SUFFICIENTE
nerit 0:1e09cd7d66b4 50 SOPRATUTTO QUANDO I BECCHI SONO MOLTO DISTANZIATI.
nerit 0:1e09cd7d66b4 51 PER OVVIARE A QUESTO PROBLEMA SI SONO INSERITI DUE ENCODER VIRTUALI CHE SEZIONANO LA RUOTA DI SEMINA IN 9000 PARTI. ENTRAMBI VENGONO GESTITI DA UN GENERATORE DINAMICO DI CLOCK INTERNO
nerit 0:1e09cd7d66b4 52 TARATO SULLA REALE VELOCITA' DI ROTAZIONE DELLA RUOTA DI SEMINA.
nerit 0:1e09cd7d66b4 53 IL PRIMO ENCODER VIRTUALE SI OCCUPA DI DETERMINARE LA POSIZIONE FISICA DELLA RUOTA DI SEMINA E SI AZZERA AL PASSAGGIO DI OGNI BECCO.
nerit 0:1e09cd7d66b4 54 IL SECONDO VIENE AZZERATO DALL'IMPULSO DI FASE DEL PRIMO ENCODER DETERMINATO DAI VALORI IMPOSTI SUL TERMINALE TRITECNICA
nerit 0:1e09cd7d66b4 55 IL SECONDO ENCODER VIENE CONFRONTATO CON LA POSIZIONE ASSOLUTA DEL TAMBURO (DETERMINATA DAL NUMERO DI STEP EMESSI DAL CONTROLLO), RAPPORTATA TRA CELLE E BECCHI.
nerit 0:1e09cd7d66b4 56 IL CONFRONTO DETERMINA LA POSIZIONE RELATIVA DELLA SINGOLA CELLA RISPETTO AL SINGOLO BECCO. IL MANTENIMENTO DELLA SINCRONIZZAZIONE DI FASE, DETERMINA IL SINCRO CELLA/BECCO.
nerit 0:1e09cd7d66b4 57 LA SINCRONIZZAZIONE VIENE PERO' E' A SUA VOLTA RICALCOLATA SHIFTANDO LA POSIZIONE DI AZZERAMENTO DEL SECONDO ENCODER IN FUNZIONE DELLA VELOCITA' DI ROTAZIONE GENERALE AL FINE
nerit 0:1e09cd7d66b4 58 DI CAMBIARE L'ANGOLO DI ANTICIPO DI RILASCIO DEL SEME IN FUNZIONE DELLA VELOCITA' E RECUPERARE COSI' IL TEMPO DI VOLO DEL SEME.
nerit 0:1e09cd7d66b4 59 IL TAMBURO HA DUE TIPI DI FUNZIONAMENTO: CONTINUO E AD IMPULSI. E' SELEZIONABILE IN FUNZIONE DELLA VELOCITA' E DEL TIPO DI DISTRIBUTORE MONTATO.
nerit 0:1e09cd7d66b4 60 **********************
nerit 0:1e09cd7d66b4 61 TUTTI I VALORI, CELLE, BECCHI, IMPULSI VELOCITA', ANCGOLO DI AVVIO, FASE DI SEMINA, ECC.. SONO IMPOSTABILI DA PANNELLO OPERATORE
nerit 0:1e09cd7d66b4 62 I DATI SONO SCAMBIATI CON IL PANNELLO OPERATORE E CON GLI ALTRI MODULI ATTRAVERSO RETE CAN CON PROTOCOLLO FREESTYLE ATTRAVERSO INDIRIZZAMENTI DEDICATI
nerit 0:1e09cd7d66b4 63 AL MOMENTO NON E' POSSIBILE ATTRIBUIRE L'INIDIRIZZO BASE DELL'ELEMENTO DA TERMINALE OPERATORE MA SOLO IN FASE DI COMPILAZIONE DEL FIRMWARE.
nerit 0:1e09cd7d66b4 64 **********************
nerit 0:1e09cd7d66b4 65 ALTRE SEZIONI RIGUARDANO LA GENERAZIONE DEGLI ALLARMI, LA COMUNICAZIONE CAN, LA SIMULAZIONE DI LAVORO, LA GESTIONE DELLA DIAGNOSI ECC..
nerit 0:1e09cd7d66b4 66 IL MOTORE DC E' CONTROLLATO DA DIVERSE ROUTINE; LE PRIORITA' SONO (DALLA PIU' BASSA ALLA PIU' ALTA): CALCOLO TEORICO, RICALCOLO REALE, CONTROLLO DI FASE QUINCONCE, CONTROLLO DI CORRENTE.
nerit 0:1e09cd7d66b4 67 LO STEPPER SEGUE IL DC.
nerit 0:1e09cd7d66b4 68 **********************
nerit 0:1e09cd7d66b4 69 IN FASE DI ACCENSIONE ED OGNI QUALVOLTA SI ARRIVA A VELOCITA' ZERO, LA MACCHINA ESEGUE UN CICLO DI AZZERAMENTO
nerit 0:1e09cd7d66b4 70 NON ESISTE PULSANTE DI MARCIA/STOP; E' SEMPRE ATTIVA.
nerit 0:1e09cd7d66b4 71 **********************
nerit 0:1e09cd7d66b4 72 NEL PROGRAMMA E' PRESENTE UNA SEZIONE DI TEST FISICO DELLA SCHEDA ATTIVABILE SOLO IN FASE DI COMPILAZIONE
nerit 0:1e09cd7d66b4 73 **********************
nerit 0:1e09cd7d66b4 74 ALTRE FUNZIONI: PRECARICAMENTO DEL TAMBURO
nerit 0:1e09cd7d66b4 75 AZZERAMENTO MANUALE
nerit 0:1e09cd7d66b4 76 STATISTICA DI SEMINA (CONTA LE CELLE)
nerit 0:1e09cd7d66b4 77 */
nerit 0:1e09cd7d66b4 78 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 79 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 80
nerit 0:1e09cd7d66b4 81 #include "main.hpp"
nerit 0:1e09cd7d66b4 82 #include "timeandtick.hpp"
nerit 0:1e09cd7d66b4 83 #include "canbus.hpp"
nerit 0:1e09cd7d66b4 84 #include "watchdog.h"
nerit 0:1e09cd7d66b4 85 #include "iodefinition.hpp"
nerit 0:1e09cd7d66b4 86 #include "parameters.hpp"
nerit 0:1e09cd7d66b4 87 #include "variables.hpp"
nerit 0:1e09cd7d66b4 88 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 89 //********************************************************************************************************************
nerit 0:1e09cd7d66b4 90 // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
nerit 0:1e09cd7d66b4 91 // TASK SECTION
nerit 0:1e09cd7d66b4 92 // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
nerit 0:1e09cd7d66b4 93 //************************************************************************
nerit 3:c0f11ca4df02 94 //************************************************************************
nerit 0:1e09cd7d66b4 95 // rise of seed speed 25 pulse sensor
nerit 0:1e09cd7d66b4 96 void sd25Fall(){
nerit 0:1e09cd7d66b4 97 timeHole=metalTimer.read_ms();
nerit 0:1e09cd7d66b4 98 int memo_TimeHole= (memoTimeHole + timeHole)/ (int)2;
nerit 0:1e09cd7d66b4 99 memoTimeHole = timeHole;
nerit 0:1e09cd7d66b4 100 metalTimer.reset();
nerit 0:1e09cd7d66b4 101 if (speedFromPick==0){
nerit 0:1e09cd7d66b4 102 speedOfSeedWheel=((seedPerimeter/25.0f)/(double)memo_TimeHole)*1000.0f; //mtS
nerit 0:1e09cd7d66b4 103 }
nerit 0:1e09cd7d66b4 104 }
nerit 5:3b95bbfe2dc9 105 // rise of seed speed motor encoder
nerit 5:3b95bbfe2dc9 106 void encoRise(){
nerit 5:3b95bbfe2dc9 107 timeHole=metalTimer.read_us();
nerit 5:3b95bbfe2dc9 108 int memo_TimeHole= (memoTimeHole + timeHole)/ (int)2;
nerit 5:3b95bbfe2dc9 109 memoTimeHole = timeHole;
nerit 5:3b95bbfe2dc9 110 metalTimer.reset();
nerit 5:3b95bbfe2dc9 111 if (encoder==true){
nerit 5:3b95bbfe2dc9 112 speedOfSeedWheel=((seedPerimeter/((SDreductionRatio*25.5f)))/(double)memo_TimeHole)*1000000.0f; //mtS
nerit 7:c9fd242538d9 113 pulseRised2=1;
nerit 5:3b95bbfe2dc9 114 }
nerit 5:3b95bbfe2dc9 115 }
nerit 7:c9fd242538d9 116 // rise of seed presence sensor
nerit 7:c9fd242538d9 117 void seedSensorTask(){
nerit 7:c9fd242538d9 118 seedSee=1;
nerit 7:c9fd242538d9 119 }
nerit 0:1e09cd7d66b4 120 //**************************************************
nerit 0:1e09cd7d66b4 121 // generate speed clock when speed is simulated from Tritecnica display
nerit 0:1e09cd7d66b4 122 void speedSimulationClock(){
nerit 0:1e09cd7d66b4 123 lastPulseRead=speedTimer.read_us();
nerit 0:1e09cd7d66b4 124 oldLastPulseRead=lastPulseRead;
nerit 0:1e09cd7d66b4 125 speedTimer.reset();
nerit 0:1e09cd7d66b4 126 pulseRised=1;
nerit 7:c9fd242538d9 127 speedFilter.reset();
nerit 0:1e09cd7d66b4 128 }
nerit 0:1e09cd7d66b4 129 //*******************************************************
nerit 0:1e09cd7d66b4 130 // interrupt task for tractor speed reading
nerit 0:1e09cd7d66b4 131 //*******************************************************
nerit 0:1e09cd7d66b4 132 void tractorReadSpeed(){
nerit 0:1e09cd7d66b4 133 if ((oldTractorSpeedRead==0)){
nerit 0:1e09cd7d66b4 134 lastPulseRead=speedTimer.read_us();
nerit 0:1e09cd7d66b4 135 oldLastPulseRead=lastPulseRead;
nerit 0:1e09cd7d66b4 136 speedTimer.reset();
nerit 0:1e09cd7d66b4 137 pulseRised=1;
nerit 0:1e09cd7d66b4 138 oldTractorSpeedRead=1;
nerit 6:3fca0ca1949e 139 spazioCoperto+= pulseDistance;
nerit 0:1e09cd7d66b4 140 }
nerit 0:1e09cd7d66b4 141 speedFilter.reset();
nerit 0:1e09cd7d66b4 142 speedClock=1;
nerit 0:1e09cd7d66b4 143 }
nerit 0:1e09cd7d66b4 144 //*******************************************************
nerit 0:1e09cd7d66b4 145 void speedMediaCalc(){
nerit 0:1e09cd7d66b4 146 double lastPd=(double) lastPulseRead/1000.0f;
nerit 0:1e09cd7d66b4 147 pulseSpeedInterval = (mediaSpeed[0]+lastPd)/2.0f;
nerit 0:1e09cd7d66b4 148 if (enableSimula==1){
nerit 0:1e09cd7d66b4 149 double TMT = (double)(speedSimula) * 100.0f /3600.0f;
nerit 0:1e09cd7d66b4 150 pulseSpeedInterval = pulseDistance / TMT;
nerit 0:1e09cd7d66b4 151 }
nerit 0:1e09cd7d66b4 152 mediaSpeed[0]=lastPd;
nerit 0:1e09cd7d66b4 153 OLDpulseSpeedInterval=pulseSpeedInterval;
nerit 0:1e09cd7d66b4 154 }
nerit 0:1e09cd7d66b4 155
nerit 0:1e09cd7d66b4 156 //*******************************************************
nerit 0:1e09cd7d66b4 157 // clocked task for manage virtual encoder of seed wheel i/o
nerit 0:1e09cd7d66b4 158 //*******************************************************
nerit 0:1e09cd7d66b4 159 //*******************************************************
nerit 0:1e09cd7d66b4 160 void step_SDPulseOut(){
nerit 0:1e09cd7d66b4 161 SDactualPosition++;
nerit 0:1e09cd7d66b4 162 prePosSD++;
nerit 5:3b95bbfe2dc9 163 #if defined(speedMaster)
nerit 5:3b95bbfe2dc9 164 posForQuinc++;
nerit 5:3b95bbfe2dc9 165 #endif
nerit 0:1e09cd7d66b4 166 }
nerit 0:1e09cd7d66b4 167 //*******************************************************
nerit 0:1e09cd7d66b4 168 void step_TBPulseOut(){
nerit 0:1e09cd7d66b4 169 TBmotorStepOut=!TBmotorStepOut;
nerit 0:1e09cd7d66b4 170 if (TBmotorStepOut==0){
nerit 0:1e09cd7d66b4 171 TBactualPosition++;
nerit 0:1e09cd7d66b4 172 }
nerit 0:1e09cd7d66b4 173 }
nerit 0:1e09cd7d66b4 174 //*******************************************************
nerit 0:1e09cd7d66b4 175 // aggiornamento parametri di lavoro fissi e da Tritecnica
nerit 0:1e09cd7d66b4 176 void aggiornaParametri(){
nerit 0:1e09cd7d66b4 177 speedPerimeter = Pi * speedWheelDiameter ; // perimeter of speed wheel
nerit 0:1e09cd7d66b4 178 pulseDistance = (speedPerimeter / speedWheelPulse)*1000.0f; // linear space between speed wheel pulse
nerit 0:1e09cd7d66b4 179 seedPerimeter = Pi * (seedWheelDiameter-(deepOfSeed*2.0f)); // perimeter of seed wheel
nerit 0:1e09cd7d66b4 180 intraPickDistance = seedPerimeter/pickNumber;
nerit 0:1e09cd7d66b4 181 K_WheelRPM = 60.0f/seedPerimeter; // calcola il K per i giri al minuto della ruota di semina
nerit 0:1e09cd7d66b4 182 K_WhellFrequency = (seedWheelMotorSteps*SDreductionRatio)/60.0f; // calcola il K per la frequenza di comando del motore di semina
nerit 0:1e09cd7d66b4 183 rapportoRuote = pickNumber/cellsNumber; // calcola il rapporto tra il numero di becchi ed il numero di celle
nerit 0:1e09cd7d66b4 184 K_percentuale = TBmotorSteps*TBreductionRatio;
nerit 0:1e09cd7d66b4 185 SDsectorStep = (double)fixedStepGiroSD / (double)pickNumber;
nerit 0:1e09cd7d66b4 186 TBsectorStep = (TBmotorSteps*TBreductionRatio)/cellsNumber;
nerit 0:1e09cd7d66b4 187 KcorT = (SDsectorStep/TBsectorStep);///2.0f;
nerit 0:1e09cd7d66b4 188 angoloFase=angoloPh;
nerit 0:1e09cd7d66b4 189 avvioGradi=angoloAv;
nerit 0:1e09cd7d66b4 190 stepGrado=fixedStepGiroSD/360.0f;
nerit 0:1e09cd7d66b4 191 TBdeltaStep=(fixedStepGiroSD/pickNumber)+(stepGrado*avvioGradi);
nerit 0:1e09cd7d66b4 192 TBfaseStep = (stepGrado*angoloFase);
nerit 0:1e09cd7d66b4 193 K_WheelRPM = 60.0f/seedPerimeter; // calcola il K per i giri al minuto della ruota di semina 25.4 25,40
nerit 7:c9fd242538d9 194 TBgiroStep = TBmotorSteps*TBreductionRatio;
nerit 7:c9fd242538d9 195 K_TBfrequency = TBgiroStep/60.0f; // 1600 * 1.65625f /60 = 44 44,00
nerit 0:1e09cd7d66b4 196 if (speedFromPick==1) {
nerit 0:1e09cd7d66b4 197 intraPickDistance = seedPerimeter/pickNumber;
nerit 0:1e09cd7d66b4 198 }else{
nerit 0:1e09cd7d66b4 199 intraPickDistance = seedPerimeter/25.0f; // 25 è il numero di fori presenti nel disco di semina
nerit 0:1e09cd7d66b4 200 }
nerit 0:1e09cd7d66b4 201 }
nerit 0:1e09cd7d66b4 202 //*******************************************************
nerit 0:1e09cd7d66b4 203 void cambiaTB(double perio){
nerit 0:1e09cd7d66b4 204 // update TB frequency
nerit 0:1e09cd7d66b4 205 double TBper=0.0f;
nerit 0:1e09cd7d66b4 206 if (aspettaStart==0){
nerit 0:1e09cd7d66b4 207 if (perio<250.0f){perio=500.0f;}
nerit 0:1e09cd7d66b4 208 double scala =0.0f;
nerit 0:1e09cd7d66b4 209 if (lowSpeed==1){
nerit 0:1e09cd7d66b4 210 scala =2.0f;
nerit 0:1e09cd7d66b4 211 }else{
nerit 0:1e09cd7d66b4 212 scala =1.8f;
nerit 0:1e09cd7d66b4 213 }
nerit 0:1e09cd7d66b4 214 TBper=perio/scala;
nerit 0:1e09cd7d66b4 215 if (oldPeriodoTB!=TBper){
nerit 0:1e09cd7d66b4 216 if (TBper >= 250.0f){
nerit 0:1e09cd7d66b4 217 TBticker.attach_us(&step_TBPulseOut,TBper); // clock time are milliseconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 218 }else{
nerit 0:1e09cd7d66b4 219 TBticker.detach();
nerit 0:1e09cd7d66b4 220 }
nerit 0:1e09cd7d66b4 221 oldPeriodoTB=TBper;
nerit 0:1e09cd7d66b4 222 }
nerit 0:1e09cd7d66b4 223 }
nerit 0:1e09cd7d66b4 224 }
nerit 0:1e09cd7d66b4 225 //*******************************************************
nerit 0:1e09cd7d66b4 226 void seedCorrect(){
nerit 0:1e09cd7d66b4 227 /*
nerit 0:1e09cd7d66b4 228 posError determina la posizione relativa di TB rispetto ad SD
nerit 0:1e09cd7d66b4 229 la reale posizione di SD viene modificata in funzione della velocità per
nerit 0:1e09cd7d66b4 230 traslare la posizione relativa di TB. All'aumentare della velocità la posizione
nerit 0:1e09cd7d66b4 231 di SD viene incrementata così che TB acceleri per raggiungerla in modo da rilasciare il seme prima
nerit 0:1e09cd7d66b4 232 La taratura del sistema avviene determinando prima il valore di angoloFase alla minima velocità,
nerit 0:1e09cd7d66b4 233 poi, alla massima velocità, dovrebbe spostarsi la posizione relativa con una variabile proporzionale alla velocità, ma c'è un però.
nerit 0:1e09cd7d66b4 234 Il problema è che il momento di avvio determina una correzione dell'angolo di partenza del tamburo
nerit 0:1e09cd7d66b4 235 angolo che viene rideterminato ogni volta che il sensore becchi legge un transito.
nerit 0:1e09cd7d66b4 236 Di fatto c'è una concorrenza tra l'angolo di avvio determinato e la correzione di posizione relativa
nerit 0:1e09cd7d66b4 237 del tamburo. E' molto probabile che convenga modificare solo la posizione relativa e non anche l'angolo di avvio
nerit 0:1e09cd7d66b4 238 Ancora di più se viene eliminata la parte gestita da ciclata.
nerit 0:1e09cd7d66b4 239 In questo modo dovrebbe esserci solo un andamento in accelerazione di TB che viene poi eventualmente decelerato
nerit 0:1e09cd7d66b4 240 dal passaggio sul sensore di TB. Funzione corretta perchè il sincronismo tra i sensori genera l'inibizione della correzione
nerit 0:1e09cd7d66b4 241 di fase di TB. In pratica il ciclo viene resettato al passaggio sul sensore di SD che riporta a 0 la posizione di SD.
nerit 0:1e09cd7d66b4 242 Appena il sensore di TB viene impegnato allora viene abilitato il controllo di fase del tamburo.
nerit 0:1e09cd7d66b4 243 Questo si traduce nel fatto che il controllo di posizione viene gestito solo all'interno di uno slot di semina in modo che
nerit 0:1e09cd7d66b4 244 il tamburo non risenta della condizione di reset della posizione di SD mentre lui è ancora nella fase precedente. Si fermerebbe.
nerit 0:1e09cd7d66b4 245
nerit 0:1e09cd7d66b4 246 // La considerazione finale è che mantenendo l'angolo di avvio fisso e regolato sulla bassa velocità, intervenendo solo sulla correzione
nerit 0:1e09cd7d66b4 247 // di posizione in questa routine, dovrebbe essere possibile seminare correttamente a tutte le velocità regolando solo 2 parametri.
nerit 0:1e09cd7d66b4 248 */
nerit 0:1e09cd7d66b4 249 /*
nerit 0:1e09cd7d66b4 250 SDsectorStep = (double)fixedStepGiroSD / (double)pickNumber;
nerit 0:1e09cd7d66b4 251 TBsectorStep = (TBmotorSteps*TBreductionRatio)/cellsNumber;
nerit 0:1e09cd7d66b4 252 KcorT = (SDsectorStep/TBsectorStep);
nerit 0:1e09cd7d66b4 253 angoloFase=angoloPh;
nerit 0:1e09cd7d66b4 254 stepGrado=fixedStepGiroSD/360.0f;
nerit 0:1e09cd7d66b4 255 avvioGradi = costante da terminale tritecnica
nerit 0:1e09cd7d66b4 256 TBdeltaStep=(fixedStepGiroSD/pickNumber)-(stepGrado*avvioGradi);
nerit 0:1e09cd7d66b4 257 TBfaseStep = (stepGrado*angoloFase);
nerit 0:1e09cd7d66b4 258 */
nerit 0:1e09cd7d66b4 259
nerit 0:1e09cd7d66b4 260 if ((tractorSpeed_MtS_timed>0.01f)){
nerit 0:1e09cd7d66b4 261 if (inhibit==0){
nerit 0:1e09cd7d66b4 262 double posError =0.0f;
nerit 0:1e09cd7d66b4 263 double posSD=((double)SDactualPosition)/KcorT;
nerit 0:1e09cd7d66b4 264 posError = posSD - (double)TBactualPosition;
nerit 0:1e09cd7d66b4 265 // interviene sulla velocità di TB per raggiungere la corretta posizione relativa
nerit 0:1e09cd7d66b4 266 if((lowSpeed==0)&&(aspettaStart==0)){
nerit 0:1e09cd7d66b4 267 if (posError>50.0f){posError=50.0f;}
nerit 0:1e09cd7d66b4 268 if (posError<-50.0f){posError=-50.0f;}
nerit 0:1e09cd7d66b4 269 if ((posError >=1.0f)||(posError<=-1.0f)){
nerit 0:1e09cd7d66b4 270 ePpos = periodo /(1.0f+ ((posError/100.0f)));
nerit 0:1e09cd7d66b4 271 cambiaTB(ePpos);
nerit 0:1e09cd7d66b4 272 }
nerit 0:1e09cd7d66b4 273 }
nerit 0:1e09cd7d66b4 274 }
nerit 4:d32258ec411f 275 }
nerit 4:d32258ec411f 276 }
nerit 4:d32258ec411f 277 //*******************************************************
nerit 4:d32258ec411f 278 void videoUpdate(){
nerit 4:d32258ec411f 279 for(int aa=0;aa<4;aa++){speedForDisplay[aa]=speedForDisplay[aa+1];}
nerit 4:d32258ec411f 280 speedForDisplay[4]=tractorSpeed_MtS_timed;
nerit 4:d32258ec411f 281 totalSpeed=0.0f;
nerit 4:d32258ec411f 282 for (int aa=0; aa<5; aa++){totalSpeed += speedForDisplay[aa];}
nerit 4:d32258ec411f 283 totalSpeed = totalSpeed / 5.0f;
nerit 0:1e09cd7d66b4 284 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 285 #if defined(SDreset)
nerit 0:1e09cd7d66b4 286 pc.printf("Fase: %d",fase);
nerit 0:1e09cd7d66b4 287 pc.printf(" PrePosSD: %d",prePosSD);
nerit 0:1e09cd7d66b4 288 pc.printf(" PosSD: %d",SDactualPosition);
nerit 0:1e09cd7d66b4 289 pc.printf(" speed: %f",tractorSpeed_MtS_timed);
nerit 0:1e09cd7d66b4 290 pc.printf(" Trigger: %d \n", trigRepos);
nerit 0:1e09cd7d66b4 291 #endif
nerit 0:1e09cd7d66b4 292 #endif
nerit 0:1e09cd7d66b4 293 }
nerit 0:1e09cd7d66b4 294 //*******************************************************
nerit 0:1e09cd7d66b4 295 void ciclaTB(){
nerit 0:1e09cd7d66b4 296 if ((startCicloTB==1)&&(cicloTbinCorso==0)){
nerit 0:1e09cd7d66b4 297 TBticker.attach_us(&step_TBPulseOut,TBperiod/2.5f); // clock time are milliseconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 298 cicloTbinCorso = 1;
nerit 0:1e09cd7d66b4 299 startCicloTB=0;
nerit 0:1e09cd7d66b4 300 }
nerit 0:1e09cd7d66b4 301 if ((loadDaCan==1)&&(loadDaCanInCorso==0)){
nerit 0:1e09cd7d66b4 302 TBticker.attach_us(&step_TBPulseOut,1000.0f); // clock time are milliseconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 303 loadDaCanInCorso=1;
nerit 0:1e09cd7d66b4 304 stopCicloTB=0;
nerit 0:1e09cd7d66b4 305 }
nerit 0:1e09cd7d66b4 306 if ((stopCicloTB==1)&&(TBactualPosition>5)){
nerit 0:1e09cd7d66b4 307 TBticker.detach();
nerit 0:1e09cd7d66b4 308 cicloTbinCorso = 0;
nerit 0:1e09cd7d66b4 309 stopCicloTB=0;
nerit 0:1e09cd7d66b4 310 loadDaCanInCorso=0;
nerit 0:1e09cd7d66b4 311 loadDaCan=0;
nerit 0:1e09cd7d66b4 312 }
nerit 0:1e09cd7d66b4 313 }
nerit 7:c9fd242538d9 314 // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
nerit 7:c9fd242538d9 315 void stepSetting(){
nerit 7:c9fd242538d9 316 // Stepper driver init and set
nerit 7:c9fd242538d9 317 TBmotorRst=1; // reset stepper driver
nerit 7:c9fd242538d9 318 TBmotorDirecti=1; // reset stepper direction
nerit 7:c9fd242538d9 319 // M1 M2 M3 RESOLUTION
nerit 7:c9fd242538d9 320 // 0 0 0 1/2
nerit 7:c9fd242538d9 321 // 1 0 0 1/8
nerit 7:c9fd242538d9 322 // 0 1 0 1/16
nerit 7:c9fd242538d9 323 // 1 1 0 1/32
nerit 7:c9fd242538d9 324 // 0 0 1 1/64
nerit 7:c9fd242538d9 325 // 1 0 1 1/128
nerit 7:c9fd242538d9 326 // 0 1 1 1/10
nerit 7:c9fd242538d9 327 // 1 1 1 1/20
nerit 7:c9fd242538d9 328 if (TBmotorSteps==400){
nerit 7:c9fd242538d9 329 TBmotor_M1=1;
nerit 7:c9fd242538d9 330 TBmotor_M2=1;
nerit 7:c9fd242538d9 331 TBmotor_M3=1;
nerit 7:c9fd242538d9 332 }else if (TBmotorSteps==1600){
nerit 7:c9fd242538d9 333 TBmotor_M1=0;
nerit 7:c9fd242538d9 334 TBmotor_M2=1;
nerit 7:c9fd242538d9 335 TBmotor_M3=1;
nerit 7:c9fd242538d9 336 }else if (TBmotorSteps==3200){
nerit 7:c9fd242538d9 337 TBmotor_M1=1;
nerit 7:c9fd242538d9 338 TBmotor_M2=0;
nerit 7:c9fd242538d9 339 TBmotor_M3=1;
nerit 7:c9fd242538d9 340 }else if (TBmotorSteps==6400){
nerit 7:c9fd242538d9 341 TBmotor_M1=0;
nerit 7:c9fd242538d9 342 TBmotor_M2=0;
nerit 7:c9fd242538d9 343 TBmotor_M3=1;
nerit 7:c9fd242538d9 344 }else if (TBmotorSteps==12800){
nerit 7:c9fd242538d9 345 TBmotor_M1=1;
nerit 7:c9fd242538d9 346 TBmotor_M2=1;
nerit 7:c9fd242538d9 347 TBmotor_M3=0;
nerit 7:c9fd242538d9 348 }else if (TBmotorSteps==25600){
nerit 7:c9fd242538d9 349 TBmotor_M1=0;
nerit 7:c9fd242538d9 350 TBmotor_M2=1;
nerit 7:c9fd242538d9 351 TBmotor_M3=0;
nerit 7:c9fd242538d9 352 }else if (TBmotorSteps==2000){
nerit 7:c9fd242538d9 353 TBmotor_M1=1;
nerit 7:c9fd242538d9 354 TBmotor_M2=0;
nerit 7:c9fd242538d9 355 TBmotor_M3=0;
nerit 7:c9fd242538d9 356 }else if (TBmotorSteps==4000){
nerit 7:c9fd242538d9 357 TBmotor_M1=0;
nerit 7:c9fd242538d9 358 TBmotor_M2=0;
nerit 7:c9fd242538d9 359 TBmotor_M3=0;
nerit 7:c9fd242538d9 360 }
nerit 7:c9fd242538d9 361 TBmotorRst=0;
nerit 7:c9fd242538d9 362 }
nerit 7:c9fd242538d9 363 //****************************************
nerit 7:c9fd242538d9 364 void dcSetting(){
nerit 7:c9fd242538d9 365 if ((speedFromPick==0)&&(encoder==false)){
nerit 7:c9fd242538d9 366 DcEncoder.rise(&sd25Fall);
nerit 7:c9fd242538d9 367 }
nerit 7:c9fd242538d9 368 if (encoder==true){
nerit 7:c9fd242538d9 369 DcEncoder.rise(&encoRise);
nerit 7:c9fd242538d9 370 //ElementPosition.fall(&encoRise);
nerit 7:c9fd242538d9 371 }
nerit 7:c9fd242538d9 372 }
nerit 0:1e09cd7d66b4 373 //*******************************************************
nerit 0:1e09cd7d66b4 374 void allarmi(){
nerit 0:1e09cd7d66b4 375 uint8_t alarmLowRegister1=0x00;
nerit 0:1e09cd7d66b4 376 alarmLowRegister=0x00;
nerit 0:1e09cd7d66b4 377 alarmHighRegister=0x80;
nerit 0:1e09cd7d66b4 378
nerit 0:1e09cd7d66b4 379 //alarmLowRegister=alarmLowRegister+(all_semiFiniti*0x01); // manca il sensore
nerit 0:1e09cd7d66b4 380 alarmLowRegister=alarmLowRegister+(all_pickSignal*0x02); // fatto
nerit 0:1e09cd7d66b4 381 alarmLowRegister=alarmLowRegister+(all_cellSignal*0x04); // fatto
nerit 0:1e09cd7d66b4 382 alarmLowRegister=alarmLowRegister+(all_lowBattery*0x08); // fatto
nerit 0:1e09cd7d66b4 383 alarmLowRegister=alarmLowRegister+(all_overCurrDC*0x10); // fatto
nerit 0:1e09cd7d66b4 384 alarmLowRegister=alarmLowRegister+(all_stopSistem*0x20); // verificarne la necessità
nerit 0:1e09cd7d66b4 385 //alarmLowRegister=alarmLowRegister+(all_upElements*0x40); // manca il sensore
nerit 7:c9fd242538d9 386 if (seedSensorEnable==true){
nerit 7:c9fd242538d9 387 alarmLowRegister=alarmLowRegister+(all_noSeedOnCe*0x80); // manca il sensore
nerit 7:c9fd242538d9 388 }
nerit 0:1e09cd7d66b4 389
nerit 0:1e09cd7d66b4 390 //alarmLowRegister1=alarmLowRegister1+(all_cfgnErrors*0x01); // da scrivere
nerit 0:1e09cd7d66b4 391 alarmLowRegister1=alarmLowRegister1+(all_noDcRotati*0x02); // fatto
nerit 0:1e09cd7d66b4 392 alarmLowRegister1=alarmLowRegister1+(all_noStepRota*0x04); // fatto
nerit 0:1e09cd7d66b4 393 alarmLowRegister1=alarmLowRegister1+(all_speedError*0x08); // fatto
nerit 0:1e09cd7d66b4 394 alarmLowRegister1=alarmLowRegister1+(all_noSpeedSen*0x10); // fatto
nerit 0:1e09cd7d66b4 395 alarmLowRegister1=alarmLowRegister1+(all_no_Zeroing*0x20); // fatto
nerit 0:1e09cd7d66b4 396 alarmLowRegister1=alarmLowRegister1+(all_genericals*0x40);
nerit 0:1e09cd7d66b4 397 if (alarmLowRegister1 > 0){
nerit 0:1e09cd7d66b4 398 alarmHighRegister = 0x81;
nerit 0:1e09cd7d66b4 399 alarmLowRegister = alarmLowRegister1;
nerit 0:1e09cd7d66b4 400 }
nerit 0:1e09cd7d66b4 401
nerit 0:1e09cd7d66b4 402 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 403 #if defined(VediAllarmi)
nerit 0:1e09cd7d66b4 404 if (all_pickSignal==1){pc.printf("AllarmeBecchi\n");}
nerit 0:1e09cd7d66b4 405 if (all_cellSignal==1){pc.printf("AllarmeCelle\n");}
nerit 0:1e09cd7d66b4 406 if (all_lowBattery==1){pc.printf("AllarmeBassaCorrente\n");}
nerit 0:1e09cd7d66b4 407 if (all_overCurrDC==1){pc.printf("AllarmeAltaCorrente\n");}
nerit 0:1e09cd7d66b4 408 if (all_stopSistem==1){pc.printf("AllarmeStop\n");}
nerit 0:1e09cd7d66b4 409 if (all_noDcRotati==1){pc.printf("AllarmeDCnoRotation\n");}
nerit 0:1e09cd7d66b4 410 if (all_noStepRota==1){pc.printf("AllarmeNoStepRotation\n");}
nerit 0:1e09cd7d66b4 411 if (all_speedError==1){pc.printf("AllarmeSpeedError\n");}
nerit 0:1e09cd7d66b4 412 if (all_noSpeedSen==1){pc.printf("AllarmeNoSpeedSensor\n");}
nerit 0:1e09cd7d66b4 413 if (all_no_Zeroing==1){pc.printf("AllarmeNoZero\n");}
nerit 0:1e09cd7d66b4 414 if (all_genericals==1){pc.printf("AllarmeGenerico\n");}
nerit 0:1e09cd7d66b4 415 pc.printf("Code: 0x%x%x\n",alarmHighRegister,alarmLowRegister);
nerit 0:1e09cd7d66b4 416 #endif
nerit 0:1e09cd7d66b4 417 #endif
nerit 0:1e09cd7d66b4 418 all_semiFiniti=0;
nerit 0:1e09cd7d66b4 419 all_pickSignal=0;
nerit 0:1e09cd7d66b4 420 all_cellSignal=0;
nerit 0:1e09cd7d66b4 421 all_lowBattery=0;
nerit 0:1e09cd7d66b4 422 all_overCurrDC=0;
nerit 0:1e09cd7d66b4 423 all_stopSistem=0;
nerit 0:1e09cd7d66b4 424 all_upElements=0;
nerit 0:1e09cd7d66b4 425 all_noSeedOnCe=0;
nerit 0:1e09cd7d66b4 426 all_cfgnErrors=0;
nerit 0:1e09cd7d66b4 427 all_noDcRotati=0;
nerit 0:1e09cd7d66b4 428 all_noStepRota=0;
nerit 0:1e09cd7d66b4 429 all_speedError=0;
nerit 0:1e09cd7d66b4 430 all_noSpeedSen=0;
nerit 0:1e09cd7d66b4 431 all_no_Zeroing=0;
nerit 0:1e09cd7d66b4 432 all_genericals=0;
nerit 0:1e09cd7d66b4 433 }
nerit 0:1e09cd7d66b4 434 //*******************************************************
nerit 0:1e09cd7d66b4 435 #if defined(speedMaster)
nerit 0:1e09cd7d66b4 436 void upDateSincro(){
nerit 0:1e09cd7d66b4 437 char val1[8]={0,0,0,0,0,0,0,0};
nerit 7:c9fd242538d9 438 val1[3]=(posForQuinc /0x00FF0000)&0x000000FF;
nerit 7:c9fd242538d9 439 val1[2]=(posForQuinc /0x0000FF00)&0x000000FF;
nerit 7:c9fd242538d9 440 val1[1]=(posForQuinc /0x000000FF)&0x000000FF;
nerit 7:c9fd242538d9 441 val1[0]=posForQuinc & 0x000000FF;
nerit 7:c9fd242538d9 442 //double pass = tractorSpeed_MtS_timed*100.0f;
nerit 7:c9fd242538d9 443 double pass = speedOfSeedWheel*100.0f;
nerit 5:3b95bbfe2dc9 444 val1[4]=(uint8_t)(pass)&0x000000FF;
nerit 5:3b95bbfe2dc9 445 val1[5]=(prePosSD /0x0000FF00)&0x000000FF;
nerit 5:3b95bbfe2dc9 446 val1[6]=(prePosSD /0x000000FF)&0x000000FF;
nerit 5:3b95bbfe2dc9 447 val1[7]=prePosSD & 0x000000FF;
nerit 0:1e09cd7d66b4 448 #if defined(canbusActive)
nerit 0:1e09cd7d66b4 449 #if defined(speedMaster)
nerit 0:1e09cd7d66b4 450 if(can1.write(CANMessage(TX_SI, *&val1,8))){
nerit 0:1e09cd7d66b4 451 checkState=0;
nerit 0:1e09cd7d66b4 452 }
nerit 0:1e09cd7d66b4 453 #endif
nerit 0:1e09cd7d66b4 454 #endif
nerit 0:1e09cd7d66b4 455 }
nerit 0:1e09cd7d66b4 456 #endif
nerit 0:1e09cd7d66b4 457 //*******************************************************
nerit 0:1e09cd7d66b4 458 void upDateSpeed(){
nerit 0:1e09cd7d66b4 459 /*
nerit 0:1e09cd7d66b4 460 aggiorna dati OPUSA3
nerit 0:1e09cd7d66b4 461 val1[0] contiene il dato di velocità
nerit 0:1e09cd7d66b4 462 val1[1] contiene il byte basso della tabella allarmi
nerit 0:1e09cd7d66b4 463 uint8_t all_semiFiniti = 0; // semi finiti (generato dal relativo sensore)
nerit 0:1e09cd7d66b4 464 uint8_t all_pickSignal = 0; // errore segnale becchi (generato dal tempo tra un becco ed il successivo)
nerit 0:1e09cd7d66b4 465 uint8_t all_cellSignal = 0; // errore segnale celle (generato dal sensore tamburo )
nerit 0:1e09cd7d66b4 466 uint8_t all_lowBattery = 0; // allarme batteria (valore interno di tritecnica)
nerit 0:1e09cd7d66b4 467 uint8_t all_overCurrDC = 0; // sovracorrente motore DC (generato dal sensore di corrente)
nerit 0:1e09cd7d66b4 468 uint8_t all_stopSistem = 0; // sistema in stop (a tempo con ruota ferma)
nerit 0:1e09cd7d66b4 469 uint8_t all_upElements = 0; // elementi sollevati (generato dal relativo sensore)
nerit 0:1e09cd7d66b4 470 uint8_t all_noSeedOnCe = 0; // fallanza di semina (manca il seme nella cella)
nerit 0:1e09cd7d66b4 471 uint8_t all_cfgnErrors = 0; // errore di configurazione (incongruenza dei parametri impostati)
nerit 0:1e09cd7d66b4 472 uint8_t all_noDcRotati = 0; // ruota di semina bloccata (arriva la velocità ma non i becchi)
nerit 0:1e09cd7d66b4 473 uint8_t all_noStepRota = 0; // tamburo di semina bloccato (comando il tamburo ma non ricevo il sensore)
nerit 0:1e09cd7d66b4 474 uint8_t all_speedError = 0; // errore sensore velocità (tempo impulsi non costante)
nerit 0:1e09cd7d66b4 475 uint8_t all_noSpeedSen = 0; // mancanza segnale di velocità (sento i becchi ma non la velocita)
nerit 0:1e09cd7d66b4 476 uint8_t all_no_Zeroing = 0; // mancato azzeramento sistema (generato dal timeout del comando motore DC)
nerit 0:1e09cd7d66b4 477 uint8_t all_genericals = 0; // allarme generico
nerit 0:1e09cd7d66b4 478 val1[2] contiene il byte alto della tabella di allarmi
nerit 0:1e09cd7d66b4 479 val1[3] contiene i segnali per la diagnostica
nerit 0:1e09cd7d66b4 480 bit 0= sensore ruota fonica
nerit 0:1e09cd7d66b4 481 bit 1= sensore celle
nerit 0:1e09cd7d66b4 482 bit 2= sensore posizione
nerit 0:1e09cd7d66b4 483 bit 3= sensore becchi
nerit 0:1e09cd7d66b4 484 bit 4= motore DC
nerit 0:1e09cd7d66b4 485 bit 5= controller
nerit 0:1e09cd7d66b4 486 bit 6= motore stepper
nerit 0:1e09cd7d66b4 487 */
nerit 0:1e09cd7d66b4 488 char val1[8]={0,0,0,0,0,0,0,0};
nerit 0:1e09cd7d66b4 489
nerit 0:1e09cd7d66b4 490 val1[3]=0;
nerit 0:1e09cd7d66b4 491 val1[3]=val1[3]+(tractorSpeedRead*0x01);
nerit 3:c0f11ca4df02 492 val1[3]=val1[3]+(TBzeroPinInput*0x02);
nerit 7:c9fd242538d9 493 val1[3]=val1[3]+(DcEncoder*0x04);
nerit 3:c0f11ca4df02 494 val1[3]=val1[3]+(seedWheelZeroPinInput*0x08);
nerit 0:1e09cd7d66b4 495 #if defined(simulaPerCan)
nerit 0:1e09cd7d66b4 496 if (buttonUser==0){
nerit 0:1e09cd7d66b4 497 val1[1]=0x02;
nerit 0:1e09cd7d66b4 498 }else{
nerit 0:1e09cd7d66b4 499 val1[1]=0x00;
nerit 0:1e09cd7d66b4 500 }
nerit 0:1e09cd7d66b4 501 val1[3]=valore;
nerit 0:1e09cd7d66b4 502 valore+=1;
nerit 0:1e09cd7d66b4 503 if(valore>50){
nerit 0:1e09cd7d66b4 504 valore=0;
nerit 0:1e09cd7d66b4 505 }
nerit 0:1e09cd7d66b4 506 tractorSpeed_MtS_timed=valore;
nerit 0:1e09cd7d66b4 507 #endif
nerit 0:1e09cd7d66b4 508 allarmi();
nerit 0:1e09cd7d66b4 509 valore = totalSpeed*36.0f; // tractorSpeed_MtS_timed*36.0f;
nerit 0:1e09cd7d66b4 510 val1[0]=valore;
nerit 0:1e09cd7d66b4 511 val1[1]=alarmHighRegister; // registro alto allarmi. Parte sempre da 0x80
nerit 0:1e09cd7d66b4 512 val1[2]=alarmLowRegister; // registro basso allarmi
nerit 0:1e09cd7d66b4 513 //val1[3]=val1[3];// registro di stato degli ingressi
nerit 0:1e09cd7d66b4 514 val1[4]=resetComandi;
nerit 0:1e09cd7d66b4 515 val1[5]=cellsCounterLow;
nerit 0:1e09cd7d66b4 516 val1[6]=cellsCounterHig;
nerit 0:1e09cd7d66b4 517 #if defined(canbusActive)
nerit 0:1e09cd7d66b4 518 if(can1.write(CANMessage(TX_ID, *&val1,8))){
nerit 0:1e09cd7d66b4 519 checkState=0;
nerit 0:1e09cd7d66b4 520 }
nerit 0:1e09cd7d66b4 521 #endif
nerit 0:1e09cd7d66b4 522 }
nerit 0:1e09cd7d66b4 523 //*******************************************************
nerit 0:1e09cd7d66b4 524 void leggiCAN(){
nerit 0:1e09cd7d66b4 525 #if defined(canbusActive)
nerit 0:1e09cd7d66b4 526 if(can1.read(rxMsg)) {
nerit 0:1e09cd7d66b4 527 if (firstStart==1){
nerit 0:1e09cd7d66b4 528 #if defined(speedMaster)
nerit 5:3b95bbfe2dc9 529 sincUpdate.attach(&upDateSincro,0.009f);
nerit 7:c9fd242538d9 530 canUpdate.attach(&upDateSpeed,0.21f);
nerit 7:c9fd242538d9 531 #else
nerit 7:c9fd242538d9 532 canUpdate.attach(&upDateSpeed,0.407f);
nerit 0:1e09cd7d66b4 533 #endif
nerit 0:1e09cd7d66b4 534 firstStart=0;
nerit 0:1e09cd7d66b4 535 }
nerit 0:1e09cd7d66b4 536
nerit 0:1e09cd7d66b4 537 if (rxMsg.id==RX_ID){
nerit 0:1e09cd7d66b4 538 #if defined(pcSerial)
nerit 7:c9fd242538d9 539 #if defined(canDataReceiveda)
nerit 0:1e09cd7d66b4 540 pc.printf("Messaggio ricevuto\n");
nerit 0:1e09cd7d66b4 541 #endif
nerit 0:1e09cd7d66b4 542 #endif
nerit 0:1e09cd7d66b4 543 }
nerit 0:1e09cd7d66b4 544 if (rxMsg.id==RX_Broadcast){
nerit 0:1e09cd7d66b4 545 #if defined(pcSerial)
nerit 7:c9fd242538d9 546 #if defined(canDataReceivedb)
nerit 0:1e09cd7d66b4 547 pc.printf("BroadCast ricevuto\n");
nerit 0:1e09cd7d66b4 548 #endif
nerit 0:1e09cd7d66b4 549 #endif
nerit 0:1e09cd7d66b4 550 enableSimula= rxMsg.data[0];
nerit 0:1e09cd7d66b4 551 speedSimula = rxMsg.data[1];
nerit 0:1e09cd7d66b4 552 avviaSimula = rxMsg.data[2];
nerit 0:1e09cd7d66b4 553 selezionato = rxMsg.data[3];
nerit 0:1e09cd7d66b4 554 comandiDaCan = rxMsg.data[4];
nerit 0:1e09cd7d66b4 555 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 556 #if defined(canDataReceived)
nerit 7:c9fd242538d9 557 pc.printf("Speed simula %d \n",speedSimula);
nerit 0:1e09cd7d66b4 558 #endif
nerit 0:1e09cd7d66b4 559 #endif
nerit 0:1e09cd7d66b4 560 switch (comandiDaCan){
nerit 0:1e09cd7d66b4 561 case 1:
nerit 0:1e09cd7d66b4 562 case 3:
nerit 0:1e09cd7d66b4 563 azzeraDaCan=1;
nerit 0:1e09cd7d66b4 564 resetComandi=0x01;
nerit 0:1e09cd7d66b4 565 comandiDaCan=0;
nerit 0:1e09cd7d66b4 566 break;
nerit 0:1e09cd7d66b4 567 case 2:
nerit 0:1e09cd7d66b4 568 loadDaCan=1;
nerit 0:1e09cd7d66b4 569 resetComandi=0x02;
nerit 0:1e09cd7d66b4 570 comandiDaCan=0;
nerit 0:1e09cd7d66b4 571 break;
nerit 0:1e09cd7d66b4 572 default:
nerit 0:1e09cd7d66b4 573 comandiDaCan=0;
nerit 0:1e09cd7d66b4 574 resetComandi=0xFF;
nerit 0:1e09cd7d66b4 575 break;
nerit 0:1e09cd7d66b4 576 }
nerit 0:1e09cd7d66b4 577 #if defined(pcSerial)
nerit 7:c9fd242538d9 578 #if defined(canDataReceivedR)
nerit 0:1e09cd7d66b4 579 pc.printf("Comandi: %x\n",resetComandi);
nerit 0:1e09cd7d66b4 580 #endif
nerit 0:1e09cd7d66b4 581 #endif
nerit 0:1e09cd7d66b4 582 if (speedSimula>45){
nerit 0:1e09cd7d66b4 583 speedSimula=45;
nerit 0:1e09cd7d66b4 584 }
nerit 0:1e09cd7d66b4 585 // modulo 1
nerit 0:1e09cd7d66b4 586 if (RX_ID==0x100){
nerit 0:1e09cd7d66b4 587 if ((selezionato&0x01)==0x01){
nerit 0:1e09cd7d66b4 588 simOk=1;
nerit 0:1e09cd7d66b4 589 }else{
nerit 0:1e09cd7d66b4 590 simOk=0;
nerit 0:1e09cd7d66b4 591 }
nerit 0:1e09cd7d66b4 592 }
nerit 0:1e09cd7d66b4 593 // modulo 2
nerit 0:1e09cd7d66b4 594 if (RX_ID==0x102){
nerit 0:1e09cd7d66b4 595 if ((selezionato&0x02)==0x02){
nerit 0:1e09cd7d66b4 596 simOk=1;
nerit 0:1e09cd7d66b4 597 }else{
nerit 0:1e09cd7d66b4 598 simOk=0;
nerit 0:1e09cd7d66b4 599 }
nerit 0:1e09cd7d66b4 600 }
nerit 0:1e09cd7d66b4 601 // modulo 3
nerit 0:1e09cd7d66b4 602 if (RX_ID==0x104){
nerit 0:1e09cd7d66b4 603 if ((selezionato&0x04)==0x04){
nerit 0:1e09cd7d66b4 604 simOk=1;
nerit 0:1e09cd7d66b4 605 }else{
nerit 0:1e09cd7d66b4 606 simOk=0;
nerit 0:1e09cd7d66b4 607 }
nerit 0:1e09cd7d66b4 608 }
nerit 0:1e09cd7d66b4 609 // modulo 4
nerit 0:1e09cd7d66b4 610 if (RX_ID==0x106){
nerit 0:1e09cd7d66b4 611 if ((selezionato&0x08)==0x08){
nerit 0:1e09cd7d66b4 612 simOk=1;
nerit 0:1e09cd7d66b4 613 }else{
nerit 0:1e09cd7d66b4 614 simOk=0;
nerit 0:1e09cd7d66b4 615 }
nerit 0:1e09cd7d66b4 616 }
nerit 0:1e09cd7d66b4 617 // modulo 5
nerit 0:1e09cd7d66b4 618 if (RX_ID==0x108){
nerit 0:1e09cd7d66b4 619 if ((selezionato&0x10)==0x10){
nerit 0:1e09cd7d66b4 620 simOk=1;
nerit 0:1e09cd7d66b4 621 }else{
nerit 0:1e09cd7d66b4 622 simOk=0;
nerit 0:1e09cd7d66b4 623 }
nerit 0:1e09cd7d66b4 624 }
nerit 0:1e09cd7d66b4 625
nerit 0:1e09cd7d66b4 626 }
nerit 0:1e09cd7d66b4 627 if (rxMsg.id==RX_Settings){
nerit 0:1e09cd7d66b4 628 if (tractorSpeed_MtS_timed==0.0f){
nerit 0:1e09cd7d66b4 629 pickNumber = rxMsg.data[0]; // numero di becchi installati sulla ruota di semina
nerit 0:1e09cd7d66b4 630 cellsNumber = rxMsg.data[1]; // numero di celle del tamburo
nerit 0:1e09cd7d66b4 631 deepOfSeed=(rxMsg.data[2]/10000.0f); // deep of seeding
nerit 0:1e09cd7d66b4 632 seedWheelDiameter = ((rxMsg.data[4]*0xFF)+rxMsg.data[3])/10000.0f; // seed wheel diameter setting
nerit 0:1e09cd7d66b4 633 speedWheelDiameter = ((rxMsg.data[6]*0xFF)+rxMsg.data[5])/10000.0f; // variable for tractor speed calculation (need to be set from UI) ( Unit= meters )
nerit 0:1e09cd7d66b4 634 speedWheelPulse = rxMsg.data[7]; // variable which define the number of pulse each turn of tractor speed wheel (need to be set from UI)
nerit 0:1e09cd7d66b4 635 aggiornaParametri();
nerit 0:1e09cd7d66b4 636 }
nerit 0:1e09cd7d66b4 637 }
nerit 0:1e09cd7d66b4 638 if (rxMsg.id==RX_AngoloPh){
nerit 0:1e09cd7d66b4 639 if (tractorSpeed_MtS_timed==0.0f){
nerit 0:1e09cd7d66b4 640 #if defined(M1)
nerit 0:1e09cd7d66b4 641 angoloPh = (double) rxMsg.data[0] ;
nerit 0:1e09cd7d66b4 642 aggiornaParametri();
nerit 0:1e09cd7d66b4 643 #endif
nerit 0:1e09cd7d66b4 644 #if defined(M2)
nerit 0:1e09cd7d66b4 645 angoloPh = (double) rxMsg.data[1] ;
nerit 0:1e09cd7d66b4 646 aggiornaParametri();
nerit 0:1e09cd7d66b4 647 #endif
nerit 0:1e09cd7d66b4 648 #if defined(M3)
nerit 0:1e09cd7d66b4 649 angoloPh = (double) rxMsg.data[2] ;
nerit 0:1e09cd7d66b4 650 aggiornaParametri();
nerit 0:1e09cd7d66b4 651 #endif
nerit 0:1e09cd7d66b4 652 #if defined(M4)
nerit 0:1e09cd7d66b4 653 angoloPh = (double) rxMsg.data[3] ;
nerit 0:1e09cd7d66b4 654 aggiornaParametri();
nerit 0:1e09cd7d66b4 655 #endif
nerit 0:1e09cd7d66b4 656 #if defined(M5)
nerit 0:1e09cd7d66b4 657 angoloPh = (double) rxMsg.data[4] ;
nerit 0:1e09cd7d66b4 658 aggiornaParametri();
nerit 0:1e09cd7d66b4 659 #endif
nerit 0:1e09cd7d66b4 660 #if defined(M6)
nerit 0:1e09cd7d66b4 661 angoloPh = (double) rxMsg.data[5] ;
nerit 0:1e09cd7d66b4 662 aggiornaParametri();
nerit 0:1e09cd7d66b4 663 #endif
nerit 0:1e09cd7d66b4 664 }
nerit 0:1e09cd7d66b4 665 }
nerit 0:1e09cd7d66b4 666 if (rxMsg.id==RX_AngoloAv){
nerit 0:1e09cd7d66b4 667 if (tractorSpeed_MtS_timed==0.0f){
nerit 0:1e09cd7d66b4 668 #if defined(M1)
nerit 0:1e09cd7d66b4 669 angoloAv = (double) rxMsg.data[0] ;
nerit 0:1e09cd7d66b4 670 aggiornaParametri();
nerit 0:1e09cd7d66b4 671 #endif
nerit 0:1e09cd7d66b4 672 #if defined(M2)
nerit 0:1e09cd7d66b4 673 angoloAv = (double) rxMsg.data[1] ;
nerit 0:1e09cd7d66b4 674 aggiornaParametri();
nerit 0:1e09cd7d66b4 675 #endif
nerit 0:1e09cd7d66b4 676 #if defined(M3)
nerit 0:1e09cd7d66b4 677 angoloAv = (double) rxMsg.data[2] ;
nerit 0:1e09cd7d66b4 678 aggiornaParametri();
nerit 0:1e09cd7d66b4 679 #endif
nerit 0:1e09cd7d66b4 680 #if defined(M4)
nerit 0:1e09cd7d66b4 681 angoloAv = (double) rxMsg.data[3] ;
nerit 0:1e09cd7d66b4 682 aggiornaParametri();
nerit 0:1e09cd7d66b4 683 #endif
nerit 0:1e09cd7d66b4 684 #if defined(M5)
nerit 0:1e09cd7d66b4 685 angoloAv = (double) rxMsg.data[4] ;
nerit 0:1e09cd7d66b4 686 aggiornaParametri();
nerit 0:1e09cd7d66b4 687 #endif
nerit 0:1e09cd7d66b4 688 #if defined(M6)
nerit 0:1e09cd7d66b4 689 angoloAv = (double) rxMsg.data[5] ;
nerit 0:1e09cd7d66b4 690 aggiornaParametri();
nerit 0:1e09cd7d66b4 691 #endif
nerit 0:1e09cd7d66b4 692 }
nerit 0:1e09cd7d66b4 693 }
nerit 0:1e09cd7d66b4 694 if (rxMsg.id==RX_Quinconce){
nerit 0:1e09cd7d66b4 695 if (tractorSpeed_MtS_timed==0.0f){
nerit 4:d32258ec411f 696 quinconceActive = (uint8_t) rxMsg.data[0];
nerit 5:3b95bbfe2dc9 697 quincPIDminus = (uint8_t) rxMsg.data[1];
nerit 5:3b95bbfe2dc9 698 quincPIDplus = (uint8_t) rxMsg.data[2];
nerit 5:3b95bbfe2dc9 699 quincLIMminus = (uint8_t) rxMsg.data[3];
nerit 5:3b95bbfe2dc9 700 quincLIMplus = (uint8_t) rxMsg.data[4];
nerit 5:3b95bbfe2dc9 701 quincSector = (uint8_t) rxMsg.data[5];
nerit 0:1e09cd7d66b4 702 aggiornaParametri();
nerit 0:1e09cd7d66b4 703 }
nerit 0:1e09cd7d66b4 704 }
nerit 0:1e09cd7d66b4 705 if (rxMsg.id==RX_QuincSinc){
nerit 7:c9fd242538d9 706 masterSinc = (uint32_t) rxMsg.data[3] * 0x00FF0000;
nerit 7:c9fd242538d9 707 masterSinc = masterSinc + ((uint32_t) rxMsg.data[2] * 0x0000FF00);
nerit 7:c9fd242538d9 708 masterSinc = masterSinc + ((uint32_t) rxMsg.data[1] * 0x000000FF);
nerit 7:c9fd242538d9 709 masterSinc = masterSinc + ((uint32_t) rxMsg.data[0]);
nerit 5:3b95bbfe2dc9 710 speedFromMaster = (double)rxMsg.data[4]/100.0f;
nerit 5:3b95bbfe2dc9 711 mast2_Sinc = ((uint32_t) rxMsg.data[5] * 0x0000FF00);
nerit 5:3b95bbfe2dc9 712 mast2_Sinc = mast2_Sinc + ((uint32_t) rxMsg.data[6] * 0x000000FF);
nerit 5:3b95bbfe2dc9 713 mast2_Sinc = mast2_Sinc + ((uint32_t) rxMsg.data[7]);
nerit 5:3b95bbfe2dc9 714 canDataCheck=1;
nerit 0:1e09cd7d66b4 715 }
nerit 6:3fca0ca1949e 716 if (rxMsg.id==RX_Configure){
nerit 6:3fca0ca1949e 717 uint8_t flags = rxMsg.data[0];
nerit 6:3fca0ca1949e 718 uint16_t steps = (uint32_t) rxMsg.data[1]*0xFF00;
nerit 6:3fca0ca1949e 719 steps = steps + ((uint32_t)rxMsg.data[2]);
nerit 7:c9fd242538d9 720 stepSetting();
nerit 6:3fca0ca1949e 721 cellsCountSet = rxMsg.data[3];
nerit 6:3fca0ca1949e 722 if ((flags&0x01)==0x01){
nerit 7:c9fd242538d9 723 if (encoder==false){
nerit 7:c9fd242538d9 724 encoder=true;
nerit 7:c9fd242538d9 725 DcEncoder.rise(NULL);
nerit 7:c9fd242538d9 726 dcSetting();
nerit 7:c9fd242538d9 727 }
nerit 6:3fca0ca1949e 728 }else{
nerit 7:c9fd242538d9 729 if (encoder==true){
nerit 7:c9fd242538d9 730 encoder=false;
nerit 7:c9fd242538d9 731 DcEncoder.rise(NULL);
nerit 7:c9fd242538d9 732 dcSetting();
nerit 7:c9fd242538d9 733 }
nerit 6:3fca0ca1949e 734 }
nerit 6:3fca0ca1949e 735 if ((flags&0x02)==0x02){
nerit 6:3fca0ca1949e 736 tankLevelEnable=true;
nerit 6:3fca0ca1949e 737 }else{
nerit 6:3fca0ca1949e 738 tankLevelEnable=false;
nerit 6:3fca0ca1949e 739 }
nerit 6:3fca0ca1949e 740 if ((flags&0x04)==0x04){
nerit 6:3fca0ca1949e 741 seedSensorEnable=true;
nerit 6:3fca0ca1949e 742 }else{
nerit 6:3fca0ca1949e 743 seedSensorEnable=false;
nerit 6:3fca0ca1949e 744 }
nerit 6:3fca0ca1949e 745 if ((flags&0x08)==0x08){
nerit 6:3fca0ca1949e 746 stendiNylonEnable=true;
nerit 6:3fca0ca1949e 747 }else{
nerit 6:3fca0ca1949e 748 stendiNylonEnable=false;
nerit 6:3fca0ca1949e 749 }
nerit 6:3fca0ca1949e 750 if ((flags&0x10)==0x10){
nerit 6:3fca0ca1949e 751 canDataCheckEnable=true;
nerit 6:3fca0ca1949e 752 }else{
nerit 6:3fca0ca1949e 753 canDataCheckEnable=false;
nerit 6:3fca0ca1949e 754 }
nerit 6:3fca0ca1949e 755 if ((flags&0x20)==0x20){
nerit 6:3fca0ca1949e 756 tamburoStandard=1;
nerit 6:3fca0ca1949e 757 }else{
nerit 6:3fca0ca1949e 758 tamburoStandard=0;
nerit 6:3fca0ca1949e 759 }
nerit 6:3fca0ca1949e 760 if ((flags&0x40)==0x40){
nerit 6:3fca0ca1949e 761 currentCheckEnable=true;
nerit 6:3fca0ca1949e 762 }else{
nerit 6:3fca0ca1949e 763 currentCheckEnable=false;
nerit 6:3fca0ca1949e 764 }
nerit 6:3fca0ca1949e 765 }
nerit 0:1e09cd7d66b4 766 }
nerit 0:1e09cd7d66b4 767 #endif
nerit 0:1e09cd7d66b4 768 #if defined(overWriteCanSimulation)
nerit 0:1e09cd7d66b4 769 enableSimula=1;
nerit 0:1e09cd7d66b4 770 speedSimula=25;
nerit 0:1e09cd7d66b4 771 avviaSimula=1;
nerit 0:1e09cd7d66b4 772 simOk=1;
nerit 0:1e09cd7d66b4 773 #endif
nerit 0:1e09cd7d66b4 774
nerit 0:1e09cd7d66b4 775 }
nerit 0:1e09cd7d66b4 776
nerit 0:1e09cd7d66b4 777 //*******************************************************
nerit 0:1e09cd7d66b4 778 void DC_CheckCurrent(){
nerit 0:1e09cd7d66b4 779
nerit 0:1e09cd7d66b4 780 // TODO: tabella di riferimento assorbimenti alle varie velocità al fine di gestire
nerit 0:1e09cd7d66b4 781 // gli allarmi e le correzioni di velocità
nerit 0:1e09cd7d66b4 782
nerit 0:1e09cd7d66b4 783 //float SD_analogMatrix[10]={0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
nerit 0:1e09cd7d66b4 784 //int SD_analogIndex[10]={0,0,0,0,0,0,0,0,0,0};
nerit 0:1e09cd7d66b4 785 // Analog reading
nerit 0:1e09cd7d66b4 786 //number = floor(number * 100) / 100;
nerit 0:1e09cd7d66b4 787 //if (pwmCheck==1){
nerit 0:1e09cd7d66b4 788 timeout.detach();
nerit 0:1e09cd7d66b4 789 for (int ii=1;ii<20;ii++){
nerit 0:1e09cd7d66b4 790 SD_analogMatrix[ii]=SD_analogMatrix[ii+1];
nerit 0:1e09cd7d66b4 791 }
nerit 0:1e09cd7d66b4 792 SD_CurrentAnalog = floor(SDcurrent.read()*100)/100; // valore in ingresso compreso tra 0.00 e 1.00
nerit 0:1e09cd7d66b4 793 SD_analogMatrix[20]=SD_CurrentAnalog;
nerit 0:1e09cd7d66b4 794 if (SDmotorPWM==0.0f){
nerit 0:1e09cd7d66b4 795 SD_CurrentStart=SD_CurrentAnalog;
nerit 0:1e09cd7d66b4 796 }
nerit 0:1e09cd7d66b4 797 float sommaTutto=0.0f;
nerit 0:1e09cd7d66b4 798 for (int ii=1;ii<21;ii++){
nerit 0:1e09cd7d66b4 799 sommaTutto=sommaTutto+SD_analogMatrix[ii];
nerit 0:1e09cd7d66b4 800 }
nerit 0:1e09cd7d66b4 801 float SD_CurrentAnalogica=sommaTutto/20.0f;
nerit 0:1e09cd7d66b4 802 SD_CurrentScaled = floor(( (SD_CurrentAnalogica - SD_CurrentStart)*3.3f) / (SD_CurrentFactor/1000.0f)*10)/10;
nerit 0:1e09cd7d66b4 803 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 804 #if defined(correnteAssorbita)
nerit 0:1e09cd7d66b4 805 pc.printf("CorrenteStart: %f ",SD_CurrentStart);
nerit 0:1e09cd7d66b4 806 pc.printf(" CorrenteScaled: %f ",SD_CurrentScaled);
nerit 0:1e09cd7d66b4 807 pc.printf(" CorrenteMedia: %f \n",SD_CurrentAnalogica);
nerit 0:1e09cd7d66b4 808 #endif
nerit 0:1e09cd7d66b4 809 #endif
nerit 0:1e09cd7d66b4 810 reduceCurrent=false;
nerit 0:1e09cd7d66b4 811 incrementCurrent=false;
nerit 0:1e09cd7d66b4 812 /*
nerit 0:1e09cd7d66b4 813 if (SD_CurrentScaled < 3.0f){
nerit 0:1e09cd7d66b4 814 #if defined(canbusActive)
nerit 0:1e09cd7d66b4 815 all_lowBattery=1;
nerit 0:1e09cd7d66b4 816 #endif
nerit 0:1e09cd7d66b4 817 incrementCurrent=true;
nerit 0:1e09cd7d66b4 818 }
nerit 0:1e09cd7d66b4 819 */
nerit 4:d32258ec411f 820 if (SD_CurrentScaled > 10.0f){
nerit 4:d32258ec411f 821 timeCurr.start();
nerit 4:d32258ec411f 822 if (timeCurr.read() > 5.0f){
nerit 4:d32258ec411f 823 #if defined(canbusActive)
nerit 4:d32258ec411f 824 all_overCurrDC=1;
nerit 4:d32258ec411f 825 #endif
nerit 4:d32258ec411f 826 reduceCurrent=true;
nerit 4:d32258ec411f 827 timeCurr.reset();
nerit 4:d32258ec411f 828 }
nerit 4:d32258ec411f 829 }else{
nerit 4:d32258ec411f 830 timeCurr.stop();
nerit 0:1e09cd7d66b4 831 }
nerit 0:1e09cd7d66b4 832 //}
nerit 0:1e09cd7d66b4 833 }
nerit 0:1e09cd7d66b4 834 //*******************************************************
nerit 0:1e09cd7d66b4 835 void DC_prepare(){
nerit 0:1e09cd7d66b4 836 // direction or brake preparation
nerit 7:c9fd242538d9 837 if (DC_brake==1){
nerit 7:c9fd242538d9 838 SDmotorInA=1;
nerit 7:c9fd242538d9 839 SDmotorInB=1;
nerit 7:c9fd242538d9 840 }else{
nerit 7:c9fd242538d9 841 if (DC_forward==0){
nerit 7:c9fd242538d9 842 SDmotorInA=1;
nerit 7:c9fd242538d9 843 SDmotorInB=0;
nerit 7:c9fd242538d9 844 }else{
nerit 7:c9fd242538d9 845 SDmotorInA=0;
nerit 7:c9fd242538d9 846 SDmotorInB=1;
nerit 7:c9fd242538d9 847 }
nerit 7:c9fd242538d9 848 }
nerit 0:1e09cd7d66b4 849 // fault reading
nerit 0:1e09cd7d66b4 850 if (SDmotorInA==1){SD_faultA=1;}else{SD_faultA=0;}
nerit 0:1e09cd7d66b4 851 if (SDmotorInB==1){SD_faultB=1;}else{SD_faultB=0;}
nerit 0:1e09cd7d66b4 852 }
nerit 0:1e09cd7d66b4 853 //*******************************************************
nerit 0:1e09cd7d66b4 854 void startDelay(){
nerit 0:1e09cd7d66b4 855 int ritardo =0;
nerit 0:1e09cd7d66b4 856 ritardo = (int)((float)(dcActualDuty*800.0f));
nerit 0:1e09cd7d66b4 857 timeout.attach_us(&DC_CheckCurrent,ritardo);
nerit 0:1e09cd7d66b4 858 }
nerit 0:1e09cd7d66b4 859 //*******************************************************
nerit 4:d32258ec411f 860 void quincTrigon(){
nerit 4:d32258ec411f 861 quincClock=true;
nerit 4:d32258ec411f 862 }
nerit 4:d32258ec411f 863 void quincTrigof(){
nerit 4:d32258ec411f 864 quincClock=false;
nerit 0:1e09cd7d66b4 865 }
nerit 0:1e09cd7d66b4 866 //*******************************************************
nerit 2:d9c7430ae953 867 void quinCalc(){
nerit 0:1e09cd7d66b4 868 // riceve l'impulso di sincro dal master e fa partire il timer di verifica dell'errore
nerit 2:d9c7430ae953 869 #if !defined(mezzo)
nerit 0:1e09cd7d66b4 870 if ((quincClock==true)&&(oldQuincIn==0)){
nerit 0:1e09cd7d66b4 871 oldQuincIn=1;
nerit 0:1e09cd7d66b4 872 if (quincStart==0){
nerit 5:3b95bbfe2dc9 873 oldQuincTimeSD = (double) quincTimeSD.read_ms();
nerit 0:1e09cd7d66b4 874 quincTime.reset();
nerit 0:1e09cd7d66b4 875 quincStart=1;
nerit 0:1e09cd7d66b4 876 }
nerit 0:1e09cd7d66b4 877 }
nerit 2:d9c7430ae953 878 if(quincClock==false){
nerit 0:1e09cd7d66b4 879 oldQuincIn=0;
nerit 0:1e09cd7d66b4 880 }
nerit 2:d9c7430ae953 881 #else
nerit 2:d9c7430ae953 882 if ((((quinconceActive==0)&&(quincClock==true))||((quinconceActive==1)&&(quincClock==false)))&&(oldQuincIn==0)){
nerit 0:1e09cd7d66b4 883 oldQuincIn=1;
nerit 0:1e09cd7d66b4 884 if (quincStart==0){
nerit 5:3b95bbfe2dc9 885 oldQuincTimeSD = (double) quincTimeSD.read_ms();
nerit 0:1e09cd7d66b4 886 quincTime.reset();
nerit 0:1e09cd7d66b4 887 quincStart=1;
nerit 0:1e09cd7d66b4 888 }
nerit 0:1e09cd7d66b4 889 }
nerit 4:d32258ec411f 890 if (quinconceActive==0){
nerit 4:d32258ec411f 891 if (quincClock==false){
nerit 4:d32258ec411f 892 oldQuincIn=0;
nerit 4:d32258ec411f 893 }
nerit 4:d32258ec411f 894 }else{
nerit 4:d32258ec411f 895 if (quincClock==true){
nerit 4:d32258ec411f 896 oldQuincIn=0;
nerit 4:d32258ec411f 897 }
nerit 0:1e09cd7d66b4 898 }
nerit 2:d9c7430ae953 899 #endif
nerit 5:3b95bbfe2dc9 900 //****************************************************************************************
nerit 7:c9fd242538d9 901 if (quincCnt>=4){
nerit 5:3b95bbfe2dc9 902 if (countPicks==0){
nerit 5:3b95bbfe2dc9 903 if ((sincroQui==1)&&(quincStart==0)){
nerit 5:3b95bbfe2dc9 904 // decelera
nerit 5:3b95bbfe2dc9 905 countPicks=1;
nerit 4:d32258ec411f 906 }
nerit 5:3b95bbfe2dc9 907 if ((sincroQui==0)&&(quincStart==1)){
nerit 5:3b95bbfe2dc9 908 // accelera
nerit 5:3b95bbfe2dc9 909 countPicks=2;
nerit 4:d32258ec411f 910 }
nerit 4:d32258ec411f 911 }
nerit 5:3b95bbfe2dc9 912 if ((sincroQui==1)&&(quincStart==1)){
nerit 5:3b95bbfe2dc9 913 if (countPicks==1){ //decelera
nerit 5:3b95bbfe2dc9 914 scostamento = oldQuincTimeSD;
nerit 5:3b95bbfe2dc9 915 if (scostamento < (tempoBecchiPerQuinc*0.75f)){
nerit 5:3b95bbfe2dc9 916 double scostPerc = (scostamento/tempoBecchiPerQuinc);
nerit 7:c9fd242538d9 917 percento -= ((double)quincPIDminus/100.0f)*(scostPerc);
nerit 5:3b95bbfe2dc9 918 #if defined(pcSerial)
nerit 5:3b95bbfe2dc9 919 #if defined(laq)
nerit 5:3b95bbfe2dc9 920 pc.printf("RALL scos2: %f tbpq: %f percento: %f \n",scostamento,tempoBecchiPerQuinc,percento);
nerit 5:3b95bbfe2dc9 921 #endif
nerit 5:3b95bbfe2dc9 922 #endif
nerit 5:3b95bbfe2dc9 923 }
nerit 7:c9fd242538d9 924 //if (scostamento <15.0f){percento=0.0f;}
nerit 5:3b95bbfe2dc9 925 }
nerit 5:3b95bbfe2dc9 926 if (countPicks==2){ //accelera
nerit 5:3b95bbfe2dc9 927 scostamento = (double)quincTime.read_ms();
nerit 5:3b95bbfe2dc9 928 if (scostamento < (tempoBecchiPerQuinc*0.75f)){
nerit 5:3b95bbfe2dc9 929 double scostPerc = (scostamento/tempoBecchiPerQuinc);
nerit 7:c9fd242538d9 930 percento += ((double)quincPIDplus/100.0f)*(scostPerc);
nerit 5:3b95bbfe2dc9 931 #if defined(pcSerial)
nerit 5:3b95bbfe2dc9 932 #if defined(laq)
nerit 5:3b95bbfe2dc9 933 pc.printf(
nerit 5:3b95bbfe2dc9 934 "ACCE scos1: %f tbpq: %f percento: %f \n",scostamento,tempoBecchiPerQuinc,percento);
nerit 5:3b95bbfe2dc9 935 #endif
nerit 5:3b95bbfe2dc9 936 #endif
nerit 5:3b95bbfe2dc9 937 }
nerit 7:c9fd242538d9 938 //if (scostamento <15.0f){percento=0.0f;}
nerit 5:3b95bbfe2dc9 939 }
nerit 5:3b95bbfe2dc9 940 sincroQui=0;
nerit 5:3b95bbfe2dc9 941 quincStart=0;
nerit 5:3b95bbfe2dc9 942 countPicks=0;
nerit 7:c9fd242538d9 943 #if !defined(speedMaster)
nerit 7:c9fd242538d9 944 if (quincCnt>=5){
nerit 7:c9fd242538d9 945 if (speedFromMaster>0.0f){
nerit 7:c9fd242538d9 946 tractorSpeed_MtS_timed = speedFromMaster + percento;
nerit 7:c9fd242538d9 947 }
nerit 7:c9fd242538d9 948 }
nerit 7:c9fd242538d9 949 #endif
nerit 5:3b95bbfe2dc9 950 }
nerit 5:3b95bbfe2dc9 951
nerit 5:3b95bbfe2dc9 952 //*******************************************************************
nerit 5:3b95bbfe2dc9 953 if (canDataCheckEnable==true){
nerit 5:3b95bbfe2dc9 954 if (canDataCheck==1){ // sincro da comunicazione can del valore di posizione del tamburo master
nerit 5:3b95bbfe2dc9 955 canDataCheck=0;
nerit 7:c9fd242538d9 956 double parametro = SDsectorStep/3.0f;
nerit 5:3b95bbfe2dc9 957 double differenza=0.0f;
nerit 5:3b95bbfe2dc9 958 #if defined(mezzo)
nerit 5:3b95bbfe2dc9 959 if (quinconceActive==1){
nerit 5:3b95bbfe2dc9 960 differenza = (double)masterSinc - (double)prePosSD;
nerit 5:3b95bbfe2dc9 961 }else{
nerit 5:3b95bbfe2dc9 962 differenza = (double)mast2_Sinc - (double)prePosSD;
nerit 5:3b95bbfe2dc9 963 }
nerit 5:3b95bbfe2dc9 964 #else
nerit 5:3b95bbfe2dc9 965 differenza = (double)mast2_Sinc - (double)prePosSD;
nerit 5:3b95bbfe2dc9 966 #endif
nerit 5:3b95bbfe2dc9 967 if ((differenza > 0.0f)&&(differenza < parametro)){
nerit 5:3b95bbfe2dc9 968 double diffPerc = differenza / parametro;
nerit 7:c9fd242538d9 969 percento += ((double)quincPIDplus/100.0f)*abs(diffPerc);
nerit 5:3b95bbfe2dc9 970 #if defined(pcSerial)
nerit 5:3b95bbfe2dc9 971 #if defined(quinca)
nerit 5:3b95bbfe2dc9 972 pc.printf("m1 %d m2 %d prePo %d diffe %f perce %f parm %f %\n",masterSinc, mast2_Sinc,prePosSD,differenza,percento, parametro);
nerit 5:3b95bbfe2dc9 973 #endif
nerit 5:3b95bbfe2dc9 974 #endif
nerit 5:3b95bbfe2dc9 975 }
nerit 5:3b95bbfe2dc9 976 if ((differenza < 0.0f)&&(abs(differenza) < parametro)){
nerit 5:3b95bbfe2dc9 977 double diffPerc = (double)differenza / parametro;
nerit 7:c9fd242538d9 978 percento -= ((double)quincPIDminus/100.0f)*abs(diffPerc);
nerit 5:3b95bbfe2dc9 979 #if defined(pcSerial)
nerit 5:3b95bbfe2dc9 980 #if defined(quinca)
nerit 5:3b95bbfe2dc9 981 pc.printf("m1 %d m2 %d prePo %d diffe %f perce %f parm %f %\n",masterSinc, mast2_Sinc,prePosSD,differenza,percento, parametro);
nerit 5:3b95bbfe2dc9 982 #endif
nerit 5:3b95bbfe2dc9 983 #endif
nerit 5:3b95bbfe2dc9 984 }
nerit 7:c9fd242538d9 985
nerit 7:c9fd242538d9 986 #if !defined(speedMaster)
nerit 7:c9fd242538d9 987 if (quincCnt>=5){
nerit 7:c9fd242538d9 988 if (speedFromMaster>0.0f){
nerit 7:c9fd242538d9 989 tractorSpeed_MtS_timed = speedFromMaster + percento;
nerit 7:c9fd242538d9 990 }
nerit 7:c9fd242538d9 991 }
nerit 7:c9fd242538d9 992 #endif
nerit 5:3b95bbfe2dc9 993 }
nerit 5:3b95bbfe2dc9 994 }
nerit 5:3b95bbfe2dc9 995
nerit 5:3b95bbfe2dc9 996 }
nerit 5:3b95bbfe2dc9 997 if ((percento) > ((double) quincLIMplus/100.0f)){
nerit 5:3b95bbfe2dc9 998 percento= (double)quincLIMplus/100.0f;
nerit 5:3b95bbfe2dc9 999 }
nerit 5:3b95bbfe2dc9 1000 if ((percento) < (((double)quincLIMminus*-1.0f)/100.0f)){
nerit 5:3b95bbfe2dc9 1001 percento=((double)quincLIMminus*-1.0f)/100.0f;
nerit 0:1e09cd7d66b4 1002 }
nerit 0:1e09cd7d66b4 1003 }
nerit 7:c9fd242538d9 1004 // ----------------------------------------
nerit 7:c9fd242538d9 1005 #if defined(seedSensor)
nerit 7:c9fd242538d9 1006 void resetDelay(){
nerit 7:c9fd242538d9 1007 delaySeedCheck.reset();
nerit 7:c9fd242538d9 1008 delaySeedCheck.stop();
nerit 7:c9fd242538d9 1009 }
nerit 7:c9fd242538d9 1010 #endif
nerit 0:1e09cd7d66b4 1011 // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
nerit 0:1e09cd7d66b4 1012 // MAIN SECTION
nerit 3:c0f11ca4df02 1013 // ---------------------------------------------------------------------------------------------------------------------------------------------------------------
nerit 0:1e09cd7d66b4 1014
nerit 0:1e09cd7d66b4 1015 int main()
nerit 0:1e09cd7d66b4 1016 {
nerit 5:3b95bbfe2dc9 1017 //wait(1.0f);
nerit 0:1e09cd7d66b4 1018 wd.Configure(2); //watchdog set at xx seconds
nerit 7:c9fd242538d9 1019
nerit 7:c9fd242538d9 1020 stepSetting();
nerit 7:c9fd242538d9 1021
nerit 0:1e09cd7d66b4 1022 for (int a=0; a<5;a++){
nerit 0:1e09cd7d66b4 1023 mediaSpeed[a]=0;
nerit 0:1e09cd7d66b4 1024 }
nerit 0:1e09cd7d66b4 1025
nerit 0:1e09cd7d66b4 1026
nerit 0:1e09cd7d66b4 1027 // DC reset ad set
nerit 0:1e09cd7d66b4 1028 int decima = 100;
nerit 0:1e09cd7d66b4 1029 wait_ms(200);
nerit 0:1e09cd7d66b4 1030 SD_CurrentStart=floor(SDcurrent.read()*decima)/decima;
nerit 0:1e09cd7d66b4 1031 wait_ms(2);
nerit 0:1e09cd7d66b4 1032 SD_CurrentStart+=floor(SDcurrent.read()*decima)/decima;
nerit 0:1e09cd7d66b4 1033 wait_ms(1);
nerit 0:1e09cd7d66b4 1034 SD_CurrentStart+=floor(SDcurrent.read()*decima)/decima;
nerit 0:1e09cd7d66b4 1035 wait_ms(3);
nerit 0:1e09cd7d66b4 1036 SD_CurrentStart+=floor(SDcurrent.read()*decima)/decima;
nerit 0:1e09cd7d66b4 1037 SD_CurrentStart=(floor((SD_CurrentStart/4.0f)*decima)/decima)-0.01f;
nerit 0:1e09cd7d66b4 1038 wait_ms(100);
nerit 0:1e09cd7d66b4 1039 DC_prepare();
nerit 0:1e09cd7d66b4 1040
nerit 0:1e09cd7d66b4 1041 speedTimer.start(); // speed pulse timer
nerit 0:1e09cd7d66b4 1042 intraPickTimer.start();
nerit 0:1e09cd7d66b4 1043 speedTimeOut.start();
nerit 0:1e09cd7d66b4 1044 speedFilter.start();
nerit 0:1e09cd7d66b4 1045 seedFilter.start();
nerit 0:1e09cd7d66b4 1046 TBfilter.start();
nerit 0:1e09cd7d66b4 1047 sincroTimer.start();
nerit 0:1e09cd7d66b4 1048 rotationTimeOut.start();
nerit 0:1e09cd7d66b4 1049 metalTimer.start();
nerit 0:1e09cd7d66b4 1050 quincTime.start();
nerit 1:e88bf5011af6 1051 quincTimeSD.start();
nerit 7:c9fd242538d9 1052 //intraEncoTimer.start();
nerit 0:1e09cd7d66b4 1053
nerit 0:1e09cd7d66b4 1054 //*******************************************************
nerit 0:1e09cd7d66b4 1055 // controls for check DC motor current
nerit 0:1e09cd7d66b4 1056 pwmCheck.rise(&startDelay);
nerit 0:1e09cd7d66b4 1057
nerit 0:1e09cd7d66b4 1058 if (inProva==0){
nerit 0:1e09cd7d66b4 1059 tractorSpeedRead.rise(&tractorReadSpeed);
nerit 0:1e09cd7d66b4 1060 #if !defined(speedMaster)
nerit 4:d32258ec411f 1061 quinconceIn.rise(&quincTrigon);
nerit 4:d32258ec411f 1062 quinconceIn.fall(&quincTrigof);
nerit 0:1e09cd7d66b4 1063 #endif
nerit 5:3b95bbfe2dc9 1064 #if defined(speedMaster)
nerit 5:3b95bbfe2dc9 1065 tftUpdate.attach(&videoUpdate,0.50f);
nerit 5:3b95bbfe2dc9 1066 #endif
nerit 0:1e09cd7d66b4 1067 seedCorrection.attach(&seedCorrect,0.005f); // con 16 becchi a 4,5Kmh ci sono 37mS tra un becco e l'altro, quindi 8 correzioni di tb
nerit 7:c9fd242538d9 1068 dcSetting();
nerit 7:c9fd242538d9 1069 #if defined(seedSensor)
nerit 7:c9fd242538d9 1070 seedCheck.rise(&seedSensorTask);
nerit 7:c9fd242538d9 1071 #endif
nerit 0:1e09cd7d66b4 1072 }else{
nerit 0:1e09cd7d66b4 1073 tftUpdate.attach(&videoUpdate,0.125f);
nerit 0:1e09cd7d66b4 1074 }
nerit 0:1e09cd7d66b4 1075
nerit 0:1e09cd7d66b4 1076 aggiornaParametri();
nerit 0:1e09cd7d66b4 1077
nerit 0:1e09cd7d66b4 1078 SDmotorPWM.period_us(periodoSD); // frequency 1KHz pilotaggio motore DC
nerit 7:c9fd242538d9 1079 SDmotorPWM.write(1.0f); // duty cycle = stop
nerit 7:c9fd242538d9 1080 TBmotorDirecti=1; // tb motor direction set
nerit 0:1e09cd7d66b4 1081
nerit 0:1e09cd7d66b4 1082 #if defined(provaStepper)
nerit 7:c9fd242538d9 1083 TBmotorRst=0;
nerit 7:c9fd242538d9 1084 TBticker.attach_us(&step_TBPulseOut,1000.0f); // clock time are seconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 1085 #endif // end prova stepper
nerit 0:1e09cd7d66b4 1086
nerit 0:1e09cd7d66b4 1087 #if defined(canbusActive)
nerit 0:1e09cd7d66b4 1088 can1.attach(&leggiCAN, CAN::RxIrq);
nerit 0:1e09cd7d66b4 1089 #endif
nerit 0:1e09cd7d66b4 1090
nerit 0:1e09cd7d66b4 1091 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1092 // MAIN LOOP
nerit 0:1e09cd7d66b4 1093 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1094 while (true){
nerit 0:1e09cd7d66b4 1095 // ripetitore segnale di velocità
nerit 0:1e09cd7d66b4 1096 if (tractorSpeedRead==0){speedClock=0;}
nerit 0:1e09cd7d66b4 1097
nerit 0:1e09cd7d66b4 1098 // inversione segnali ingressi
nerit 4:d32258ec411f 1099 #if !defined(simulaBanco)
nerit 4:d32258ec411f 1100 seedWheelZeroPinInput = !seedWheelZeroPinInputRev;
nerit 4:d32258ec411f 1101 #else
nerit 4:d32258ec411f 1102 if ((prePosSD-500) >= SDsectorStep){
nerit 4:d32258ec411f 1103 seedWheelZeroPinInput=1;
nerit 4:d32258ec411f 1104 }
nerit 4:d32258ec411f 1105 if ((prePosSD > 500)&&(prePosSD<580)){
nerit 4:d32258ec411f 1106 seedWheelZeroPinInput=0;
nerit 4:d32258ec411f 1107 }
nerit 4:d32258ec411f 1108 #endif
nerit 0:1e09cd7d66b4 1109 TBzeroPinInput = !TBzeroPinInputRev;
nerit 0:1e09cd7d66b4 1110
nerit 0:1e09cd7d66b4 1111 // se quinconce attivo ed unita' master invia segnale di sincro
nerit 0:1e09cd7d66b4 1112 #if defined(speedMaster)
nerit 0:1e09cd7d66b4 1113 if (seedWheelZeroPinInput==1){
nerit 7:c9fd242538d9 1114 quinconceOut=0;
nerit 0:1e09cd7d66b4 1115 }
nerit 5:3b95bbfe2dc9 1116 if (((double)(prePosSD-500) >= (SDsectorStep/((double)quincSector)))&&(quinconceOut=1)){
nerit 7:c9fd242538d9 1117 quinconceOut=1;
nerit 7:c9fd242538d9 1118 }
nerit 7:c9fd242538d9 1119 if (quinconceActive==1){
nerit 7:c9fd242538d9 1120 if ((quinconceOut==1)&&(oldQuinconceOut==1)){
nerit 5:3b95bbfe2dc9 1121 posForQuinc=500;
nerit 7:c9fd242538d9 1122 oldQuinconceOut=0;
nerit 7:c9fd242538d9 1123 }
nerit 7:c9fd242538d9 1124 if (((double)posForQuinc-500.0f)> (SDsectorStep-200)){
nerit 7:c9fd242538d9 1125 oldQuinconceOut=1;
nerit 7:c9fd242538d9 1126 }
nerit 7:c9fd242538d9 1127 }
nerit 0:1e09cd7d66b4 1128 #endif
nerit 0:1e09cd7d66b4 1129
nerit 0:1e09cd7d66b4 1130 #if defined(overWriteCanSimulation)
nerit 0:1e09cd7d66b4 1131 leggiCAN();
nerit 0:1e09cd7d66b4 1132 #endif
nerit 0:1e09cd7d66b4 1133
nerit 0:1e09cd7d66b4 1134 // simulazione velocita
nerit 0:1e09cd7d66b4 1135 if (enableSimula==1){
nerit 0:1e09cd7d66b4 1136 double TMT = 0.0f;
nerit 0:1e09cd7d66b4 1137 if (speedSimula > 0){
nerit 0:1e09cd7d66b4 1138 TMT = (double)(speedSimula) * 100.0f /3600.0f;
nerit 0:1e09cd7d66b4 1139 pulseSpeedInterval = pulseDistance / TMT;
nerit 0:1e09cd7d66b4 1140 }else{
nerit 0:1e09cd7d66b4 1141 pulseSpeedInterval = 10000.0f;
nerit 0:1e09cd7d66b4 1142 }
nerit 0:1e09cd7d66b4 1143 if (avviaSimula==1){
nerit 0:1e09cd7d66b4 1144 if(oldSimulaSpeed!=pulseSpeedInterval){
nerit 7:c9fd242538d9 1145 #if defined(pcSerial)
nerit 7:c9fd242538d9 1146 #if defined(canDataReceived)
nerit 7:c9fd242538d9 1147 pc.printf("Pulseinterval %f \n",pulseSpeedInterval);
nerit 7:c9fd242538d9 1148 #endif
nerit 7:c9fd242538d9 1149 #endif
nerit 0:1e09cd7d66b4 1150 spedSimclock.attach_us(&speedSimulationClock,pulseSpeedInterval);
nerit 0:1e09cd7d66b4 1151 oldSimulaSpeed=pulseSpeedInterval;
nerit 0:1e09cd7d66b4 1152 }
nerit 0:1e09cd7d66b4 1153 }else{
nerit 0:1e09cd7d66b4 1154 oldSimulaSpeed=10000.0f;
nerit 0:1e09cd7d66b4 1155 spedSimclock.detach();
nerit 0:1e09cd7d66b4 1156 }
nerit 0:1e09cd7d66b4 1157 }else{
nerit 0:1e09cd7d66b4 1158 spedSimclock.detach();
nerit 0:1e09cd7d66b4 1159 }
nerit 0:1e09cd7d66b4 1160
nerit 0:1e09cd7d66b4 1161 //*******************************************************
nerit 0:1e09cd7d66b4 1162 // determina se sono in bassa velocità per il controllo di TB
nerit 0:1e09cd7d66b4 1163 if (speedOfSeedWheel<=minSeedSpeed){
nerit 0:1e09cd7d66b4 1164 if (lowSpeedRequired==0){
nerit 0:1e09cd7d66b4 1165 ritardaLowSpeed.reset();
nerit 0:1e09cd7d66b4 1166 ritardaLowSpeed.start();
nerit 0:1e09cd7d66b4 1167 }
nerit 0:1e09cd7d66b4 1168 lowSpeedRequired=1;
nerit 0:1e09cd7d66b4 1169 }else{
nerit 0:1e09cd7d66b4 1170 if (lowSpeedRequired==1){
nerit 0:1e09cd7d66b4 1171 lowSpeedRequired=0;
nerit 0:1e09cd7d66b4 1172 ritardaLowSpeed.reset();
nerit 0:1e09cd7d66b4 1173 ritardaLowSpeed.stop();
nerit 0:1e09cd7d66b4 1174 }
nerit 0:1e09cd7d66b4 1175 }
nerit 0:1e09cd7d66b4 1176
nerit 0:1e09cd7d66b4 1177 if (ritardaLowSpeed.read_ms()> 2000){
nerit 0:1e09cd7d66b4 1178 lowSpeed=1;
nerit 0:1e09cd7d66b4 1179 }else{
nerit 0:1e09cd7d66b4 1180 lowSpeed=0;
nerit 0:1e09cd7d66b4 1181 }
nerit 0:1e09cd7d66b4 1182
nerit 0:1e09cd7d66b4 1183
nerit 0:1e09cd7d66b4 1184 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1185 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1186 // LOGICAL CONTROLS
nerit 0:1e09cd7d66b4 1187 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1188 //**************************************************************************************************************
nerit 0:1e09cd7d66b4 1189
nerit 0:1e09cd7d66b4 1190 if (inProva==0){
nerit 7:c9fd242538d9 1191 if ((startCycleSimulation==0)&&(enableSimula==0)){
nerit 7:c9fd242538d9 1192 zeroRequestBuf=1;
nerit 7:c9fd242538d9 1193 runRequestBuf=0;
nerit 7:c9fd242538d9 1194 enableCycle=1;
nerit 7:c9fd242538d9 1195 }else{
nerit 7:c9fd242538d9 1196 zeroRequestBuf=1;
nerit 7:c9fd242538d9 1197 runRequestBuf=0;
nerit 7:c9fd242538d9 1198 enableCycle=1;
nerit 7:c9fd242538d9 1199 }
nerit 7:c9fd242538d9 1200 if ((tractorSpeedRead==0)&&(speedFilter.read_ms()>3)){
nerit 7:c9fd242538d9 1201 oldTractorSpeedRead=0;
nerit 7:c9fd242538d9 1202 }
nerit 0:1e09cd7d66b4 1203 // ----------------------------------------
nerit 0:1e09cd7d66b4 1204 // filtra il segnale dei becchi per misurare il tempo di intervallo tra loro
nerit 0:1e09cd7d66b4 1205 // ----------------------------------------
nerit 4:d32258ec411f 1206 if ((seedWheelZeroPinInput==0)&&(oldSeedWheelZeroPinInput==1)){
nerit 0:1e09cd7d66b4 1207 if(seedFilter.read_ms()>=4){
nerit 0:1e09cd7d66b4 1208 oldSeedWheelZeroPinInput=0;
nerit 0:1e09cd7d66b4 1209 SDzeroDebounced=0;
nerit 0:1e09cd7d66b4 1210 }
nerit 0:1e09cd7d66b4 1211 }
nerit 0:1e09cd7d66b4 1212 if ((seedWheelZeroPinInput==1)&&(oldSeedWheelZeroPinInput==0)){
nerit 5:3b95bbfe2dc9 1213 timeIntraPick = (double)intraPickTimer.read_ms();
nerit 0:1e09cd7d66b4 1214 prePosSD=500; // preposizionamento SD
nerit 0:1e09cd7d66b4 1215 intraPickTimer.reset();
nerit 0:1e09cd7d66b4 1216 rotationTimeOut.reset();
nerit 0:1e09cd7d66b4 1217 seedFilter.reset();
nerit 0:1e09cd7d66b4 1218 sincroTimer.reset();
nerit 0:1e09cd7d66b4 1219 oldSeedWheelZeroPinInput=1;
nerit 1:e88bf5011af6 1220 quincTimeSD.reset();
nerit 0:1e09cd7d66b4 1221 SDzeroDebounced=1;
nerit 4:d32258ec411f 1222 sincroQui=1;
nerit 0:1e09cd7d66b4 1223 SDwheelTimer.reset();
nerit 5:3b95bbfe2dc9 1224 #if defined(speedMaster)
nerit 5:3b95bbfe2dc9 1225 if (quinconceActive==0){
nerit 5:3b95bbfe2dc9 1226 posForQuinc=500;
nerit 5:3b95bbfe2dc9 1227 }
nerit 5:3b95bbfe2dc9 1228 #endif
nerit 0:1e09cd7d66b4 1229 if (quincCnt<10){
nerit 0:1e09cd7d66b4 1230 quincCnt++;
nerit 0:1e09cd7d66b4 1231 }
nerit 0:1e09cd7d66b4 1232 if ((aspettaStart==0)&&(lowSpeed==1)){
nerit 0:1e09cd7d66b4 1233 beccoPronto=1;
nerit 0:1e09cd7d66b4 1234 }
nerit 0:1e09cd7d66b4 1235 SDzeroCyclePulse=1;
nerit 0:1e09cd7d66b4 1236 lockStart=0;
nerit 0:1e09cd7d66b4 1237 double fase1=0.0f;
nerit 0:1e09cd7d66b4 1238 forzaFase=0;
nerit 0:1e09cd7d66b4 1239 double limite=fixedStepGiroSD/pickNumber;
nerit 0:1e09cd7d66b4 1240 if (tamburoStandard==0){
nerit 0:1e09cd7d66b4 1241 fase1=TBdeltaStep;
nerit 0:1e09cd7d66b4 1242 }else{
nerit 0:1e09cd7d66b4 1243 if(speedForCorrection >= speedOfSeedWheel){
nerit 0:1e09cd7d66b4 1244 fase1=TBdeltaStep;
nerit 0:1e09cd7d66b4 1245 }else{
nerit 6:3fca0ca1949e 1246 //fase1=(TBdeltaStep)-(((speedOfSeedWheel-speedForCorrection)/maxWorkSpeed)*(TBfaseStep));
nerit 6:3fca0ca1949e 1247 fase1=(TBdeltaStep)-(((speedOfSeedWheel)/maxWorkSpeed)*(TBfaseStep));
nerit 0:1e09cd7d66b4 1248 }
nerit 0:1e09cd7d66b4 1249 if (fase1 > limite){
nerit 0:1e09cd7d66b4 1250 fase1 -= limite; // se la fase calcolata supera gli step del settore riporta il valore nell'arco precedente (es. fase 1 800, limite 750, risulta 50)
nerit 0:1e09cd7d66b4 1251 //forzaFase=1;
nerit 0:1e09cd7d66b4 1252 }
nerit 0:1e09cd7d66b4 1253 if ((fase1 > (limite -20.0f))&&(fase1<(limite +5.0f))){
nerit 0:1e09cd7d66b4 1254 fase1 = limite -20.0f; // se la fase è molto vicina al limite interpone uno spazio minimo di lavoro del sincronismo
nerit 0:1e09cd7d66b4 1255 forzaFase=1;
nerit 0:1e09cd7d66b4 1256 }
nerit 0:1e09cd7d66b4 1257 trigRepos=1;
nerit 0:1e09cd7d66b4 1258 }
nerit 0:1e09cd7d66b4 1259 fase = (uint32_t)fase1+500;
nerit 0:1e09cd7d66b4 1260 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 1261 #if defined(inCorre)
nerit 0:1e09cd7d66b4 1262 pc.printf(" limite %f", limite);
nerit 0:1e09cd7d66b4 1263 pc.printf(" delta %f", TBdeltaStep);
nerit 0:1e09cd7d66b4 1264 pc.printf(" faseStep %f", TBfaseStep);
nerit 0:1e09cd7d66b4 1265 pc.printf(" fase %d",fase);
nerit 0:1e09cd7d66b4 1266 pc.printf(" forzaFase %d",forzaFase);
nerit 0:1e09cd7d66b4 1267 pc.printf(" trigRepos %d", trigRepos);
nerit 0:1e09cd7d66b4 1268 pc.printf(" ActualSD: %d",SDactualPosition);
nerit 0:1e09cd7d66b4 1269 pc.printf(" SpeedWheel: %f",speedOfSeedWheel);
nerit 0:1e09cd7d66b4 1270 pc.printf(" SPEED: %f \n",tractorSpeed_MtS_timed);
nerit 0:1e09cd7d66b4 1271 #endif
nerit 0:1e09cd7d66b4 1272 #endif
nerit 0:1e09cd7d66b4 1273 if (timeIntraPick >= (memoIntraPick*2)){
nerit 0:1e09cd7d66b4 1274 if ((aspettaStart==0)&&(zeroRequestBuf==0)){
nerit 0:1e09cd7d66b4 1275 if (firstStart==0){
nerit 0:1e09cd7d66b4 1276 all_pickSignal=1;
nerit 0:1e09cd7d66b4 1277 }
nerit 0:1e09cd7d66b4 1278 }
nerit 0:1e09cd7d66b4 1279 }
nerit 0:1e09cd7d66b4 1280 memoIntraPick = timeIntraPick;
nerit 5:3b95bbfe2dc9 1281 if ((speedFromPick==1)&&(encoder==false)){
nerit 0:1e09cd7d66b4 1282 speedOfSeedWheel=((seedPerimeter / pickNumber)/timeIntraPick)*1000.0f;
nerit 0:1e09cd7d66b4 1283 #if defined(pcSerial)
nerit 4:d32258ec411f 1284 #if defined(Qnca)
nerit 4:d32258ec411f 1285 pc.printf("perim: %f pickN: %f sped: %f\n", seedPerimeter, pickNumber,speedOfSeedWheel);
nerit 0:1e09cd7d66b4 1286 #endif
nerit 0:1e09cd7d66b4 1287 #endif
nerit 0:1e09cd7d66b4 1288 }
nerit 7:c9fd242538d9 1289 if (encoder==false){
nerit 7:c9fd242538d9 1290 pulseRised2=1;
nerit 7:c9fd242538d9 1291 }
nerit 4:d32258ec411f 1292 #if defined(speedMaster)
nerit 4:d32258ec411f 1293 if ((tractorSpeed_MtS_timed==0.0f)&&(zeroCycle==0)&&(zeroCycleEnd==1)){
nerit 4:d32258ec411f 1294 if ((firstStart==0)&&(simOk==0)&&(enableSimula==0)&&(zeroCycleEnd==1)){
nerit 4:d32258ec411f 1295 all_noSpeedSen=1;
nerit 4:d32258ec411f 1296 }
nerit 4:d32258ec411f 1297 }
nerit 5:3b95bbfe2dc9 1298 double oldLastPr = (double)oldLastPulseRead*1.5f;
nerit 0:1e09cd7d66b4 1299 if((double)speedTimeOut.read_us()> (oldLastPr)){
nerit 0:1e09cd7d66b4 1300 if ((firstStart==0)&&(simOk==0)&&(enableSimula==0)&&(zeroCycleEnd==1)){
nerit 0:1e09cd7d66b4 1301 all_speedError =1;
nerit 0:1e09cd7d66b4 1302 }
nerit 0:1e09cd7d66b4 1303 }
nerit 0:1e09cd7d66b4 1304 #endif
nerit 0:1e09cd7d66b4 1305 //*******************************************
nerit 0:1e09cd7d66b4 1306 // esegue calcolo clock per la generazione della posizione teorica
nerit 0:1e09cd7d66b4 1307 // la realtà in base al segnale di presenza del becco
nerit 0:1e09cd7d66b4 1308 realSpeed = speedOfSeedWheel;
nerit 0:1e09cd7d66b4 1309 realGiroSD = seedPerimeter / realSpeed;
nerit 0:1e09cd7d66b4 1310 tempoBecco = (realGiroSD/360.0f)*16000.0f;
nerit 0:1e09cd7d66b4 1311 frequenzaReale = fixedStepGiroSD/realGiroSD;
nerit 0:1e09cd7d66b4 1312 semiPeriodoReale = (1000000.0f/frequenzaReale);
nerit 0:1e09cd7d66b4 1313 tempoTraBecchi_mS = 0.0f;
nerit 0:1e09cd7d66b4 1314 seedWheelRPM = (speedOfSeedWheel)*K_WheelRPM ; // calcola i giri al minuto della ruota di semina 7.37 31,75
nerit 0:1e09cd7d66b4 1315 TBrpm = seedWheelRPM*rapportoRuote; // 5.896 31,75
nerit 0:1e09cd7d66b4 1316 TBfrequency = (TBrpm*K_TBfrequency); // 130Hz a 0,29Mts 1397,00 a 1,25mt/s con 15 becchi e 15 celle
nerit 0:1e09cd7d66b4 1317 TBperiod=1000000.0f/TBfrequency; // 715uS
nerit 0:1e09cd7d66b4 1318 }
nerit 0:1e09cd7d66b4 1319 // ----------------------------------------
nerit 0:1e09cd7d66b4 1320 // check SD fase
nerit 0:1e09cd7d66b4 1321 if ((prePosSD >= fase)||(forzaFase==1)){//&&(prePosSD < (fase +30))){
nerit 0:1e09cd7d66b4 1322 forzaFase=0;
nerit 0:1e09cd7d66b4 1323 if (trigRepos==1){
nerit 0:1e09cd7d66b4 1324 SDactualPosition=0;
nerit 0:1e09cd7d66b4 1325 if ((countCicli<30)&&(trigCicli==0)){countCicli++;trigCicli=1;}
nerit 0:1e09cd7d66b4 1326 if(countCicli>=cicliAspettaStart){aspettaStart=0;}
nerit 0:1e09cd7d66b4 1327 if ((lowSpeed==0)&&(aspettaStart==0)&&(lockStart==0)){syncroCheck=1;beccoPronto=0;}
nerit 0:1e09cd7d66b4 1328 if (trigTB==0){
nerit 0:1e09cd7d66b4 1329 inhibit=1;
nerit 0:1e09cd7d66b4 1330 trigSD=1;
nerit 0:1e09cd7d66b4 1331 }else{
nerit 0:1e09cd7d66b4 1332 inhibit=0;
nerit 0:1e09cd7d66b4 1333 trigTB=0;
nerit 0:1e09cd7d66b4 1334 trigSD=0;
nerit 0:1e09cd7d66b4 1335 }
nerit 0:1e09cd7d66b4 1336 trigRepos=0;
nerit 0:1e09cd7d66b4 1337 }
nerit 0:1e09cd7d66b4 1338 }else{
nerit 0:1e09cd7d66b4 1339 trigCicli=0;
nerit 0:1e09cd7d66b4 1340 }
nerit 0:1e09cd7d66b4 1341 // ----------------------------------------
nerit 0:1e09cd7d66b4 1342 // filtra il segnale del tamburo per lo stop in fase del tamburo stesso
nerit 0:1e09cd7d66b4 1343 if (TBzeroPinInput==0){if (TBfilter.read_ms()>=5){oldTBzeroPinInput=0;}}
nerit 0:1e09cd7d66b4 1344 if ((TBzeroPinInput==1)&&(oldTBzeroPinInput==0)){
nerit 0:1e09cd7d66b4 1345 oldTBzeroPinInput=1;
nerit 0:1e09cd7d66b4 1346 if (loadDaCanInCorso==0){
nerit 0:1e09cd7d66b4 1347 stopCicloTB=1;
nerit 0:1e09cd7d66b4 1348 startCicloTB=0;
nerit 0:1e09cd7d66b4 1349 }
nerit 0:1e09cd7d66b4 1350 TBfilter.reset();
nerit 0:1e09cd7d66b4 1351 TBzeroCyclePulse=1;
nerit 0:1e09cd7d66b4 1352 TBactualPosition=0;
nerit 0:1e09cd7d66b4 1353 if (cellsCounterLow < 0xFF){
nerit 0:1e09cd7d66b4 1354 cellsCounterLow++;
nerit 0:1e09cd7d66b4 1355 }else{
nerit 0:1e09cd7d66b4 1356 cellsCounterHig++;
nerit 0:1e09cd7d66b4 1357 cellsCounterLow=0;
nerit 0:1e09cd7d66b4 1358 }
nerit 0:1e09cd7d66b4 1359 if (loadDaCanInCorso==1){
nerit 0:1e09cd7d66b4 1360 cntCellsForLoad++;
nerit 0:1e09cd7d66b4 1361 if (cntCellsForLoad >= 5){
nerit 0:1e09cd7d66b4 1362 stopCicloTB=1;
nerit 0:1e09cd7d66b4 1363 cntCellsForLoad=0;
nerit 0:1e09cd7d66b4 1364 }
nerit 0:1e09cd7d66b4 1365 }else{
nerit 0:1e09cd7d66b4 1366 cntCellsForLoad=0;
nerit 0:1e09cd7d66b4 1367 }
nerit 0:1e09cd7d66b4 1368 if (trigSD==0){
nerit 0:1e09cd7d66b4 1369 inhibit=1;
nerit 0:1e09cd7d66b4 1370 trigTB=1;
nerit 0:1e09cd7d66b4 1371 }else{
nerit 0:1e09cd7d66b4 1372 inhibit=0;
nerit 0:1e09cd7d66b4 1373 trigTB=0;
nerit 0:1e09cd7d66b4 1374 trigSD=0;
nerit 0:1e09cd7d66b4 1375 }
nerit 0:1e09cd7d66b4 1376 // torna indietro per sbloccare il tamburo
nerit 7:c9fd242538d9 1377 if ((TBmotorDirecti==0)&&(erroreTamburo==1)){
nerit 0:1e09cd7d66b4 1378 cntCellsForReload++;
nerit 6:3fca0ca1949e 1379 if (cntCellsForReload >= cellsCountSet){
nerit 7:c9fd242538d9 1380 TBmotorDirecti=1;
nerit 0:1e09cd7d66b4 1381 erroreTamburo=0;
nerit 0:1e09cd7d66b4 1382 }
nerit 0:1e09cd7d66b4 1383 }
nerit 7:c9fd242538d9 1384 #if defined(seedSensor)
nerit 7:c9fd242538d9 1385 resetDelay();
nerit 7:c9fd242538d9 1386 delaySeedCheck.start();
nerit 7:c9fd242538d9 1387 #endif
nerit 0:1e09cd7d66b4 1388 }
nerit 0:1e09cd7d66b4 1389 if ((double)TBactualPosition > ((TBgiroStep/cellsNumber)*1.2f)){
nerit 0:1e09cd7d66b4 1390 if (firstStart==0){
nerit 5:3b95bbfe2dc9 1391 if (cntTbError>2){
nerit 5:3b95bbfe2dc9 1392 all_cellSignal=1;
nerit 7:c9fd242538d9 1393 #if defined(seedSensor)
nerit 7:c9fd242538d9 1394 resetDelay();
nerit 7:c9fd242538d9 1395 #endif
nerit 5:3b95bbfe2dc9 1396 }
nerit 0:1e09cd7d66b4 1397 }
nerit 0:1e09cd7d66b4 1398 if (erroreTamburo==0){
nerit 0:1e09cd7d66b4 1399 erroreTamburo=1;
nerit 7:c9fd242538d9 1400 TBmotorDirecti=0;
nerit 0:1e09cd7d66b4 1401 cntCellsForReload=0;
nerit 0:1e09cd7d66b4 1402 cntTbError++;
nerit 7:c9fd242538d9 1403 #if defined(seedSensor)
nerit 7:c9fd242538d9 1404 resetDelay();
nerit 7:c9fd242538d9 1405 #endif
nerit 0:1e09cd7d66b4 1406 }
nerit 0:1e09cd7d66b4 1407 }
nerit 5:3b95bbfe2dc9 1408 if (((double)TBactualPosition > ((TBgiroStep/cellsNumber)*3.0f))||(cntTbError>4)){
nerit 0:1e09cd7d66b4 1409 if (firstStart==0){
nerit 0:1e09cd7d66b4 1410 all_noStepRota=1;
nerit 7:c9fd242538d9 1411 #if defined(seedSensor)
nerit 7:c9fd242538d9 1412 resetDelay();
nerit 7:c9fd242538d9 1413 #endif
nerit 0:1e09cd7d66b4 1414 }
nerit 0:1e09cd7d66b4 1415 cntTbError=0;
nerit 0:1e09cd7d66b4 1416 }
nerit 0:1e09cd7d66b4 1417 // ----------------------------------------
nerit 0:1e09cd7d66b4 1418 // read and manage joystick
nerit 0:1e09cd7d66b4 1419 // WARNING - ENABLE CYCLE IS SOFTWARE ALWAYS ACTIVE
nerit 0:1e09cd7d66b4 1420 if (enableCycle==1){
nerit 0:1e09cd7d66b4 1421 if(runRequestBuf==1){
nerit 0:1e09cd7d66b4 1422 if (OldStartCycle!=runRequestBuf){
nerit 0:1e09cd7d66b4 1423 if((startCycle==0)&&(zeroCycleEnd==1)){
nerit 0:1e09cd7d66b4 1424 startCycle=1;
nerit 0:1e09cd7d66b4 1425 OldStartCycle = runRequestBuf;
nerit 0:1e09cd7d66b4 1426 oldZeroCycle=0;
nerit 0:1e09cd7d66b4 1427 }
nerit 0:1e09cd7d66b4 1428 }
nerit 0:1e09cd7d66b4 1429 }else{
nerit 0:1e09cd7d66b4 1430 startCycle=0;
nerit 0:1e09cd7d66b4 1431 pntMedia=0;
nerit 0:1e09cd7d66b4 1432 }
nerit 0:1e09cd7d66b4 1433 if (azzeraDaCan==1){
nerit 0:1e09cd7d66b4 1434 if (tractorSpeed_MtS_timed==0.0f){
nerit 0:1e09cd7d66b4 1435 zeroRequestBuf=1;
nerit 0:1e09cd7d66b4 1436 oldZeroCycle=0;
nerit 0:1e09cd7d66b4 1437 }
nerit 0:1e09cd7d66b4 1438 azzeraDaCan=0;
nerit 0:1e09cd7d66b4 1439 }
nerit 0:1e09cd7d66b4 1440 if (loadDaCan==1){
nerit 0:1e09cd7d66b4 1441 if (tractorSpeed_MtS_timed==0.0f){
nerit 0:1e09cd7d66b4 1442 ciclaTB();
nerit 0:1e09cd7d66b4 1443 }
nerit 0:1e09cd7d66b4 1444 }
nerit 0:1e09cd7d66b4 1445 if ((zeroRequestBuf==1)){
nerit 0:1e09cd7d66b4 1446 if (oldZeroCycle!=zeroRequestBuf){
nerit 0:1e09cd7d66b4 1447 zeroCycle=1;
nerit 0:1e09cd7d66b4 1448 zeroCycleEnd=0;
nerit 0:1e09cd7d66b4 1449 SDzeroed=0;
nerit 0:1e09cd7d66b4 1450 TBzeroed=0;
nerit 0:1e09cd7d66b4 1451 zeroTrigger=0;
nerit 0:1e09cd7d66b4 1452 oldZeroCycle = zeroRequestBuf;
nerit 0:1e09cd7d66b4 1453 }
nerit 0:1e09cd7d66b4 1454 }
nerit 0:1e09cd7d66b4 1455 }else{
nerit 0:1e09cd7d66b4 1456 startCycle=0;
nerit 0:1e09cd7d66b4 1457 zeroCycle=0;
nerit 0:1e09cd7d66b4 1458 }
nerit 0:1e09cd7d66b4 1459
nerit 0:1e09cd7d66b4 1460 //***************************************************************************************************
nerit 0:1e09cd7d66b4 1461 // pulseRised define the event of speed wheel pulse occurs
nerit 0:1e09cd7d66b4 1462 //
nerit 0:1e09cd7d66b4 1463 double maxInterval = pulseDistance/minWorkSpeed;
nerit 0:1e09cd7d66b4 1464 double minIntervalPulse = pulseDistance/maxWorkSpeed;
nerit 0:1e09cd7d66b4 1465 if (pulseRised==1){
nerit 0:1e09cd7d66b4 1466 if (enableSpeed<10){enableSpeed++;}
nerit 5:3b95bbfe2dc9 1467 pulseRised=0;
nerit 5:3b95bbfe2dc9 1468 pulseRised1=1;
nerit 5:3b95bbfe2dc9 1469 speedMediaCalc();
nerit 0:1e09cd7d66b4 1470 // calcola velocità trattore
nerit 0:1e09cd7d66b4 1471 if(enableSpeed>=2){
nerit 0:1e09cd7d66b4 1472 if ((pulseSpeedInterval>=0.0f)){ //minIntervalPulse)&&(pulseSpeedInterval<maxInterval)){
nerit 7:c9fd242538d9 1473 if((quincCnt<5)||(speedFromMaster==0.0f)){
nerit 7:c9fd242538d9 1474 tractorSpeed_MtS_timed = ((pulseDistance / pulseSpeedInterval)); // tractor speed (unit= Mt/S) from pulse time interval
nerit 7:c9fd242538d9 1475 }
nerit 7:c9fd242538d9 1476 /*#if !defined(speedMaster)
nerit 5:3b95bbfe2dc9 1477 if (quincCnt>=5){
nerit 5:3b95bbfe2dc9 1478 if (speedFromMaster>0.0f){
nerit 5:3b95bbfe2dc9 1479 tractorSpeed_MtS_timed = speedFromMaster + percento;
nerit 5:3b95bbfe2dc9 1480 }
nerit 5:3b95bbfe2dc9 1481 }
nerit 7:c9fd242538d9 1482 #endif*/
nerit 0:1e09cd7d66b4 1483 if (checkSDrotation==0){
nerit 0:1e09cd7d66b4 1484 checkSDrotation=1;
nerit 0:1e09cd7d66b4 1485 SDwheelTimer.start();
nerit 0:1e09cd7d66b4 1486 }
nerit 0:1e09cd7d66b4 1487 }
nerit 0:1e09cd7d66b4 1488 }
nerit 0:1e09cd7d66b4 1489 speedTimeOut.reset();
nerit 0:1e09cd7d66b4 1490 }else{
nerit 0:1e09cd7d66b4 1491 double oldLastPr = (double)oldLastPulseRead*1.7f;
nerit 0:1e09cd7d66b4 1492 if((double)speedTimeOut.read_us()> (oldLastPr)){
nerit 0:1e09cd7d66b4 1493 tractorSpeed_MtS_timed = 0.0f;
nerit 7:c9fd242538d9 1494 #if defined(seedSensor)
nerit 7:c9fd242538d9 1495 resetDelay();
nerit 7:c9fd242538d9 1496 #endif
nerit 0:1e09cd7d66b4 1497 pntMedia=0;
nerit 0:1e09cd7d66b4 1498 speedTimeOut.reset();
nerit 0:1e09cd7d66b4 1499 enableSpeed=0;
nerit 0:1e09cd7d66b4 1500 quincCnt=0;
nerit 0:1e09cd7d66b4 1501 }
nerit 0:1e09cd7d66b4 1502 }
nerit 7:c9fd242538d9 1503
nerit 7:c9fd242538d9 1504 #if defined(seedSensor)
nerit 7:c9fd242538d9 1505 if (seedSensorEnable==true){
nerit 7:c9fd242538d9 1506 if (delaySeedCheck.read_ms()>100){
nerit 7:c9fd242538d9 1507 if (seedSee==0){
nerit 7:c9fd242538d9 1508 all_noSeedOnCe=1;
nerit 7:c9fd242538d9 1509 }
nerit 7:c9fd242538d9 1510 resetDelay();
nerit 7:c9fd242538d9 1511 }
nerit 7:c9fd242538d9 1512 }
nerit 7:c9fd242538d9 1513 #endif
nerit 0:1e09cd7d66b4 1514 // esegue il controllo di velocità minima
nerit 7:c9fd242538d9 1515 /*if ((double)speedTimer.read_ms()>=maxInterval){
nerit 4:d32258ec411f 1516 tractorSpeed_MtS_timed = 0.0f;
nerit 0:1e09cd7d66b4 1517 enableSpeed=0;
nerit 7:c9fd242538d9 1518 }*/
nerit 6:3fca0ca1949e 1519 // esegue il controllo di velocità massima
nerit 7:c9fd242538d9 1520 /*if ((double)speedTimer.read_ms()<=minIntervalPulse){
nerit 6:3fca0ca1949e 1521 tractorSpeed_MtS_timed = 4.5f;
nerit 7:c9fd242538d9 1522 }*/
nerit 0:1e09cd7d66b4 1523 //***************************************************************************************************************
nerit 0:1e09cd7d66b4 1524 // cycle logic control section
nerit 0:1e09cd7d66b4 1525 //***************************************************************************************************************
nerit 0:1e09cd7d66b4 1526 if (enableSimula==1){if(simOk==0){tractorSpeed_MtS_timed=0.0f;}}
nerit 0:1e09cd7d66b4 1527 if ((tractorSpeed_MtS_timed>0.01f)){
nerit 0:1e09cd7d66b4 1528 cycleStopRequest=1;
nerit 0:1e09cd7d66b4 1529 // calcola il tempo teorico di passaggio becchi sulla base della velocità del trattore
nerit 0:1e09cd7d66b4 1530 tempoGiroSD = seedPerimeter / tractorSpeed_MtS_timed; // tempo Teorico impiegato dalla ruota di semina per fare un giro completo (a 4,5Km/h impiega 1,89 secondi)
nerit 5:3b95bbfe2dc9 1531 if (encoder==false){
nerit 5:3b95bbfe2dc9 1532 if (speedFromPick==1) {
nerit 5:3b95bbfe2dc9 1533 tempoTraBecchi_mS = (tempoGiroSD / pickNumber)*1000.0f; // tempo tra due impulsi dai becchi in millisecondi ( circa 157mS a 4,5Km/h)
nerit 5:3b95bbfe2dc9 1534 }else{
nerit 5:3b95bbfe2dc9 1535 tempoTraBecchi_mS = (tempoGiroSD / 25.0f)*1000.0f; // tempo tra due impulsi dai becchi in millisecondi ( circa 157mS a 4,5Km/h)
nerit 5:3b95bbfe2dc9 1536 }
nerit 0:1e09cd7d66b4 1537 }else{
nerit 5:3b95bbfe2dc9 1538 tempoTraBecchi_mS = (tempoGiroSD / (SDreductionRatio*25.5f))*1000.0f; // tempo tra due impulsi dai becchi in millisecondi ( circa 157mS a 4,5Km/h)
nerit 5:3b95bbfe2dc9 1539 double pippo=0.0f;
nerit 5:3b95bbfe2dc9 1540 #if !defined(speedMaster)
nerit 5:3b95bbfe2dc9 1541 pippo = seedPerimeter / speedFromMaster;
nerit 5:3b95bbfe2dc9 1542 #endif
nerit 5:3b95bbfe2dc9 1543 tempoBecchiPerQuinc = (pippo / pickNumber)*1000.0f;
nerit 0:1e09cd7d66b4 1544 }
nerit 0:1e09cd7d66b4 1545 //*******************************************
nerit 0:1e09cd7d66b4 1546 // segue calcolo duty cycle comando motore DC per allinearsi con la velocità del trattore
nerit 0:1e09cd7d66b4 1547 double dutyTeorico = 0.00;
nerit 0:1e09cd7d66b4 1548 if ((tractorSpeed_MtS_timed>0.0)&&(tractorSpeed_MtS_timed<tabSpeed[0])){dutyTeorico = tabComan[0];}
nerit 0:1e09cd7d66b4 1549 for (int ii = 0;ii<16;ii++){
nerit 0:1e09cd7d66b4 1550 if ((tractorSpeed_MtS_timed>=tabSpeed[ii])&&(tractorSpeed_MtS_timed<tabSpeed[ii+1])){
nerit 0:1e09cd7d66b4 1551 dutyTeorico = tabComan[ii+1];
nerit 0:1e09cd7d66b4 1552 }
nerit 0:1e09cd7d66b4 1553 }
nerit 6:3fca0ca1949e 1554 if (tractorSpeed_MtS_timed > tabSpeed[16]){dutyTeorico=tractorSpeed_MtS_timed/maxWorkSpeed;}
nerit 2:d9c7430ae953 1555 #if !defined(speedMaster)
nerit 4:d32258ec411f 1556 quinCalc();
nerit 2:d9c7430ae953 1557 #endif
nerit 5:3b95bbfe2dc9 1558 if ((enableSpeed>3)&&(pulseRised2==1)&&(quincCnt>=2)){
nerit 0:1e09cd7d66b4 1559 double erroreTempo = 0.0f;
nerit 5:3b95bbfe2dc9 1560 if(encoder==false){
nerit 5:3b95bbfe2dc9 1561 if(speedFromPick==1){
nerit 3:c0f11ca4df02 1562 erroreTempo = (double)timeIntraPick - tempoTraBecchi_mS;
nerit 5:3b95bbfe2dc9 1563 }else{
nerit 5:3b95bbfe2dc9 1564 erroreTempo = (double)memoTimeHole - tempoTraBecchi_mS; // errore tra il tempo previsto ed il tempo reale ( >0 se sto andando più piano del previsto)
nerit 5:3b95bbfe2dc9 1565 }
nerit 0:1e09cd7d66b4 1566 }else{
nerit 5:3b95bbfe2dc9 1567 erroreTempo = ((double)memoTimeHole/1000.0f) - tempoTraBecchi_mS; // errore tra il tempo previsto ed il tempo reale ( >0 se sto andando più piano del previsto)
nerit 0:1e09cd7d66b4 1568 }
nerit 0:1e09cd7d66b4 1569 double errorePercentuale = erroreTempo / tempoTraBecchi_mS;
nerit 1:e88bf5011af6 1570 double k3=0.0f;
nerit 1:e88bf5011af6 1571 double k4=0.0f;
nerit 1:e88bf5011af6 1572 double k5=0.0f;
nerit 4:d32258ec411f 1573 double k6=0.0f;
nerit 4:d32258ec411f 1574 /*if (tractorSpeed_MtS_timed <= minSeedSpeed){
nerit 4:d32258ec411f 1575 k3=1.030f;
nerit 4:d32258ec411f 1576 k4=5.103f;
nerit 4:d32258ec411f 1577 k5=10.00f;
nerit 4:d32258ec411f 1578 k6=20.50f;
nerit 4:d32258ec411f 1579 }else{*/
nerit 4:d32258ec411f 1580 k3=0.050f;
nerit 7:c9fd242538d9 1581 k4=1.103f;
nerit 7:c9fd242538d9 1582 k5=10.00f;
nerit 7:c9fd242538d9 1583 k6=20.50f;
nerit 4:d32258ec411f 1584 //}
nerit 7:c9fd242538d9 1585 double L1 = 0.045f;
nerit 7:c9fd242538d9 1586 double L_1=-0.045f;
nerit 7:c9fd242538d9 1587 double L2 = 0.150f;
nerit 7:c9fd242538d9 1588 double L_2=-0.150f;
nerit 7:c9fd242538d9 1589 double L3 = 0.301f;
nerit 7:c9fd242538d9 1590 double L_3=-0.301f;
nerit 4:d32258ec411f 1591 double k1=0.0f;
nerit 4:d32258ec411f 1592 if ((errorePercentuale > L3)||(errorePercentuale < L_3)){
nerit 4:d32258ec411f 1593 k1=errorePercentuale*k6;
nerit 1:e88bf5011af6 1594 }
nerit 4:d32258ec411f 1595 if (((errorePercentuale >= L2)&&(errorePercentuale<=L3))||((errorePercentuale <= L_2)&&(errorePercentuale>=L_3))){
nerit 1:e88bf5011af6 1596 k1=errorePercentuale*k5;
nerit 1:e88bf5011af6 1597 }
nerit 4:d32258ec411f 1598 if (((errorePercentuale < L2)&&(errorePercentuale>L1))||((errorePercentuale > L_2)&&(errorePercentuale<L_1))){
nerit 1:e88bf5011af6 1599 k1=errorePercentuale*k4;
nerit 1:e88bf5011af6 1600 }
nerit 4:d32258ec411f 1601 if ((errorePercentuale < L1)||(errorePercentuale > L_1)){
nerit 1:e88bf5011af6 1602 k1=errorePercentuale*k3;
nerit 1:e88bf5011af6 1603 }
nerit 4:d32258ec411f 1604 double memoCorrezione = k1;
nerit 4:d32258ec411f 1605 if (quincCnt >= 2){
nerit 4:d32258ec411f 1606 correzione = correzione + memoCorrezione;
nerit 4:d32258ec411f 1607 if (correzione > (1.0f - dutyTeorico)){correzione = (1.0f - dutyTeorico);}
nerit 4:d32258ec411f 1608 if ((correzione < 0.0f)&&(dutyTeorico+correzione<0.0f)){correzione = -1.0f*dutyTeorico;}
nerit 4:d32258ec411f 1609 }
nerit 4:d32258ec411f 1610 pulseRised1=0;
nerit 4:d32258ec411f 1611 pulseRised2=0;
nerit 1:e88bf5011af6 1612 #if defined(pcSerial)
nerit 4:d32258ec411f 1613 #if defined(Qnca)
nerit 5:3b95bbfe2dc9 1614 pc.printf("ErTem: %f K1: %f Corr: %f MemoCorr:%f DutyTeo: %f \n",erroreTempo, k1,correzione, memoCorrezione, dutyTeorico);
nerit 4:d32258ec411f 1615 pc.printf("TsP: %f SpW: %f InPic: %f TBec: %f EPerc: %f Duty:%f \n",tractorSpeed_MtS_timed,speedOfSeedWheel,timeIntraPick, tempoTraBecchi_mS,errorePercentuale, dcActualDuty);
nerit 1:e88bf5011af6 1616 #endif
nerit 1:e88bf5011af6 1617 #endif
nerit 4:d32258ec411f 1618 #if defined(pcSerial)
nerit 4:d32258ec411f 1619 #if defined(Qncb)
nerit 4:d32258ec411f 1620 pc.printf("TsP: %f SpW: %f InPic: %f EPerc: %f Duty:%f \n",tractorSpeed_MtS_timed,speedOfSeedWheel,timeIntraPick, errorePercentuale, dcActualDuty);
nerit 4:d32258ec411f 1621 #endif
nerit 4:d32258ec411f 1622 #endif
nerit 0:1e09cd7d66b4 1623 }
nerit 0:1e09cd7d66b4 1624 // introduce il controllo di corrente
nerit 6:3fca0ca1949e 1625 if (currentCheckEnable==true){
nerit 6:3fca0ca1949e 1626 if (incrementCurrent){
nerit 6:3fca0ca1949e 1627 boostDcOut +=0.005f;
nerit 6:3fca0ca1949e 1628 }
nerit 6:3fca0ca1949e 1629 if (reduceCurrent){
nerit 6:3fca0ca1949e 1630 boostDcOut -=0.005f;
nerit 6:3fca0ca1949e 1631 }
nerit 6:3fca0ca1949e 1632 if (boostDcOut >= 0.2f){
nerit 6:3fca0ca1949e 1633 boostDcOut=0.2f;
nerit 6:3fca0ca1949e 1634 all_genericals=1;
nerit 6:3fca0ca1949e 1635 }
nerit 6:3fca0ca1949e 1636 if (boostDcOut <=-0.2f){
nerit 6:3fca0ca1949e 1637 boostDcOut=-0.2f;
nerit 6:3fca0ca1949e 1638 all_genericals=1;
nerit 6:3fca0ca1949e 1639 }
nerit 6:3fca0ca1949e 1640 correzione += boostDcOut;
nerit 0:1e09cd7d66b4 1641 }
nerit 1:e88bf5011af6 1642 DC_brake=0;
nerit 1:e88bf5011af6 1643 DC_forward=1;
nerit 1:e88bf5011af6 1644 DC_prepare();
nerit 0:1e09cd7d66b4 1645
nerit 0:1e09cd7d66b4 1646 // il semiperiodoreale è calcolato sulla lettura del passaggio becchi reale
nerit 5:3b95bbfe2dc9 1647 seedWheelPeriod = semiPeriodoReale;
nerit 0:1e09cd7d66b4 1648 if (seedWheelPeriod < 180.0f){seedWheelPeriod = 180.0f;}
nerit 0:1e09cd7d66b4 1649 if ((oldSeedWheelPeriod!=seedWheelPeriod)&&(seedWheelPeriod >=180.0f )){
nerit 0:1e09cd7d66b4 1650 SDticker.attach_us(&step_SDPulseOut,seedWheelPeriod); // clock time are milliseconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 1651 oldSeedWheelPeriod=seedWheelPeriod;
nerit 0:1e09cd7d66b4 1652 }
nerit 0:1e09cd7d66b4 1653
nerit 4:d32258ec411f 1654 if((quincCnt>=3)){
nerit 0:1e09cd7d66b4 1655 if (correzioneAttiva==1){
nerit 0:1e09cd7d66b4 1656 dcActualDuty = dutyTeorico + correzione;
nerit 4:d32258ec411f 1657 }else{
nerit 4:d32258ec411f 1658 dcActualDuty = dutyTeorico;
nerit 0:1e09cd7d66b4 1659 }
nerit 0:1e09cd7d66b4 1660 }else{
nerit 0:1e09cd7d66b4 1661 dcActualDuty = dutyTeorico;
nerit 0:1e09cd7d66b4 1662 }
nerit 7:c9fd242538d9 1663 if (dcActualDuty <=0.0f){dcActualDuty=0.05f;}
nerit 7:c9fd242538d9 1664 if (dcActualDuty > 0.95f){dcActualDuty = 0.95f;}//dcMaxSpeed;}
nerit 4:d32258ec411f 1665 if (olddcActualDuty!=dcActualDuty){
nerit 7:c9fd242538d9 1666 SDmotorPWM.write(1.0f-dcActualDuty);
nerit 4:d32258ec411f 1667 olddcActualDuty=dcActualDuty;
nerit 4:d32258ec411f 1668 }
nerit 0:1e09cd7d66b4 1669 // allarme
nerit 0:1e09cd7d66b4 1670 if (SDwheelTimer.read_ms()>4000){
nerit 0:1e09cd7d66b4 1671 if (firstStart==0){
nerit 0:1e09cd7d66b4 1672 all_noDcRotati=1;
nerit 0:1e09cd7d66b4 1673 }
nerit 0:1e09cd7d66b4 1674 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 1675 #if defined(VediAllarmi)
nerit 0:1e09cd7d66b4 1676 pc.printf("allarme no DC rotation");
nerit 0:1e09cd7d66b4 1677 #endif
nerit 0:1e09cd7d66b4 1678 #endif
nerit 0:1e09cd7d66b4 1679
nerit 0:1e09cd7d66b4 1680 }
nerit 0:1e09cd7d66b4 1681
nerit 0:1e09cd7d66b4 1682 //***************************************************************************************************************
nerit 0:1e09cd7d66b4 1683 // CONTROLLA TAMBURO
nerit 0:1e09cd7d66b4 1684 //***************************************************************************************************************
nerit 0:1e09cd7d66b4 1685 if(lowSpeed==0){
nerit 0:1e09cd7d66b4 1686 if (syncroCheck==1){
nerit 0:1e09cd7d66b4 1687 syncroCheck=0;
nerit 0:1e09cd7d66b4 1688 lockStart=1;
nerit 0:1e09cd7d66b4 1689 periodo = TBperiod;
nerit 0:1e09cd7d66b4 1690 if (aspettaStart==0){cambiaTB(periodo);}
nerit 0:1e09cd7d66b4 1691 }
nerit 0:1e09cd7d66b4 1692 // controllo di stop
nerit 0:1e09cd7d66b4 1693 double memoIntraP = (double)memoIntraPick*1.8f;
nerit 0:1e09cd7d66b4 1694 if ((double)rotationTimeOut.read_ms()> (memoIntraP)){
nerit 0:1e09cd7d66b4 1695 syncroCheck=0;
nerit 0:1e09cd7d66b4 1696 aspettaStart=1;
nerit 0:1e09cd7d66b4 1697 countCicli=0;
nerit 0:1e09cd7d66b4 1698 if (TBzeroCyclePulse==1){TBticker.detach();}
nerit 0:1e09cd7d66b4 1699 }
nerit 0:1e09cd7d66b4 1700 }else{ // fine ciclo fuori da low speed
nerit 0:1e09cd7d66b4 1701 syncroCheck=0;
nerit 0:1e09cd7d66b4 1702 lockStart=0;
nerit 0:1e09cd7d66b4 1703 if (beccoPronto==1){
nerit 0:1e09cd7d66b4 1704 if (tamburoStandard==1){
nerit 0:1e09cd7d66b4 1705 double ritardoMassimo = 0.0f;
nerit 5:3b95bbfe2dc9 1706 if (encoder==false){
nerit 5:3b95bbfe2dc9 1707 if(speedFromPick==1){
nerit 5:3b95bbfe2dc9 1708 ritardoMassimo = (double)timeIntraPick;
nerit 5:3b95bbfe2dc9 1709 }else{
nerit 5:3b95bbfe2dc9 1710 ritardoMassimo = (double)memoTimeHole;
nerit 5:3b95bbfe2dc9 1711 }
nerit 5:3b95bbfe2dc9 1712 }else{
nerit 0:1e09cd7d66b4 1713 ritardoMassimo = (double)timeIntraPick;
nerit 0:1e09cd7d66b4 1714 }
nerit 6:3fca0ca1949e 1715 int tempoDiSincro = (int)((double)(ritardoMassimo - ((tempoBecco/2.0f)+((speedOfSeedWheel/maxWorkSpeed)*ritardoMassimo)))); //
nerit 0:1e09cd7d66b4 1716 if (tempoDiSincro <= 1){tempoDiSincro=1;}
nerit 0:1e09cd7d66b4 1717 if ((sincroTimer.read_ms()>= tempoDiSincro)){
nerit 0:1e09cd7d66b4 1718 if (tractorSpeed_MtS_timed >= minWorkSpeed){startCicloTB=1;}
nerit 0:1e09cd7d66b4 1719 beccoPronto=0;
nerit 0:1e09cd7d66b4 1720 }
nerit 0:1e09cd7d66b4 1721 }else{
nerit 0:1e09cd7d66b4 1722 // tamburo per zucca
nerit 0:1e09cd7d66b4 1723 if (speedOfSeedWheel >= minWorkSpeed){startCicloTB=1;}
nerit 0:1e09cd7d66b4 1724 beccoPronto=0;
nerit 0:1e09cd7d66b4 1725 }
nerit 0:1e09cd7d66b4 1726 }
nerit 0:1e09cd7d66b4 1727 ciclaTB();
nerit 0:1e09cd7d66b4 1728 }
nerit 0:1e09cd7d66b4 1729 //*************************************************************
nerit 0:1e09cd7d66b4 1730 }else{ // fine ciclo con velocita maggiore di 0
nerit 0:1e09cd7d66b4 1731 SDwheelTimer.stop();
nerit 0:1e09cd7d66b4 1732 SDwheelTimer.reset();
nerit 7:c9fd242538d9 1733 #if defined(seedSensor)
nerit 7:c9fd242538d9 1734 resetDelay();
nerit 7:c9fd242538d9 1735 #endif
nerit 0:1e09cd7d66b4 1736 checkSDrotation=0;
nerit 0:1e09cd7d66b4 1737 oldFaseLavoro=0;
nerit 0:1e09cd7d66b4 1738 aspettaStart=1;
nerit 0:1e09cd7d66b4 1739 countCicli=0;
nerit 0:1e09cd7d66b4 1740 startCycle=0;
nerit 0:1e09cd7d66b4 1741 oldSeedWheelPeriod=0.0f;
nerit 0:1e09cd7d66b4 1742 oldPeriodoTB=0.0f;
nerit 0:1e09cd7d66b4 1743 correzione=0.0f;
nerit 0:1e09cd7d66b4 1744 OLDpulseSpeedInterval=1000.01f;
nerit 0:1e09cd7d66b4 1745 cicloTbinCorso=0;
nerit 0:1e09cd7d66b4 1746 cntTbError=0;
nerit 0:1e09cd7d66b4 1747 if (cycleStopRequest==1){
nerit 0:1e09cd7d66b4 1748 zeroDelay.reset();
nerit 0:1e09cd7d66b4 1749 zeroDelay.start();
nerit 0:1e09cd7d66b4 1750 runRequestBuf=0;
nerit 0:1e09cd7d66b4 1751 zeroRequestBuf=1;
nerit 0:1e09cd7d66b4 1752 cycleStopRequest=0;
nerit 0:1e09cd7d66b4 1753 SDzeroCyclePulse=0;
nerit 0:1e09cd7d66b4 1754 TBzeroCyclePulse=0;
nerit 0:1e09cd7d66b4 1755 zeroCycleEnd=0;
nerit 0:1e09cd7d66b4 1756 zeroCycle=1;
nerit 0:1e09cd7d66b4 1757 zeroTrigger=0;
nerit 0:1e09cd7d66b4 1758 }
nerit 0:1e09cd7d66b4 1759 }
nerit 0:1e09cd7d66b4 1760 //*************************************************************************************************
nerit 0:1e09cd7d66b4 1761 // ciclo di azzeramento motori
nerit 0:1e09cd7d66b4 1762 if ((zeroCycleEnd==0)&&(zeroCycle==1)){//&&(zeroDelay.read_ms()>10000)){
nerit 0:1e09cd7d66b4 1763 if (zeroTrigger==0){
nerit 0:1e09cd7d66b4 1764 TBticker.attach_us(&step_TBPulseOut,1000.0f); // clock time are seconds and attach seed motor stepper controls
nerit 0:1e09cd7d66b4 1765 DC_forward=1;
nerit 0:1e09cd7d66b4 1766 DC_brake=1;
nerit 0:1e09cd7d66b4 1767 DC_prepare();
nerit 0:1e09cd7d66b4 1768 frenata=0;
nerit 0:1e09cd7d66b4 1769 zeroTrigger=1;
nerit 0:1e09cd7d66b4 1770 ritardoStop.reset();
nerit 0:1e09cd7d66b4 1771 ritardoComandoStop.reset();
nerit 0:1e09cd7d66b4 1772 ritardoComandoStop.start();
nerit 0:1e09cd7d66b4 1773 timeOutZeroSD.start();
nerit 0:1e09cd7d66b4 1774 quincTimeSet.reset();
nerit 0:1e09cd7d66b4 1775 }
nerit 0:1e09cd7d66b4 1776 int tempoFrenata=300;
nerit 0:1e09cd7d66b4 1777 if (((ritardoStop.read_ms()>tempoFrenata)&&(SDzeroDebounced==0))||(accensione==1)||(ritardoComandoStop.read_ms()>400)){
nerit 0:1e09cd7d66b4 1778 accensione=0;
nerit 0:1e09cd7d66b4 1779 avvicinamento=1;
nerit 0:1e09cd7d66b4 1780 avvicinamentoOn.reset();
nerit 0:1e09cd7d66b4 1781 avvicinamentoOn.start();
nerit 7:c9fd242538d9 1782 SDmotorPWM.write(1.0f-0.32f); // duty cycle = 60% of power
nerit 0:1e09cd7d66b4 1783 DC_forward=1;
nerit 0:1e09cd7d66b4 1784 DC_brake=0;
nerit 0:1e09cd7d66b4 1785 DC_prepare();
nerit 0:1e09cd7d66b4 1786 ritardoComandoStop.reset();
nerit 0:1e09cd7d66b4 1787 ritardoComandoStop.stop();
nerit 0:1e09cd7d66b4 1788 }
nerit 0:1e09cd7d66b4 1789 if (avvicinamento==1){
nerit 0:1e09cd7d66b4 1790 if(avvicinamentoOn.read_ms()>300){
nerit 7:c9fd242538d9 1791 SDmotorPWM.write(1.0f-0.7f);
nerit 0:1e09cd7d66b4 1792 avvicinamentoOn.reset();
nerit 0:1e09cd7d66b4 1793 avvicinamentoOff.reset();
nerit 0:1e09cd7d66b4 1794 avvicinamentoOff.start();
nerit 0:1e09cd7d66b4 1795 }
nerit 0:1e09cd7d66b4 1796 if(avvicinamentoOff.read_ms()>100){
nerit 7:c9fd242538d9 1797 SDmotorPWM.write(1.0f-0.32f);
nerit 0:1e09cd7d66b4 1798 avvicinamentoOff.reset();
nerit 0:1e09cd7d66b4 1799 avvicinamentoOff.stop();
nerit 0:1e09cd7d66b4 1800 avvicinamentoOn.start();
nerit 0:1e09cd7d66b4 1801 }
nerit 0:1e09cd7d66b4 1802 }else{
nerit 0:1e09cd7d66b4 1803 avvicinamentoOn.stop();
nerit 0:1e09cd7d66b4 1804 avvicinamentoOff.stop();
nerit 0:1e09cd7d66b4 1805 avvicinamentoOn.reset();
nerit 0:1e09cd7d66b4 1806 avvicinamentoOff.reset();
nerit 0:1e09cd7d66b4 1807 }
nerit 0:1e09cd7d66b4 1808 if (frenata==0){
nerit 0:1e09cd7d66b4 1809 if (SDzeroCyclePulse==1){
nerit 0:1e09cd7d66b4 1810 SDticker.detach();
nerit 0:1e09cd7d66b4 1811 frenata=1;
nerit 0:1e09cd7d66b4 1812 quincTimeSet.reset();
nerit 0:1e09cd7d66b4 1813 quincTimeSet.start();
nerit 0:1e09cd7d66b4 1814 ritardoStop.start();
nerit 0:1e09cd7d66b4 1815 //ritardoComandoStop.reset();
nerit 0:1e09cd7d66b4 1816 //ritardoComandoStop.stop();
nerit 0:1e09cd7d66b4 1817 }
nerit 0:1e09cd7d66b4 1818 }else{
nerit 7:c9fd242538d9 1819 #if defined(mezzo)
nerit 7:c9fd242538d9 1820 if (quinconceActive==0){
nerit 7:c9fd242538d9 1821 if (SDzeroCyclePulse==1){
nerit 7:c9fd242538d9 1822 avvicinamento=0;
nerit 7:c9fd242538d9 1823 DC_brake=1;
nerit 7:c9fd242538d9 1824 DC_prepare();
nerit 7:c9fd242538d9 1825 SDzeroed=1;
nerit 7:c9fd242538d9 1826 ritardoStop.reset();
nerit 7:c9fd242538d9 1827 ritardoStop.stop();
nerit 7:c9fd242538d9 1828 }
nerit 7:c9fd242538d9 1829 }else{
nerit 7:c9fd242538d9 1830 if (quincTimeSet.read_ms()>700){
nerit 7:c9fd242538d9 1831 avvicinamento=0;
nerit 7:c9fd242538d9 1832 DC_brake=1;
nerit 7:c9fd242538d9 1833 DC_prepare();
nerit 7:c9fd242538d9 1834 SDzeroed=1;
nerit 7:c9fd242538d9 1835 ritardoStop.reset();
nerit 7:c9fd242538d9 1836 ritardoStop.stop();
nerit 7:c9fd242538d9 1837 quincTimeSet.stop();
nerit 7:c9fd242538d9 1838 }
nerit 7:c9fd242538d9 1839 }
nerit 7:c9fd242538d9 1840 #else
nerit 0:1e09cd7d66b4 1841 if (SDzeroCyclePulse==1){
nerit 0:1e09cd7d66b4 1842 avvicinamento=0;
nerit 0:1e09cd7d66b4 1843 DC_brake=1;
nerit 0:1e09cd7d66b4 1844 DC_prepare();
nerit 0:1e09cd7d66b4 1845 SDzeroed=1;
nerit 0:1e09cd7d66b4 1846 ritardoStop.reset();
nerit 0:1e09cd7d66b4 1847 ritardoStop.stop();
nerit 0:1e09cd7d66b4 1848 }
nerit 7:c9fd242538d9 1849 #endif
nerit 0:1e09cd7d66b4 1850 }
nerit 0:1e09cd7d66b4 1851 // azzera tutto in time out
nerit 0:1e09cd7d66b4 1852 if (timeOutZeroSD.read_ms()>=10000){
nerit 0:1e09cd7d66b4 1853 if (firstStart==0){
nerit 0:1e09cd7d66b4 1854 all_no_Zeroing=1;
nerit 0:1e09cd7d66b4 1855 }
nerit 0:1e09cd7d66b4 1856 avvicinamento=0;
nerit 0:1e09cd7d66b4 1857 DC_brake=1;
nerit 0:1e09cd7d66b4 1858 DC_prepare();
nerit 0:1e09cd7d66b4 1859 SDzeroed=1;
nerit 0:1e09cd7d66b4 1860 ritardoStop.reset();
nerit 0:1e09cd7d66b4 1861 ritardoStop.stop();
nerit 0:1e09cd7d66b4 1862 avvicinamentoOn.stop();
nerit 0:1e09cd7d66b4 1863 avvicinamentoOff.stop();
nerit 0:1e09cd7d66b4 1864 avvicinamentoOn.reset();
nerit 0:1e09cd7d66b4 1865 avvicinamentoOff.reset();
nerit 0:1e09cd7d66b4 1866 ritardoComandoStop.reset();
nerit 0:1e09cd7d66b4 1867 ritardoComandoStop.stop();
nerit 0:1e09cd7d66b4 1868 timeOutZeroSD.stop();
nerit 0:1e09cd7d66b4 1869 timeOutZeroSD.reset();
nerit 0:1e09cd7d66b4 1870 }
nerit 0:1e09cd7d66b4 1871 if (TBzeroCyclePulse==1){
nerit 0:1e09cd7d66b4 1872 TBticker.detach();
nerit 0:1e09cd7d66b4 1873 TBzeroed=1;
nerit 0:1e09cd7d66b4 1874 }
nerit 0:1e09cd7d66b4 1875 if ((SDzeroed==1)&&(TBzeroed==1)){
nerit 0:1e09cd7d66b4 1876 avvicinamentoOn.stop();
nerit 0:1e09cd7d66b4 1877 avvicinamentoOff.stop();
nerit 0:1e09cd7d66b4 1878 ritardoComandoStop.stop();
nerit 0:1e09cd7d66b4 1879 ritardoStop.stop();
nerit 0:1e09cd7d66b4 1880 zeroCycleEnd=1;
nerit 0:1e09cd7d66b4 1881 zeroCycle=0;
nerit 0:1e09cd7d66b4 1882 zeroTrigger=0;
nerit 0:1e09cd7d66b4 1883 runRequestBuf=1;
nerit 0:1e09cd7d66b4 1884 zeroRequestBuf=0;
nerit 0:1e09cd7d66b4 1885 cycleStopRequest=0;
nerit 0:1e09cd7d66b4 1886 SDzeroed=0;
nerit 0:1e09cd7d66b4 1887 TBzeroed=0;
nerit 0:1e09cd7d66b4 1888 timeOutZeroSD.stop();
nerit 0:1e09cd7d66b4 1889 timeOutZeroSD.reset();
nerit 0:1e09cd7d66b4 1890 }
nerit 0:1e09cd7d66b4 1891 }
nerit 0:1e09cd7d66b4 1892
nerit 0:1e09cd7d66b4 1893 //*************************************************************************************************
nerit 0:1e09cd7d66b4 1894 if (enableCycle==0){
nerit 0:1e09cd7d66b4 1895 zeroTrigger=0;
nerit 0:1e09cd7d66b4 1896 SDmotorPWM=0;
nerit 0:1e09cd7d66b4 1897 SDmotorInA=0;
nerit 0:1e09cd7d66b4 1898 SDmotorInB=0;
nerit 0:1e09cd7d66b4 1899 }
nerit 0:1e09cd7d66b4 1900 SDzeroCyclePulse=0;
nerit 0:1e09cd7d66b4 1901 TBzeroCyclePulse=0;
nerit 0:1e09cd7d66b4 1902 //*************************************************************************************************
nerit 0:1e09cd7d66b4 1903 }else{//end ciclo normale
nerit 0:1e09cd7d66b4 1904 //*************************************************************************************************
nerit 0:1e09cd7d66b4 1905 // task di prova della scheda
nerit 0:1e09cd7d66b4 1906 //*************************************************************************************************
nerit 0:1e09cd7d66b4 1907 #if defined(provaScheda)
nerit 0:1e09cd7d66b4 1908 clocca++;
nerit 0:1e09cd7d66b4 1909 //led = !led;
nerit 0:1e09cd7d66b4 1910 //txMsg.clear();
nerit 0:1e09cd7d66b4 1911 //txMsg << clocca;
nerit 0:1e09cd7d66b4 1912 //test.printf("aogs \n");
nerit 0:1e09cd7d66b4 1913 //if(can1.write(txMsg)){
nerit 0:1e09cd7d66b4 1914 #if defined(pcSerial)
nerit 0:1e09cd7d66b4 1915 pc.printf("Can write OK \n");
nerit 0:1e09cd7d66b4 1916 #endif
nerit 0:1e09cd7d66b4 1917 //}
nerit 0:1e09cd7d66b4 1918 switch (clocca){
nerit 0:1e09cd7d66b4 1919 case 1:
nerit 0:1e09cd7d66b4 1920 TBmotorStepOut=1; // define step command for up down motor driver
nerit 0:1e09cd7d66b4 1921 break;
nerit 0:1e09cd7d66b4 1922 case 2:
nerit 0:1e09cd7d66b4 1923 SDmotorPWM=1; // define step command for seeding whell motor driver
nerit 0:1e09cd7d66b4 1924 break;
nerit 0:1e09cd7d66b4 1925 case 3:
nerit 0:1e09cd7d66b4 1926 speedClock=1; // define input of
nerit 0:1e09cd7d66b4 1927 break;
nerit 0:1e09cd7d66b4 1928 case 4:
nerit 0:1e09cd7d66b4 1929 break;
nerit 0:1e09cd7d66b4 1930 case 5:
nerit 0:1e09cd7d66b4 1931 SDmotorInA=1;
nerit 0:1e09cd7d66b4 1932 SDmotorInB=0;
nerit 0:1e09cd7d66b4 1933 break;
nerit 0:1e09cd7d66b4 1934 case 6:
nerit 0:1e09cd7d66b4 1935 break;
nerit 0:1e09cd7d66b4 1936 case 7:
nerit 0:1e09cd7d66b4 1937 break;
nerit 0:1e09cd7d66b4 1938 case 8:
nerit 0:1e09cd7d66b4 1939 break;
nerit 0:1e09cd7d66b4 1940 case 9:
nerit 0:1e09cd7d66b4 1941 break;
nerit 0:1e09cd7d66b4 1942 case 10:
nerit 0:1e09cd7d66b4 1943 break;
nerit 0:1e09cd7d66b4 1944 case 11:
nerit 0:1e09cd7d66b4 1945 break;
nerit 0:1e09cd7d66b4 1946 case 12:
nerit 0:1e09cd7d66b4 1947 break;
nerit 0:1e09cd7d66b4 1948 case 13:
nerit 0:1e09cd7d66b4 1949 break;
nerit 0:1e09cd7d66b4 1950 case 14:
nerit 0:1e09cd7d66b4 1951 SDmotorPWM=1; // power mosfet 2 command out
nerit 0:1e09cd7d66b4 1952 break;
nerit 0:1e09cd7d66b4 1953 case 15:
nerit 0:1e09cd7d66b4 1954 break;
nerit 0:1e09cd7d66b4 1955 case 16:
nerit 0:1e09cd7d66b4 1956 case 17:
nerit 0:1e09cd7d66b4 1957 break;
nerit 0:1e09cd7d66b4 1958 case 18:
nerit 0:1e09cd7d66b4 1959 TBmotorStepOut=0; // define step command for up down motor driver
nerit 0:1e09cd7d66b4 1960 SDmotorPWM=0; // define step command for seeding whell motor driver
nerit 0:1e09cd7d66b4 1961 speedClock=0; // define input of
nerit 0:1e09cd7d66b4 1962 SDmotorInA=0;
nerit 0:1e09cd7d66b4 1963 SDmotorInB=0;
nerit 0:1e09cd7d66b4 1964 SDmotorPWM=0; // power mosfet 2 command out
nerit 0:1e09cd7d66b4 1965 break;
nerit 0:1e09cd7d66b4 1966 default:
nerit 0:1e09cd7d66b4 1967 clocca=0;
nerit 0:1e09cd7d66b4 1968 break;
nerit 0:1e09cd7d66b4 1969 }
nerit 0:1e09cd7d66b4 1970 wait_ms(100);
nerit 0:1e09cd7d66b4 1971 #endif // end prova scheda
nerit 0:1e09cd7d66b4 1972
nerit 0:1e09cd7d66b4 1973 #if defined(provaDC)
nerit 0:1e09cd7d66b4 1974 int rampa=1000;
nerit 0:1e09cd7d66b4 1975 int pausa=3000;
nerit 0:1e09cd7d66b4 1976 switch (clocca){
nerit 0:1e09cd7d66b4 1977 case 0:
nerit 0:1e09cd7d66b4 1978 DC_brake=0;
nerit 0:1e09cd7d66b4 1979 DC_forward=1;
nerit 0:1e09cd7d66b4 1980 duty_DC+=0.01f;
nerit 7:c9fd242538d9 1981 if (duty_DC>=1.0f){
nerit 7:c9fd242538d9 1982 duty_DC=1.0f;
nerit 0:1e09cd7d66b4 1983 clocca=1;
nerit 0:1e09cd7d66b4 1984 }
nerit 0:1e09cd7d66b4 1985 wait_ms(rampa);
nerit 0:1e09cd7d66b4 1986 break;
nerit 0:1e09cd7d66b4 1987 case 1:
nerit 0:1e09cd7d66b4 1988 wait_ms(pausa*4);
nerit 0:1e09cd7d66b4 1989 clocca=2;
nerit 0:1e09cd7d66b4 1990 break;
nerit 0:1e09cd7d66b4 1991 case 2:
nerit 0:1e09cd7d66b4 1992 DC_brake=0;
nerit 0:1e09cd7d66b4 1993 DC_forward=1;
nerit 0:1e09cd7d66b4 1994 duty_DC-=0.01f;
nerit 0:1e09cd7d66b4 1995 if (duty_DC<=0.0f){
nerit 0:1e09cd7d66b4 1996 duty_DC=0.0f;
nerit 0:1e09cd7d66b4 1997 clocca=3;
nerit 0:1e09cd7d66b4 1998 }
nerit 0:1e09cd7d66b4 1999 wait_ms(rampa);
nerit 0:1e09cd7d66b4 2000 break;
nerit 0:1e09cd7d66b4 2001 case 3:
nerit 0:1e09cd7d66b4 2002 wait_ms(pausa);
nerit 0:1e09cd7d66b4 2003 clocca=4;
nerit 0:1e09cd7d66b4 2004 break;
nerit 0:1e09cd7d66b4 2005 case 4:
nerit 0:1e09cd7d66b4 2006 DC_brake=0;
nerit 0:1e09cd7d66b4 2007 DC_forward=1;
nerit 0:1e09cd7d66b4 2008 duty_DC+=0.01f;
nerit 0:1e09cd7d66b4 2009 if (duty_DC>=1.0f){
nerit 0:1e09cd7d66b4 2010 duty_DC=1.0f;
nerit 0:1e09cd7d66b4 2011 clocca=5;
nerit 0:1e09cd7d66b4 2012 }
nerit 0:1e09cd7d66b4 2013 wait_ms(rampa);
nerit 0:1e09cd7d66b4 2014 break;
nerit 0:1e09cd7d66b4 2015 case 5:
nerit 0:1e09cd7d66b4 2016 wait_ms(pausa);
nerit 0:1e09cd7d66b4 2017 clocca=6;
nerit 0:1e09cd7d66b4 2018 break;
nerit 0:1e09cd7d66b4 2019 case 6:
nerit 0:1e09cd7d66b4 2020 DC_brake=0;
nerit 0:1e09cd7d66b4 2021 DC_forward=1;
nerit 0:1e09cd7d66b4 2022 duty_DC-=0.01f;
nerit 0:1e09cd7d66b4 2023 if (duty_DC<=0.0f){
nerit 0:1e09cd7d66b4 2024 duty_DC=0.0f;
nerit 0:1e09cd7d66b4 2025 clocca=7;
nerit 0:1e09cd7d66b4 2026 }
nerit 0:1e09cd7d66b4 2027 wait_ms(rampa);
nerit 0:1e09cd7d66b4 2028 break;
nerit 0:1e09cd7d66b4 2029 case 7:
nerit 0:1e09cd7d66b4 2030 wait_ms(pausa);
nerit 0:1e09cd7d66b4 2031 clocca=0;
nerit 0:1e09cd7d66b4 2032 break;
nerit 0:1e09cd7d66b4 2033 default:
nerit 0:1e09cd7d66b4 2034 break;
nerit 0:1e09cd7d66b4 2035 }
nerit 0:1e09cd7d66b4 2036 if (oldDuty_DC != duty_DC){
nerit 7:c9fd242538d9 2037 SDmotorPWM.write(1.0f-duty_DC); // duty cycle = stop
nerit 0:1e09cd7d66b4 2038 oldDuty_DC=duty_DC;
nerit 0:1e09cd7d66b4 2039 DC_prepare();
nerit 0:1e09cd7d66b4 2040 }
nerit 0:1e09cd7d66b4 2041 #endif
nerit 0:1e09cd7d66b4 2042 }//end in prova
nerit 0:1e09cd7d66b4 2043 wd.Service(); // kick the dog before the timeout
nerit 0:1e09cd7d66b4 2044 } // end while
nerit 0:1e09cd7d66b4 2045 }// end main
nerit 0:1e09cd7d66b4 2046