Motor simulation -- how to deal with brake mode?

Using The Motor Characterization Paper, I’ve got this for computing motor acceleration/velocity:



    def compute(self, motor_pct, tm_diff):
        
        motor_pct = max(min(1, motor_pct), -1)
        appliedVoltage = self.config.nominalVoltage * motor_pct
        appliedVoltage = math.copysign(max(abs(appliedVoltage) - self._vintercept, 0), appliedVoltage)
        
        self.acceleration = (appliedVoltage - (self._kv * self.velocity)) / self._ka
        self.velocity += self.acceleration * tm_diff
        self.torque = self.acceleration * self._mass


So far so good… the result looks and feels pretty ‘real’ now. However, it occurred to me that I need to deal with brake mode. What’s the best way to hack that into this?

One way that snobotsim uses is just detect a voltage of 0… and then set acceleration/velocity to 0 and calls it a day. I feel there’s probably a slightly more realistic way to compute this, right? However, I’m not quite sure of the best way to do it.

Thanks!

No hack needed.

Brake mode is a short across the motor leads, and shorts are by definition 0 volts. So, just set applied voltage to 0, you’ll get a big negative acceleration and you’ll see the motor stop.

Coast mode is the harder one, because you need to add some friction term or drag term to model that. That would require a “hack”. It’ll be a physics based sum-of-forces “hack”, but from a coding perspective you would need extra terms and logic.

Can confirm what you have now is brake mode.

Wouldn’t coast mode be equivalent except with kv=0? Because coast mode is only activated when neutral (when applied voltage is 0). So the only thing slowing down the system is friction.

That doesn’t seem right. Here’s a graph of the velocity output of the current model when applying 100% for 1 second and 0% for another second.

Imgur

That seems an awful lot like a coast mode instead of brake mode.

For instance, the documentation for the Victor SPX mentions that the shorting creates an additional back emf force… so, shouldn’t that additional force be accounted for somehow?

Its definitely not perfect, but I think it does a good enough job to get the point across and help with the gravity based simulations. I think it causes a more immediate stoppage than what is realistic, but when you consider that the nuances that aren’t accounted for in simplistic simulation, its “close enough and sound alike” enough for me.

This is correct. What you have now is almost certainly brake mode; when applied voltage is zero and velocity is positive, the total voltage across the motor is negative, causing a torque that opposes the motion.

*When the motor leads are shorted together and velocity is positive, the voltage across the motor is zero but the current through the motor is negative, causing a torque that opposes the motion.
*
*

The back-EMF from the motor results in a situation precisely analogous to a negative voltage applied to a stationary motor; one can think of the circuit usefully as if the back EMF were a voltage source, with the motor coils acting as a resistor. I find this is a more-intuitive way to conceptualize the situation.

I suppose this might be idiosyncratic, though; the net voltage across the motor leads is obviously, indeed, zero, as they’re shorted together.

[strike]I don’t want to get into a flame war here, but you’re moving the goalposts: your previous post was not stated as an analogy and did not involve a stationary motor.[/strike]

I’m sure you understand this, Eli, but for the sake of students who are trying to understand this: When the motor leads are shorted, the voltage across the motor is zero, but the current through the motor is not zero.

That’s because the current flowing through the motor creates a voltage drop across the motor resistance that cancels the voltage generated by the spinning motor.

Here’s a simple resistance circuit analogy:

Take a small battery (like a AAA size carbon cell) and short the leads together with a thick copper wire (do NOT do this with an FRC battery!!). The voltage across the battery is (essentially) zero, but the current through the battery is most certainly not zero: the current through the battery creates a voltage drop across the battery internal resistance that cancels the battery galvanic voltage.

**EDIT:

I just saw you edited your post, but I’ll leave the above for the benefit of other readers.**