View Single Post
Old 12-31-2016, 03:31 PM   #2920 (permalink)
motorulf
EcoModding Lurker
 
motorulf's Avatar
 
Join Date: Dec 2016
Location: northern sweden
Posts: 33
Thanks: 17
Thanked 20 Times in 17 Posts
Ok, now i have been hacking away at Pauls code some more.
Now actually think this would work.
As the code is now, it uses maximum regen as maximum reverse. Not very elegant, i know, this will be a separate variable in the future. I thought it would be enough to test it out anyway.

Test it at your own risk, be shure that you understand what this will do before trying it. I havent tested this myself yet, there are probably some errors, both in my thinking and my code... you have been warned.

I marked the code with *'s where i have been touching it.
Code:
		// Is there a throttle fault?
		if (throttle <= savedValues.throttleFaultPosition) {
			faultBits |= THROTTLE_FAULT;
		}
		if (savedValues2.throttleType == 1) {
			throttle = 1023 - throttle;  // invert it.
		}

		// I think I'll work my way left to right.  ThrottleFaultVoltage < ThrottleMaxRegen < ThrottleMinRegen < ThrottleMin < ThrottleMax 
		if (throttle < savedValues.maxRegenPosition) {  //First clamp it below.
			throttle = savedValues.maxRegenPosition;	
		}
		else if (throttle > savedValues.maxThrottlePosition) {	// And clamp it above!  
			throttle = savedValues.maxThrottlePosition;
		}
		if (throttle < savedValues.minRegenPosition) {  // It's in regen territory.  Map it from [savedValues.maxThrottleRegen, savedValues.minThrottleRegen) to [-maxMotorCurrentNormalizedRegen, 0)
			throttle -= savedValues.minRegenPosition;  // now it's in the range [maxThrottleRegen - minThrottleRegen, 0)

            // Do this if we are reversing *******************************
            if (reversing = 1){  //Dont invert it, as regen.brake now will mean a forward rotating force *******************************
                throttle =	__builtin_divsd(((long)throttle) * maxMotorCurrentNormalizedRegen, savedValues.maxRegenPosition - savedValues.minRegenPosition); //*******************************
            }
            else{      //Do what you did before *******************************
                throttle =	-__builtin_divsd(((long)throttle) * maxMotorCurrentNormalizedRegen, savedValues.maxRegenPosition - savedValues.minRegenPosition); //*******************************
            }
		}
		else if (throttle <= savedValues.minThrottlePosition) { // in the dead zone!
			throttle = 0;
		}
		else { // <= throttle max is the only other option!  Map the throttle from (savedValues.minThrottlePosition, savedValues.maxThrottlePosition] to (0,maxMotorCurrentNormalized]
			throttle -= savedValues.minThrottlePosition;

            if (reversing = 1){ //Invert the signal and map it so max regen is maximum reverse. This needs its own value defined in code *******************************
                throttle = -__builtin_divsd(__builtin_mulss(throttle, maxMotorCurrentNormalizedRegen), savedValues.maxThrottlePosition - savedValues.minThrottlePosition); //*******************************
            }
            else{ //Hands off!  dont touch Pauls code   *******************************
                throttle = __builtin_divsd(__builtin_mulss(throttle, maxMotorCurrentNormalized), savedValues.maxThrottlePosition - savedValues.minThrottlePosition); //*******************************
            }
		}
		if (temperatureBasePlate > THERMAL_CUTBACK_START) {  // Force the throttle to cut back.
			temperatureMultiplier = (temperatureBasePlate - THERMAL_CUTBACK_START) >> 3;  // 0 THROUGH 7.
			if (temperatureMultiplier >= 7)
				temperatureMultiplier = 0;
			else {
				// temperatureMultiplier is now 6 to 0 (for 1/8 to 7/8 current)
				temperatureMultiplier = 7 - temperatureMultiplier;
				// temperatureMultiplier is now 1 for 1/8, 2 for 2/8, ..., 7 for 7/8, etc.
			}
		}
		else {
			temperatureMultiplier = 8;	// Allow full throttle.
		}
		currentRadiusRefRef = __builtin_mulss(throttle,temperatureMultiplier) >> 3;  // scale back the current command based on temperature.   *******************************
		if (RPS_times16 < 8) {  // if less than 0.5 rev per second, make sure there's no regen. Also allow changing into reverse  *******************************
			if (currentRadiusRefRef < 0) currentRadiusRefRef = 0;
                if (I_PORT_REGEN_THROTTLE == 0) {   //check input for reverse switch, This input is pin 27 (or pin 10 if you use the 40-Pin PDIP verion)   *******************************
                    reversing = 1;   //This guy is not defined yet, define him already   *******************************
                {                
                else{
                    reversing = 0;   //*******************************
                }
            }
		}
        if (reversing = 0){    //We are moving forward, keep up the good work   //*******************************
            if (currentRadiusRefRef < 0) {
                currentRadiusRefRef = -currentRadiusRefRef;
                sign = -1;
            }
            else {
                sign = 1;
            }
        }
        if (reversing = 1){   // Now we wanna go backwards, only give it power while rotating backwards.   //*******************************
            if (currentRadiusRefRef > 0) {
                currentRadiusRefRef = -currentRadiusRefRef;
                sign = -1;
            }
            else {
                sign = 1;
            }
        }

Last edited by motorulf; 12-31-2016 at 03:46 PM.. Reason: usage warning
  Reply With Quote
The Following User Says Thank You to motorulf For This Useful Post:
MPaulHolmes (01-01-2017)