Interface to a standard tarco sensor. Measure the periode and the cal the speed

Committer:
gert_lauritsen
Date:
Thu Sep 11 07:43:31 2014 +0000
Revision:
8:2e6b17f1da02
Parent:
7:752b8065ce2d
Child:
9:6b8619ccb136
callback tilf?jet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 2:c50da089786a 1 #include "Tarco.h"
gert_lauritsen 4:35024179a736 2 #include "mbed.h"
gert_lauritsen 4:35024179a736 3
gert_lauritsen 4:35024179a736 4 DigitalOut debug(LED2);
gert_lauritsen 0:8421e11df32a 5
gert_lauritsen 8:2e6b17f1da02 6 Tarco::Tarco(PinName tarcosignal, char mode,callback_type _callback) :
gert_lauritsen 4:35024179a736 7 _tarcosensor(tarcosignal)
gert_lauritsen 4:35024179a736 8 {
gert_lauritsen 4:35024179a736 9 tarcomode=mode;
gert_lauritsen 7:752b8065ce2d 10 // AdvPeriode=0;
gert_lauritsen 8:2e6b17f1da02 11 if (mode==RunMeanSwitchAuto)
gert_lauritsen 7:752b8065ce2d 12 _tarcosensor.fall(this, &Tarco::RunMeanMeasure);
gert_lauritsen 8:2e6b17f1da02 13 if (mode==SwitchAuto)
gert_lauritsen 7:752b8065ce2d 14 _tarcosensor.fall(this, &Tarco::Rotationtimer);
gert_lauritsen 8:2e6b17f1da02 15 if (mode==HirthMode)
gert_lauritsen 8:2e6b17f1da02 16 _tarcosensor.fall(this, &Tarco::Hird);
gert_lauritsen 8:2e6b17f1da02 17 callback = _callback;
gert_lauritsen 8:2e6b17f1da02 18 }
gert_lauritsen 8:2e6b17f1da02 19
gert_lauritsen 8:2e6b17f1da02 20 void Tarco::Hird()
gert_lauritsen 8:2e6b17f1da02 21 {
gert_lauritsen 8:2e6b17f1da02 22 //Hird er en totakt, der kommer med
gert_lauritsen 8:2e6b17f1da02 23 t.stop();
gert_lauritsen 8:2e6b17f1da02 24 AdvPeriode[0]=t.read_us();
gert_lauritsen 8:2e6b17f1da02 25 t.reset();
gert_lauritsen 8:2e6b17f1da02 26 t.start();
gert_lauritsen 0:8421e11df32a 27 }
gert_lauritsen 0:8421e11df32a 28
gert_lauritsen 7:752b8065ce2d 29 void Tarco::Rotationtimer()
gert_lauritsen 4:35024179a736 30 {
gert_lauritsen 7:752b8065ce2d 31 //Da switchAuto motoren kommer ud med 24 pulser/rotation laver vi en prescale på 24
gert_lauritsen 7:752b8065ce2d 32 //
gert_lauritsen 7:752b8065ce2d 33 static int pulscounter;
gert_lauritsen 8:2e6b17f1da02 34 if (pulscounter++>=PulsPrRotation-1) { //Vi tæller til 24, således at det er tiden/omgang
gert_lauritsen 4:35024179a736 35 t.stop();
gert_lauritsen 7:752b8065ce2d 36 AdvPeriode[0]=t.read_us();
gert_lauritsen 8:2e6b17f1da02 37 callback(AdvPeriode[0]);
gert_lauritsen 4:35024179a736 38 t.reset();
gert_lauritsen 4:35024179a736 39 t.start();
gert_lauritsen 7:752b8065ce2d 40 pulscounter=0;
gert_lauritsen 4:35024179a736 41 }
gert_lauritsen 0:8421e11df32a 42 }
gert_lauritsen 0:8421e11df32a 43
gert_lauritsen 4:35024179a736 44
gert_lauritsen 7:752b8065ce2d 45 void Tarco::RunMeanMeasure()
gert_lauritsen 4:35024179a736 46 {
gert_lauritsen 7:752b8065ce2d 47 static unsigned int TarcoHead;
gert_lauritsen 7:752b8065ce2d 48 //Måler tarco ved at lave en runing mean på periodetiderne, hvis der en variation
gert_lauritsen 7:752b8065ce2d 49 //på over 20% bliver den pågældende periode skippet
gert_lauritsen 8:2e6b17f1da02 50 //Hvilket btyder at man skipper den lange Syncpuls, og kun tællere på de korte
gert_lauritsen 8:2e6b17f1da02 51
gert_lauritsen 7:752b8065ce2d 52 static bool MistedPeriod; //dette var en lang periode
gert_lauritsen 7:752b8065ce2d 53 int thisperiode;
gert_lauritsen 7:752b8065ce2d 54 float periodechange=0;
gert_lauritsen 4:35024179a736 55 t.stop();
gert_lauritsen 5:451dd1189b0f 56 if (TarcoHead>=TarcoRunMean) TarcoHead=0;
gert_lauritsen 6:2ad5029f7b1a 57 thisperiode=t.read_us();
gert_lauritsen 7:752b8065ce2d 58 if (lastperiode!=0)
gert_lauritsen 7:752b8065ce2d 59 periodechange=(float) thisperiode/lastperiode;
gert_lauritsen 6:2ad5029f7b1a 60 if((periodechange<1.2) || (lastperiode==0) || MistedPeriod) {
gert_lauritsen 7:752b8065ce2d 61 AdvPeriode[TarcoHead++]=thisperiode;
gert_lauritsen 7:752b8065ce2d 62 MistedPeriod=false;
gert_lauritsen 7:752b8065ce2d 63 } else
gert_lauritsen 7:752b8065ce2d 64 MistedPeriod=true;
gert_lauritsen 4:35024179a736 65 t.reset();
gert_lauritsen 5:451dd1189b0f 66 t.start();
gert_lauritsen 0:8421e11df32a 67 }
gert_lauritsen 0:8421e11df32a 68
gert_lauritsen 4:35024179a736 69 float Tarco::Speed()
gert_lauritsen 4:35024179a736 70 {
gert_lauritsen 4:35024179a736 71 //returns puls/sek [hz]
gert_lauritsen 7:752b8065ce2d 72 unsigned int periodetid;
gert_lauritsen 7:752b8065ce2d 73 lastfrekvens=0;
gert_lauritsen 7:752b8065ce2d 74 switch (tarcomode) {
gert_lauritsen 8:2e6b17f1da02 75 case RunMeanSwitchAuto: {
gert_lauritsen 7:752b8065ce2d 76 periodetid=0;
gert_lauritsen 7:752b8065ce2d 77 for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i];
gert_lauritsen 7:752b8065ce2d 78 lastperiode=periodetid/TarcoRunMean;
gert_lauritsen 7:752b8065ce2d 79 lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25);
gert_lauritsen 7:752b8065ce2d 80 } break;
gert_lauritsen 8:2e6b17f1da02 81 case SwitchAuto: {
gert_lauritsen 7:752b8065ce2d 82 lastfrekvens=(1e6/AdvPeriode[0]);
gert_lauritsen 7:752b8065ce2d 83 } break;
gert_lauritsen 8:2e6b17f1da02 84 case HirthMode: {
gert_lauritsen 8:2e6b17f1da02 85 lastfrekvens=(500e3/AdvPeriode[0]); //vi skal kun gannge med det halve da der er 2 pulser /omgang
gert_lauritsen 8:2e6b17f1da02 86 } break;
gert_lauritsen 8:2e6b17f1da02 87
gert_lauritsen 7:752b8065ce2d 88 default: break;
gert_lauritsen 7:752b8065ce2d 89 }
gert_lauritsen 7:752b8065ce2d 90 return (float) lastfrekvens;
gert_lauritsen 2:c50da089786a 91 }
gert_lauritsen 4:35024179a736 92
gert_lauritsen 4:35024179a736 93 float Tarco::RPM()
gert_lauritsen 4:35024179a736 94 {
gert_lauritsen 7:752b8065ce2d 95 float rpm=lastfrekvens*60;
gert_lauritsen 7:752b8065ce2d 96 return (float) rpm;
gert_lauritsen 4:35024179a736 97 }
gert_lauritsen 4:35024179a736 98
gert_lauritsen 4:35024179a736 99