Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Thruster Latency with respect to Sensors

Discussion in 'Programming Questions and Suggestions' started by Aura, Aug 27, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Aura Trainee Engineer

    Messages:
    7
    Hey guys, I'm trying to make anticollision for my autopilot, however, I want to try and keep lag down to a minimum so I decided I'd try and use a single sensor.

    How it works, is every second I iterate through every direction of the sensor to test direction, then if something is detected it fires the thrusters on that side of the ship. However, even though everything is detecting properly, the thrusters aren't firing in time. If I put an error in the code at the section the thruster is going to fire, it will activate properly, so my theory is that I'm simply sending commands too fast.

    This is the anticollision function:
    Code:
    
    void AntiCollision( ){
    	float left = (sensorStep >= 0 & sensorStep <= 11 ? 1f : 0f);
    	float top = (sensorStep >=12 & sensorStep <= 23 ? 1f : 0f);
    	float right = (sensorStep >= 24 & sensorStep <= 35 ? 1f : 0f);
    	float bottom = (sensorStep >= 36 & sensorStep <= 47 ? 1f : 0f);
    	float outward = ((sensorStep >= 48 & sensorStep <= 60) ? 1f : 0f);
    	
    	if ( !disableAntiCollision ){
    		for (int i = 0; i < sensorArray.Count; i++){
    					
    			sensorArray[i].SetValueFloat("Left",sensorRange * left);
    			sensorArray[i].SetValueFloat("Top",sensorRange * top);
    			sensorArray[i].SetValueFloat("Right",sensorRange * right);
    			sensorArray[i].SetValueFloat("Bottom",sensorRange * bottom);
    			sensorArray[i].SetValueFloat("Front",MathHelper.Max(sensorRange * outward,30));//Give this a minimum so we actually can react to shit in time
    			var detectedEnt = sensorArray[i].LastDetectedEntity;
    			if ( detectedEnt == null && sensorRange < 50 ){ sensorRange++; }else{ sensorRange-= 10; }
    			if ( detectedEnt != null ){
    				if ( typeEntityDetected(detectedEnt.ToString())==ENTITY_TYPE_ASTEROID ){
    				asteroidDetected = true; 
    				}}//This works!
    			
    			if ( sensorStep <= 60 ){ 
    				if ( sensorStep >= 0 & sensorStep <= 11 & detectedEnt != null){ //This WORKS BUT BARELY LOL GOOD ENOUGH FOR THE GIRLS I SLEEP WITH ( :C )
    				//The thrusters also dont seem to react fast enough for this sensor strategy.
    					for (int x = 0; x < rightThrusters.Count; x++){
    						rightThrusters[x].GetActionWithName("OnOff_On").Apply(rightThrusters[x]); 
    						rightThrusters[x].SetValue<float>("Override", rightThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						
    					//	for (int y = 0; y < rearThrusters.Count; y++){rearThrusters[y].SetValue<float>("Override", 0f);}
    						
    				}else if( sensorStep >=12 & sensorStep <= 23 & detectedEnt != null){
    					for (int x = 0; x < downThrusters.Count; x++){
    						downThrusters[x].GetActionWithName("OnOff_On").Apply(downThrusters[x]); 
    						downThrusters[x].SetValue<float>("Override", downThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						
    						for (int y = 0; y < rightThrusters.Count; y++){rightThrusters[y].SetValue<float>("Override", 0f);}
    						
    				}else if( sensorStep >= 24 & sensorStep <= 35 & detectedEnt != null){
    					for (int x = 0; x < leftThrusters.Count; x++){
    						leftThrusters[x].GetActionWithName("OnOff_On").Apply(leftThrusters[x]); 
    						leftThrusters[x].SetValue<float>("Override", leftThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						
    						for (int y = 0; y < downThrusters.Count; y++){downThrusters[y].SetValue<float>("Override", 0f);}
    						
    				}else if( sensorStep >= 36 & sensorStep <= 47 & detectedEnt != null){
    					for (int x = 0; x < upThrusters.Count; x++){
    						upThrusters[x].GetActionWithName("OnOff_On").Apply(upThrusters[x]); 
    						upThrusters[x].SetValue<float>("Override", upThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						
    						for (int y = 0; y < leftThrusters.Count; y++){leftThrusters[y].SetValue<float>("Override", 0f);}
    						
    				}else if( sensorStep >= 48 & sensorStep <= 60 & detectedEnt != null){
    						//	if ( sensorArray[i] == frontSensor & !isNavigating){
    						//	for (int x = 0; x < rearThrusters.Count; x++){
    						//		rearThrusters[x].GetActionWithName("OnOff_On").Apply(rearThrusters[x]); 
    						//		rearThrusters[x].SetValue<float>("Override", rearThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						//}else if (!isNavigating){
    						//	for (int x = 0; x < forwardThrusters.Count; x++){
    						//		forwardThrusters[x].GetActionWithName("OnOff_On").Apply(forwardThrusters[x]); 
    						//		forwardThrusters[x].SetValue<float>("Override", forwardThrusters[x].GetMaximum<float>("Override") * (sensorRange / 50));}
    						//}
    						for (int y = 0; y < upThrusters.Count; y++){upThrusters[y].SetValue<float>("Override", 0f);}
    				}		//The shit above is commented out because it interferes with navigation and WHY ARE WE KAMIKAZE-ING INTO THINGS JESUS FUCK WHO IS GIVING US COMMANDS
    				sensorStep++;
    				
    				} else { sensorStep = 0; }	
    		}	
    	}
    }
    
    Freshly copy pasted.

    The sensor steps are designed with a constantly triggering timer block in mind, which to my knowledge is 60 ticks per second, which gives me an effective scan rate of 1Hz, and a 1/6th second time between when the thruster should be on, and then off again. The aim is to give lots of correction when scanning a large radius, while giving smaller corrections closer in, which given a perfect update rate should allow me to become positioned in the center of any enclosed area.

    If you have any suggestions or possibly know the exact frequency I can update block statuses, please let me know.
     
  2. Sinbad Senior Engineer

    Messages:
    2,788
    as far as I can tell changing the sensor ranges takes at least 3 updates before they are available to be read again. I use one in 4 ticks on my setups to get around it. I'm don't know if there is a latency in thrusters, but text panels can be changed and read as many times in an pb run as you like, so they at least have no latency.
     
  3. Wicorel Senior Engineer

    Messages:
    1,242
    There is definitely a lag for sensor updating. It's at least one tick.

    I have the sensors set to trigger my PB on activate and triggernow the timer block on deactivate so I get called as soon as the sensor changes.

    I don't use variable sensors in my miner. the 'front' is set to max range to allow stopping on detection.

    For other direction, I have shorter range sensors (fixed) (about ship space), I limit the speed to allow for stopping in that distance.

    When the sensors 'hit' something, the PB gets almost immediately (next tick?) called, so response is pretty fast.

    Also, sensor keep the lastdetected for longer than is correct, I think.


    About your code: how many sensors do you have in your sensor array?

    Maybe one sensor is turning the thrusters on and the next is turning them off... resulting in off...
     
  4. Aura Trainee Engineer

    Messages:
    7
    Currently there is one sensor in the array, stationed at the front center of the ship. The script was made with using two sensors in mind (forward and back) however I found that there was rarely a need for rear sensing so I cut it down.
     
Thread Status:
This last post in this thread was made more than 31 days old.