Why the motor runs at random speed?

Hello, I am trying to run the motor in sensorless mode. The logic of the code is to calibrate and accept some vel_input and run at that speed. However, it either runs at some random speed or stops at 0.

What could be the problem? I am using Odrive 3.6V (odrivetool 0.6.9) and 340kv motor.

I can see that it calibrates, but instantly it switches to another velocity and just keeps running at that speed (I have no idea where it got that speed from).

I think it could be related to 'AXIS_STATE_CLOSED_LOOP_CONTROL ’ command.

Thank you!

import odrive
import time
import math
from odrive.enums import *

odrv0 = odrive.find_any()
print(str(odrv0.vbus_voltage))

print("Erasing pre-exsisting configuration...")
try:
    odrv0.erase_configuration()
except Exception:
    pass

odrv0 = odrive.find_any()

odrv0.config.brake_resistance = 2.0  
odrv0.config.dc_max_positive_current = 20  
odrv0.config.dc_max_negative_current = -1  
odrv0.config.max_regen_current = 0  #(usually 0)
odrv0.config.dc_bus_undervoltage_trip_level = 8.0
odrv0.config.dc_bus_overvoltage_trip_level = 26  
try:
    odrv0.save_configuration()
except:
    print('Config 1 pass!')
# print('Config 1 pass!')

odrv0 = odrive.find_any()

odrv0.axis0.motor.config.motor_type = odrive.utils.MotorType.HIGH_CURRENT
odrv0.axis0.controller.config.pos_gain = 20
odrv0.axis0.controller.config.vel_gain = 0.02
odrv0.axis0.controller.config.vel_integrator_gain = 0.01
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL #odrive.utils.ControlMode.VELOCITY_CONTROL
odrv0.axis0.controller.config.vel_limit = odrv0.axis0.config.sensorless_ramp.vel / (2 * math.pi * 14)
odrv0.axis0.motor.config.current_lim = 68 
odrv0.axis0.motor.config.pole_pairs = 14
# odrv0.axis0.motor.config.direction = 1
odrv0.axis0.sensorless_estimator.config.pm_flux_linkage = 5.51328895422 / (14 * 340)  
odrv0.axis0.config.enable_sensorless_mode = True

odrv0.axis0.requested_state = odrive.utils.AxisState.MOTOR_CALIBRATION

time.sleep(10)

print(str(odrive.utils.dump_errors(odrv0)))
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL #odrive.utils.AxisState.SENSORLESS_CONTROL

time.sleep(10)

try:
    odrv0.save_configuration()
except:
    print('Config 2 pass!')

odrv0 = odrive.find_any()

print('Config saved')

odrv0.axis0.controller.input_vel = 0
while True: 
    velocity = input("Enter the velocity: ")
    odrv0.axis0.controller.input_vel = float(velocity)

What motor specifically are you using?