You are here

Lisp scripting questions/suggestions/remarks

1 post / 0 new
Gatze
Offline
Last seen: 3 days 2 hours ago
VESC FreeVESC Silver
Joined: 2018-02-03 18:06
Posts: 52
Lisp scripting questions/suggestions/remarks

On previous versions of the Vesc firmware I implemented a cruisecontrol using a single actuator (thumb throttle) connected to adc 0. Not a big application, but it works nice. While on my bike I don't have to hold the thumb throttle all the time and can lay down on my triathlon steer. Of course safety is a big issue, but my brakes are always stronger then my motor and every little tap on the thumb throttle deactivate the cruise control. I used it for now like 10k kilometers without any hazardous situations. I would not recommend this on high power motors for safety reasons.

anyway code and explanation can be found here:

https://github.com/GatzeTech/VESC_Cruisecontrol

https://youtu.be/lclRbZko9Us?t=489

Because I couldn't succeed with compiling with the latest version (bin file to big) I thought I will give the Lisp scripting possibility a try. So I translated my C code one on one to Lisp. I don't know if a one-on-one translation is smart to do, because probably in Lisp you can do things smarter (or not) I don't know. If there are any suggestion please let me know. For example is there a better way to do a switch state ?

First I couldn't find the lisp scripting in the Vesc tool, found the right VESC Tool in the downloads in 'vesc_tool_BETA_ALL.zip' also the silent HFI is integrated in this version, also want to integrate.

I must say I don't find Lisp very straightforward (would have preferred like micro Python), it is less readable, but overall it went quite well. And the most important thing that there is a possibility for scripting. Also very handy is that you can see directly the value's of variables (bindings).

Some questions:

Is it possible to make a little GUI with QML and set some variables in LISP and save it to the eeprom ? And if so is there a tutorial for how to 'connect' between QML and Lisp variable's and functions ?

So this is my Lisp code:

(define pwr 0) ;0-1 used for setting the dutycycle
(define pwrtmp 0) ;0-1 thumbthrottle value 
(define state 0) 
(define timer-throttle 0)
(define pwr-highest 0)

;Fixed variables
(define timer-throttle-return 20)
(define border-throttle  0.3)

(loopwhile t
    (progn 
        (setvar 'pwr (get-adc-decoded 0)) 
        (setvar 'pwrtmp pwr) 
        
        ;if power is > 0 then go to state 1
        (if (= state 0)
            (progn 
                (if (> pwrtmp 0) 
                    (setvar 'state 1)
                )
                (setvar 'pwr 0)               
            )
        )
        ;if power is 0 again go to state 2
        (if (= state 1)
            (progn
                (setvar 'pwr pwrtmp)                
                (if (= pwrtmp 0) 
                    (progn
                        (setvar 'state 2)
                        (setvar 'timer-throttle 0)                       
                    )
                )                                
            )
        )
        ;if within 0.2 seconds (timer-throttle-return) pwr is bigger than 0 go to state 3
        (if (= state 2)
            (progn                
                (setvar 'timer-throttle (+ timer-throttle 1))               
                (if (> timer-throttle timer-throttle-return) 
                    (setvar 'state 0)                                                                 
                )  
                 
                (if (> pwrtmp 0) 
                    (progn
                        (setvar 'state 3)
                        (setvar 'pwr-highest 0)                       
                    )
                )                                                                                                                                                                                                                                                                                                                                                                 
            )
        )
        ;Keep track of the highest power in pwr-highest 
        ;if pwr-highest is < border-throttle then pwrtmp is the dutycylce (pwr) to send
        ;if pwr-highest is > border-throttle then pwr-highest is the dutycycle (pwr) to send = cruisecontrol
        ;if thumbthrottle = 0  again (pwrtmp) go to state 4 if pwr-highest > border-throttle or to state 0 when pwr-highest < border-throttle
        (if (= state 3)
            (progn                
                (if (> pwrtmp pwr-highest) 
                    (setvar 'pwr-highest pwrtmp)                                                                 
                ) 
                 
                (if (< pwr-highest border-throttle) 
                    (setvar 'pwr pwrtmp)
                    (if (>= pwr-highest border-throttle) ;else if
                        (setvar 'pwr pwr-highest)                         
                    )
                )               
                                                                                                                                                                                                                                                                                                                                                                                               
                (if (= pwrtmp 0)
                    (if (>= pwr-highest border-throttle)
                        (setvar 'state 4)
                        (if (< pwr-highest border-throttle) ;else if
                            (setvar 'state 0)
                        )
                    )
                )                
            )
        )
        ;cruise control in action, dutycycle (pwr) is pwr-highest until 
        ;the thumbthrottle is touched again (pwrtmp > 0)
        (if (= state 4) 
            (progn
                (setvar 'pwr pwr-highest)
                (if (> pwrtmp 0)
                    (setvar 'state 5)
                )
            )                                                    
        )
        (if (= state 5) 
            (if (= pwrtmp 0)
                (setvar 'state 0)
            )
        )
                        
        (set-duty pwr)
        
        ;100 hz
        (sleep 0.01)      
    )
)