; VESC Motor Application 0.0.2
; *************************************************
; Parameters & Definitions
; *************************************************
; ON/OFF Status for Motor Lock
(define ON 1)
(define OFF 0)
; Loop frequency in Hz adjust for Motor Lock responsiveness
(define hz 1000)
; Determines if Motor Locks on bootup
(define isBraking 1)
; Minimum RPM that the motor must be at to initiate Motor Lock
(define minRPM 200)
; RPM that is too high for Motor Lock to engage
(define RPM 300)
; Current RPM
(define curRPM (abs(get-rpm)))
; Motor Lock Current in (A)
(define brakeVal 20)
; Displays PPM variable in Terminal
(define PPM(get-PPM))
; Input Range For Handbrake to engage
; inputTop is the top of the range, inputButtom is the bottom of the range
; Input Value Which AT/BELOW To Engage Handbrake (CHANGE)
; Negative Value Means Handbrake Will NEVER Engage
(define inputTop 0.1)
; Input Value Which AT/ABOVE To Engage Handbrake (CHANGE)
; Negative Value Means Handbrake Will Engage At Any Value Less Than inputTop
(define inputBottom -0.1)
;---------------------------------------------------
; Motor LOCK Function
;---------------------------------------------------
(defun motor_lock()
(progn
; Function for if Motor is UNLOCKED
(if (eq isBraking OFF)
(progn
(app-disable-output -1)
(define isBraking ON)
)
)
; Function for if Motor is LOCKED
(if (> curRPM minRPM)
(set-handbrake brakeVal)
)
)
)
;---------------------------------------------------
; Motor UNLOCK Function
;---------------------------------------------------
(defun motor_unlock()
(progn
(if (eq isBraking ON)
(progn
(set-handbrake 0)
(app-disable-output 0)
(define isBraking OFF)
)
)
)
)
; *************************************************
; Main Function
; *************************************************
(defun main()
(progn
; Definitions that need to be active within loop
(define PPM (get-ppm))
(define RPM (get-rpm))
(define Duty_Cycle (get-duty))
(define Velocity (get-speed))
; Function for determining when to LOCK/UNLOCK motor
(if (and (< PPM inputTop) (> PPM inputBottom))
(if (or (eq isBraking 1) (< (abs(get-rpm)) RPM))
(motor_lock)
)
(if (eq isBraking 1)
(motor_unlock)
)
)
; Loop/Loop Rate
(yield (/ 1000000 hz))
(main)
)
)
(motor_unlock)
(main)