Example #1
0
int main(void) {
  const Mesh m(IPoint(MV::fill,2,2));
  Tensor<DataMesh> t(3,"aa",m ,0.0);
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){

      for(int k=0;k<m.Size();k++){
	srand ( time(NULL) );
	 t(i,j)[k]=rand()/((k+1)*10);

      }
    }


  }
 
  
  Tensor<DataMesh> inv = findInverse(t);
  Tensor<DataMesh> inv2 = findInverse(inv);
  
  checkInverse(inv,inv2);
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){

      
      std::cout<<inv(i,j)-inv2(i,j)<<std::endl;
    }
  }
  
  
  return EXIT_SUCCESS;
}
Example #2
0
// Starter function for modular product
void modprod(char *modP1, char *modP2) {

	unsigned char *modP1Char = malloc(8 * sizeof (unsigned char));
	unsigned char *modP2Char = malloc(8 * sizeof (unsigned char));
	memset(modP1Char, 0, 8);
	memset(modP2Char, 0, 8);

	uint32_t returnVal = 0;

	if (polyCheck(modP1) != 8) {
		fprintf(stderr, "Poly 1 is invalid. Please check the input and try again.\n");
	}
	if (polyCheck(modP2) != 8) {
		fprintf(stderr, "Poly 2 is invalid. Please check the input and try again.\n");
	}

	convertPoly(modP1, modP1Char);
	convertPoly(modP2, modP2Char);

	returnVal = checkInverse(modP1Char, modP2Char);	

	printModProd(modP1Char, modP2Char, returnVal);

}
/*
 * Step 5. Take MAX readings for 3 seconds then show results
 */
void ThrottleDetector::detectMaxCalibrate() {
	if ((millis() - startTime) < 2000 && sampleCount < maxSamples) {
		readThrottleValues();
	} else {
		displayCalibratedValues(false);

		// take some stats before normalizing min/max
		int throttle1MinFluctuation = abs(throttle1MaxRest-throttle1MinRest);
		int throttle2MinFluctuation = abs(throttle2MaxRest-throttle2MinRest);
		int throttle1MaxFluctuation = abs(throttle1Max-throttle1Min);
		int throttle2MaxFluctuation = abs(throttle2Max-throttle2Min);

		// Determine throttle type based off min/max
		if (throttle1MinRest > throttle1Min+maxThrottleReadingDeviationPercent) { // high to low pot
			throttle1HighLow = true;
		}

		if (throttle2MinRest > throttle2Min+maxThrottleReadingDeviationPercent) { // high to low pot
			throttle2HighLow = true;
		}

		// restore the true min
		throttle1Min = throttle1MinRest;

		if ((throttle1HighLow && !throttle2HighLow) || (throttle2HighLow && !throttle1HighLow)) {
			throttle2Inverse = true;
		}

		// Detect grounded pin (always zero) or floating values which indicate no potentiometer provided
		if ((throttle2MinRest == 0 && throttle2MaxRest == 0 && throttle2Min == INT16_MAX && throttle2Max == 0)
				|| (abs(throttle2MaxRest-throttle2Max) < maxThrottleReadingDeviationPercent
						&& abs(throttle2MinRest-throttle2Min) < maxThrottleReadingDeviationPercent)) {
			potentiometerCount = 1;
		} else {
			potentiometerCount = 2;
		}

		// restore the true min/max for T2
		if ( throttle2Inverse ) {
			throttle2Max = throttle2MaxRest;
		} else {
			throttle2Min = throttle2MinRest;
		}

		Logger::debug("Inverse: %s, throttle2Min: %d, throttle2Max: %d", (throttle2Inverse?"true":"false"), throttle2Min, throttle2Max);

		// fluctuation percentages - make sure not to divide by zero
		if (!(throttle1Max == throttle1Min))
		{
			throttle1MinFluctuationPercent = throttle1MinFluctuation * 100 / abs(throttle1Max - throttle1Min);
			throttle1MaxFluctuationPercent = throttle1MaxFluctuation * 100 / abs(throttle1Max - throttle1Min);
		}
		else
		{
			throttle1MinFluctuationPercent = 0;
			throttle1MaxFluctuationPercent = 0;
		}
		if (!(throttle2Max == throttle2Min))
		{
			throttle2MinFluctuationPercent = throttle2MinFluctuation * 100 / abs(throttle2Max - throttle2Min);
			throttle2MaxFluctuationPercent = throttle2MaxFluctuation * 100 / abs(throttle2Max - throttle2Min);
		}
		else
		{
			throttle2MinFluctuationPercent = 0;
			throttle2MaxFluctuationPercent = 0;
		}

		// Determine throttle subtype by examining the data sampled
		for (int i = 0; i < sampleCount; i++) {
			// normalize the values to a 0-1000 scale using the found min/max
			uint16_t value1 = normalize(throttle1Values[i], throttle1Min, throttle1Max, 0, 1000);
			uint16_t value2 = normalize(throttle2Values[i], throttle2Min, throttle2Max, 0, 1000);

			// see if they match known subtypes
			linearCount += checkLinear(value1, value2);
			inverseCount += checkInverse(value1, value2);

			//Logger::debug("T1: %d, T2: %d = NT1: %d, NT2: %d, L: %d, I: %d", throttle1Values[i], throttle2Values[i], value1, value2, linearCount, inverseCount);
		}

		throttleSubType = 0;
		if (potentiometerCount > 1) {
			// For dual pots, we trust the detection of >75%
			if ((linearCount * 100) / sampleCount > 75) {
				throttleSubType = 1;
			} else if ((inverseCount * 100) / sampleCount > 75) {
				throttleSubType = 2;
			}
		} else {
			// For single pots we use the high/low
			if (throttle1HighLow) {
				throttleSubType = 2;
			} else {
				throttleSubType = 1;
			}
		}

		char *type = "UNKNOWN";
		if (throttleSubType == 1) {
			type = "Linear";
		} else if (throttleSubType == 2) {
			type = "Inverse";
		}

		if ( Logger::isDebug()) {
			Logger::console("\n----- RAW values ----");
			for (int i = 0; i < sampleCount; i++) {
				Logger::console("T1: %d, T2: %d", throttle1Values[i], throttle2Values[i]);
			}
		}

		Logger::console("\n=======================================");
		Logger::console("Detection complete");
		Logger::console("Num samples taken: %d", sampleCount);
		Logger::console("Num potentiometers found: %d", potentiometerCount);
		Logger::console("T1: %d to %d %s", (throttle1HighLow ? throttle1Max : throttle1Min), (throttle1HighLow ? throttle1Min : throttle1Max),
				(throttle1HighLow ? "HIGH-LOW" : "LOW-HIGH"));
		Logger::console("T1: rest fluctuation %d%%, full throttle fluctuation %d%%", throttle1MinFluctuationPercent, throttle1MaxFluctuationPercent);

		if (potentiometerCount > 1) {
			Logger::console("T2: %d to %d %s %s", (throttle2HighLow ? throttle2Max : throttle2Min), (throttle2HighLow ? throttle2Min : throttle2Max),
					(throttle2HighLow ? "HIGH-LOW" : "LOW-HIGH"), (throttle2Inverse ? " (Inverse of T1)" : ""));
			Logger::console("T2: rest fluctuation %d%%, full throttle fluctuation %d%%", throttle2MinFluctuationPercent, throttle2MaxFluctuationPercent);
			Logger::console("Num linear throttle matches: %d", linearCount);
			Logger::console("Num inverse throttle matches: %d", inverseCount);
		}

		Logger::console("Throttle type: %s", type);
		Logger::console("========================================");

		// update the throttle's configuration (without storing it yet)
		config->minimumLevel1 = throttle1Min;
		config->maximumLevel1 = throttle1Max;
		config->numberPotMeters = potentiometerCount;
		if (config->numberPotMeters > 1) {
			config->minimumLevel2 = throttle2Min;
			config->maximumLevel2 = throttle2Max;
		} else {
			config->minimumLevel2 = 0;
			config->maximumLevel2 = 0;
		}
		config->throttleSubType = throttleSubType;

		// Done!
		state = DoNothing;
		TickHandler::getInstance()->detach(this);

		// send updates to ichip wifi
		DeviceManager::getInstance()->sendMessage(DEVICE_WIFI, ICHIP2128, MSG_CONFIG_CHANGE, NULL);
	}
}