Motor rotating at opposite direction, giving negative velocity and breaking velocity control

Thanks for your help. I’ve figured out what went wrong. It’s kind of silly and has almost nothing to do with the code. I have multiple AS5047P connected to the same SPI bus, but I didn’t pull down their CS pins in the circuit. This caused the other CS pins to remain floating when I initialized only some of the sensors. This messed up the communication. The sensors returned no data, and the zero electric angle was incorrectly calibrated to 6.28, which is almost 180° away from the correct value of 3.97. I changed the order of initialization. I initialized all the sensors first before initializing the motors, and then everything worked fine.