Tank fixes (#892)
* fixing turnration computation * updated tank computation * fix rendering glitch * restore tank computation * rounding errors
This commit is contained in:
		@@ -86,6 +86,51 @@ for (let i = 0; i < 4; i++) {
 | 
			
		||||
motors.stopAll()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Steer tester
 | 
			
		||||
 | 
			
		||||
This program lets you change the values of speed and turn ratio with the buttons.
 | 
			
		||||
 | 
			
		||||
```typescript
 | 
			
		||||
let speed = 0;
 | 
			
		||||
let turnRatio = 0;
 | 
			
		||||
 | 
			
		||||
brick.showString(`steer tester`, 1)
 | 
			
		||||
brick.showString(`connect motors BC`, 7)
 | 
			
		||||
brick.showString(`up/down for speed`, 8)
 | 
			
		||||
brick.showString(`left/right for turn ratio`, 9)
 | 
			
		||||
 | 
			
		||||
forever(function () {
 | 
			
		||||
    brick.showString(`motor B speed ${motors.largeB.speed()}%`, 4)
 | 
			
		||||
    brick.showString(`motor C speed ${motors.largeC.speed()}%`, 5)
 | 
			
		||||
    pause(100)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
function updateSteer() {
 | 
			
		||||
    motors.largeBC.steer(turnRatio, speed);
 | 
			
		||||
    brick.showString(`speed ${speed}%`, 2)
 | 
			
		||||
    brick.showString(`turnRatio ${turnRatio}`, 3)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    speed += 10
 | 
			
		||||
    updateSteer()
 | 
			
		||||
})
 | 
			
		||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    speed -= 10
 | 
			
		||||
    updateSteer()
 | 
			
		||||
})
 | 
			
		||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    turnRatio -= 10
 | 
			
		||||
    updateSteer()
 | 
			
		||||
})
 | 
			
		||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    turnRatio += 10
 | 
			
		||||
    updateSteer()
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
updateSteer()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See also
 | 
			
		||||
 | 
			
		||||
[tank](/reference/motors/synced/tank), [run](/reference/motors/motor/run)
 | 
			
		||||
@@ -82,6 +82,51 @@ pause(5000)
 | 
			
		||||
motors.stopAll()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Tank tester
 | 
			
		||||
 | 
			
		||||
This program lets you change the tank values using the brick buttons
 | 
			
		||||
 | 
			
		||||
```typescript
 | 
			
		||||
let tankB = 0;
 | 
			
		||||
let tankC = 0;
 | 
			
		||||
 | 
			
		||||
brick.showString(`tank tester`, 1)
 | 
			
		||||
brick.showString(`connect motors BC`, 7)
 | 
			
		||||
brick.showString(`up/down for tank B`, 8)
 | 
			
		||||
brick.showString(`left/right for tank C`, 9)
 | 
			
		||||
 | 
			
		||||
forever(function () {
 | 
			
		||||
    brick.showString(`motor B speed ${motors.largeB.speed()}%`, 4)
 | 
			
		||||
    brick.showString(`motor C speed ${motors.largeC.speed()}%`, 5)
 | 
			
		||||
    pause(100)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
function updateTank() {
 | 
			
		||||
    brick.showString(`tank A: ${tankB}%`, 2)
 | 
			
		||||
    brick.showString(`tank B: ${tankC}%`, 3)
 | 
			
		||||
    motors.largeBC.tank(tankB, tankC);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    tankB += 10
 | 
			
		||||
    updateTank();
 | 
			
		||||
})
 | 
			
		||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    tankB -= 10
 | 
			
		||||
    updateTank();
 | 
			
		||||
})
 | 
			
		||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    tankC += 10
 | 
			
		||||
    updateTank();
 | 
			
		||||
})
 | 
			
		||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    tankC -= 10
 | 
			
		||||
    updateTank();
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
updateTank();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See also
 | 
			
		||||
 | 
			
		||||
[steer](/reference/motors/synced/steer), [run](/reference/motors/motor/run)
 | 
			
		||||
@@ -129,7 +129,7 @@ namespace sensors.internal {
 | 
			
		||||
 | 
			
		||||
    let nonActivated = 0;
 | 
			
		||||
    function detectDevices() {
 | 
			
		||||
        control.dmesg(`detect devices (${nonActivated} na)`)
 | 
			
		||||
        //control.dmesg(`detect devices (${nonActivated} na)`)
 | 
			
		||||
        const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS)
 | 
			
		||||
        let numChanged = 0;
 | 
			
		||||
        const uartSensors: SensorInfo[] = [];
 | 
			
		||||
@@ -198,7 +198,7 @@ namespace sensors.internal {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        control.dmesg(`detect devices done`)
 | 
			
		||||
        //control.dmesg(`detect devices done`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    export class Sensor extends control.Component {
 | 
			
		||||
 
 | 
			
		||||
@@ -339,7 +339,7 @@ namespace motors {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected setOutputType(large: boolean) {
 | 
			
		||||
           for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
 | 
			
		||||
            for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
 | 
			
		||||
                if (this._port & (1 << i)) {
 | 
			
		||||
                    // (0x07: Large motor, Medium motor = 0x08)
 | 
			
		||||
                    MotorBase.output_types[i] = large ? 0x07 : 0x08;
 | 
			
		||||
@@ -561,10 +561,12 @@ namespace motors {
 | 
			
		||||
            speedRight = Math.clamp(-100, 100, speedRight >> 0);
 | 
			
		||||
 | 
			
		||||
            const speed = Math.abs(speedLeft) > Math.abs(speedRight) ? speedLeft : speedRight;
 | 
			
		||||
            const turnRatio = speedLeft == speed
 | 
			
		||||
                ? (100 - speedRight / speedLeft * 100)
 | 
			
		||||
                : (speedLeft / speedRight * 100 - 100);
 | 
			
		||||
            let turnRatio = speedLeft == speed
 | 
			
		||||
                ? speedLeft == 0 ? 0 : (100 - speedRight / speedLeft * 100)
 | 
			
		||||
                : speedRight == 0 ? 0 : (speedLeft / speedRight * 100 - 100);
 | 
			
		||||
            turnRatio = Math.floor(turnRatio);
 | 
			
		||||
 | 
			
		||||
            //control.dmesg(`tank ${speedLeft} ${speedRight} => ${turnRatio} ${speed}`)
 | 
			
		||||
            this.steer(turnRatio, speed, value, unit);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -63,7 +63,8 @@ namespace pxsim {
 | 
			
		||||
            delete this.speedCmd;
 | 
			
		||||
            delete this.speedCmdValues;
 | 
			
		||||
            delete this._synchedMotor;
 | 
			
		||||
        }
 | 
			
		||||
            this.setChangedState();
 | 
			
		||||
        }        
 | 
			
		||||
 | 
			
		||||
        clearSyncCmd() {
 | 
			
		||||
            if (this._synchedMotor)
 | 
			
		||||
@@ -184,11 +185,13 @@ namespace pxsim {
 | 
			
		||||
                    case DAL.opOutputStepSync:
 | 
			
		||||
                    case DAL.opOutputTimeSync: {
 | 
			
		||||
                        const otherMotor = this._synchedMotor;
 | 
			
		||||
                        if (otherMotor.port < this.port) // handled in other motor code
 | 
			
		||||
                            break;
 | 
			
		||||
 | 
			
		||||
                        const speed = this.speedCmdValues[0];
 | 
			
		||||
                        const turnRatio = this.speedCmdValues[1];
 | 
			
		||||
                        // if turnratio is negative, right motor at power level
 | 
			
		||||
                        // right motor -> this.port > otherMotor.port
 | 
			
		||||
                        if (Math.sign(this.port - otherMotor.port)
 | 
			
		||||
                            == Math.sign(turnRatio))
 | 
			
		||||
                            break; // handled in other motor code
 | 
			
		||||
                        const stepsOrTime = this.speedCmdValues[2];
 | 
			
		||||
                        const brake = this.speedCmdValues[3];
 | 
			
		||||
                        const dstep = this.speedCmd == DAL.opOutputTimeSync
 | 
			
		||||
@@ -204,12 +207,7 @@ namespace pxsim {
 | 
			
		||||
 | 
			
		||||
                        // turn ratio is a bit weird to interpret
 | 
			
		||||
                        // see https://communities.theiet.org/blogs/698/1706
 | 
			
		||||
                        if (turnRatio < 0) {
 | 
			
		||||
                            otherMotor.speed = speed;
 | 
			
		||||
                            this.speed *= (100 + turnRatio) / 100;
 | 
			
		||||
                        } else {
 | 
			
		||||
                            otherMotor.speed = this.speed * (100 - turnRatio) / 100;
 | 
			
		||||
                        }
 | 
			
		||||
                        otherMotor.speed = this.speed * (100 - Math.abs(turnRatio)) / 100;
 | 
			
		||||
 | 
			
		||||
                        // clamp
 | 
			
		||||
                        this.speed = Math.max(-100, Math.min(100, this.speed >> 0));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user