/* skip header and return average with of a short pulse */ float skipHeader(int8_t *buffer, int32_t *index, int32_t size) { int32_t width; int32_t count = 0; float average = 0; /* skip first pulse for phase independance */ getPulseWidth(buffer,index,size); while (*index<size) { width=getPulseWidth(buffer,index,size); if (average && width>(float)average*window ) { *index-=width; return average; } /* average=(count*average+width)/++count; */ count++; average=((count-1)*average+width)/count; } return average; }
//------------------------------------------------------------------------------ // transmit() -- send radar emissions //------------------------------------------------------------------------------ void Radar::transmit(const LCreal dt) { BaseClass::transmit(dt); // Transmitting, scanning and have an antenna? if ( !areEmissionsDisabled() && isTransmitting() ) { // Send the emission to the other player Emission* em = new Emission(); em->setFrequency(getFrequency()); em->setBandwidth(getBandwidth()); const LCreal prf1 = getPRF(); em->setPRF(prf1); int pulses = static_cast<int>(prf1 * dt + 0.5); if (pulses == 0) pulses = 1; // at least one em->setPulses(pulses); const LCreal p = getPeakPower(); em->setPower(p); em->setMaxRangeNM(getRange()); em->setPulseWidth(getPulseWidth()); em->setTransmitLoss(getRfTransmitLoss()); em->setReturnRequest( isReceiverEnabled() ); em->setTransmitter(this); getAntenna()->rfTransmit(em); em->unref(); } }
int Sirc::getRawCode(){ //Start with a empty sirc code. int sircCode = 0; //Wait for start burst. //The start burst is +- 2400 us. while(true){ //Get the pulse width. int pulse = getPulseWidth(); //Check if getting the pulse width has timed out. if(pulse == TIMEOUT){ //Return the timeout value (-1). return TIMEOUT; } //Check if the pulse width is the start burst. //Due to background interference the start burst is not exactly 2400 us. //The length can swing in both directions. //To correct this error we also accept a burst of length 2300 us and anything higer. if (pulse >=23){ //Get out of the loop. break; } } //Read the next 12 bits. for(int i=0;i<12;++i){ //We get the new pulse width. //We just assume at this point it will not longer time out. int pulse = getPulseWidth(); //We check if the pulse width is longer then 800 us. //0 = 600 us //1 = 1200 us //We have the same problem with background interference here. //So we ccept anything equal and below 800 us as a 0 and anthing above as a 1. if(pulse > 8){ //Write a 1 on the first bit. //Leave the other bits untouched. sircCode |= 0x01; } //Shift the bits to the left. sircCode <<= 1; } //We have shifted one to may times to the left in the above loop. //To fix this we shift all the bits back to the right. sircCode >>= 1; //Return the sirc code. return sircCode; }
bool XAGYLWheel::getSettingInfo() { bool rc1 = getMaximumSpeed(); bool rc2 = getJitter(); bool rc3 = getThreshold(); bool rc4 = true; if (firmwareVersion >= 3) rc4 = getPulseWidth(); return (rc1 && rc2 && rc3 && rc4); }
/* detect headers */ bool isHeader(int8_t *buffer, int32_t index, int32_t size) { int32_t width; int32_t pulses = 0; int32_t biggest = 0; /* skip first pulse for phase independance */ getPulseWidth(buffer,&index,size); while (index<size && pulses<THRESHOLD_HEADER ) { width = getPulseWidth(buffer,&index,size); if (!biggest) biggest=width; if (width>(float)biggest*window) return false; if (width>biggest) biggest = width; pulses++; } if (pulses>=THRESHOLD_HEADER) return true; return false; }
/* read a byte from wave data */ int readByte(int8_t *buffer, int32_t *index, int32_t size, float average) { int bit; int32_t width; int value = 0; int i; /* start bit (int32_t pulse) */ width=getPulseWidth(buffer,index,size); if (isSilence(buffer,*index,size) || width<average*window) return -1; /* data bits (lsb first) */ for (bit=0;bit<8;bit++) { width=getPulseWidth(buffer,index,size); if (isSilence(buffer,*index,size)) return -1; if (width<average*window) { value+=(1<<bit); getPulseWidth(buffer,index,size); /* skip 2nd short pulse */ if (isSilence(buffer,*index,size)) return -1; } } /* two stop bits (four short pulses) */ for (i=0;i<3;i++) { getPulseWidth(buffer,index,size); if (isSilence(buffer,*index,size)) return -1; } getPulseWidth(buffer,index,size); return value; }
bool XAGYLWheel::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n) { if(strcmp(dev,getDeviceName())==0) { if (!strcmp(OffsetNP.name, name)) { bool rc_offset=true; for (int i=0; i < n; i++) { if (!strcmp(names[i], OffsetN[i].name)) { while (values[i] != OffsetN[i].value && rc_offset) { if (values[i] > OffsetN[i].value) rc_offset = setOffset(i, 1); else rc_offset = setOffset(i, -1); } } } OffsetNP.s = rc_offset ? IPS_OK : IPS_ALERT; IDSetNumber(&OffsetNP, NULL); return true; } if (!strcmp(SettingsNP.name, name)) { double newSpeed, newJitter, newThreshold, newPulseWidth; for (int i=0; i < n; i++) { if (!strcmp(names[i], SettingsN[SET_SPEED].name)) newSpeed = values[i]; else if (!strcmp(names[i], SettingsN[SET_JITTER].name)) newJitter = values[i]; if (!strcmp(names[i], SettingsN[SET_THRESHOLD].name)) newThreshold = values[i]; if (!strcmp(names[i], SettingsN[SET_PULSE_WITDH].name)) newPulseWidth = values[i]; } bool rc_speed=true, rc_jitter=true, rc_threshold=true, rc_pulsewidth=true; if (newSpeed != SettingsN[SET_SPEED].value) { rc_speed = setCommand(SET_SPEED, newSpeed); getMaximumSpeed(); } // Jitter while (newJitter != SettingsN[SET_JITTER].value && rc_jitter) { if (newJitter > SettingsN[SET_JITTER].value) { rc_jitter &= setCommand(SET_JITTER, 1); getJitter(); } else { rc_jitter &= setCommand(SET_JITTER, -1); getJitter(); } } // Threshold while (newThreshold != SettingsN[SET_THRESHOLD].value && rc_threshold) { if (newThreshold > SettingsN[SET_THRESHOLD].value) { rc_threshold &= setCommand(SET_THRESHOLD, 1); getThreshold(); } else { rc_threshold &= setCommand(SET_THRESHOLD, -1); getThreshold(); } } // Pulse width while (firmwareVersion >= 3 && newPulseWidth != SettingsN[SET_PULSE_WITDH].value && rc_pulsewidth) { if (newPulseWidth> SettingsN[SET_PULSE_WITDH].value) { rc_pulsewidth &= setCommand(SET_PULSE_WITDH, 1); getPulseWidth(); } else { rc_pulsewidth &= setCommand(SET_PULSE_WITDH, -1); getPulseWidth(); } } if (rc_speed && rc_jitter && rc_threshold && rc_pulsewidth) SettingsNP.s = IPS_OK; else SettingsNP.s = IPS_ALERT; IDSetNumber(&SettingsNP, NULL); return true; } } return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n); }
int main(void) { uint16_t i, result, averageResult; uint8_t pwmOut; pwmOut = 0; // Set Output/Input modes for pins // PWM_LEDS (PB2) as output (set 1) DDRB |= (1<<DDB2); // TRIG (PD3) as output (set 1) US_DDR |= (1<<DDD3); _delay_us(10); // ECHO (PD2) as input (set 0) NOT NECESSARY DEFAULT IS INPUT // US_DDR &= (0b111111011); // configure timer 0 NOT NECESSARY HERE, is done in // set prescaler = FCPU/256 // TCCR0B |= (1<<CS02); // initialize counter // TCNT0 = 0; // configure PWM // Set non-inverting 8-bit fast PWM with no pre-scaler (full Fclk) TCCR1A = 0x23; TCCR1B = 0x09; while (1) { // Read from SR04 // DESIRED: Average 4 or so readings. averageResult = 0; for(i = 0; i < 4; i++) { // Give the SR04 TRIG pin a 15 us High Pulse US_PORT |= (1<<US_TRIG_POS); _delay_us(16); US_PORT &= (~(1<<US_TRIG_POS)); result = getPulseWidth(); if(result == US_ERROR) { // SR04 timed out. Reset everything. averageResult = 0; break; } else { averageResult += result; } } // convert to PWM if(averageResult > 0 ) { averageResult = averageResult >> 2; // divide by 4 to get average // now check if further than 3 inches away if(averageResult > MINIMUM_DISTANCE) { // if it is, convert it to our 16 bit PWM value pwmOut = (uint8_t) (averageResult - MINIMUM_DISTANCE) * MICROSEC_TO_PWM; } else { pwmOut = 0; } } // Set PWM out accordingly OCR1A = pwmOut; }