mirror of https://github.com/rusefi/bldc.git
Added speed test example c lib
This commit is contained in:
parent
841b5d7613
commit
d08d57e53e
|
@ -0,0 +1,7 @@
|
|||
TARGET = example
|
||||
|
||||
SOURCES = code.c
|
||||
|
||||
VESC_C_LIB_PATH=../../
|
||||
include $(VESC_C_LIB_PATH)rules.mk
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
# Speed Test
|
||||
|
||||
C code for the speed test example in lisp. This is the corresponding code in lisp that loads the compiled binary of this example:
|
||||
|
||||
```clj
|
||||
(def example [
|
||||
0x00 0x00 0x00 0x00 0x0a 0x4b 0x0b 0x49 0x0b 0x48 0x10 0xb5 0x7b 0x44 0x07 0x4c 0x1b 0x68 0x23 0x68
|
||||
0x79 0x44 0x78 0x44 0x98 0x47 0x08 0x49 0x08 0x48 0x23 0x68 0x79 0x44 0x78 0x44 0x98 0x47 0x01 0x20
|
||||
0x10 0xbd 0x00 0xbf 0x00 0xf8 0x00 0x10 0xf0 0xff 0xff 0xff 0xc1 0x00 0x00 0x00 0x46 0x00 0x00 0x00
|
||||
0x05 0x01 0x00 0x00 0x46 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||
0x43 0x20 0x54 0x61 0x6b 0x52 0x65 0x73 0x3a 0x20 0x25 0x64 0x00 0x00 0x00 0x00 0x65 0x78 0x74 0x2d
|
||||
0x64 0x65 0x63 0x2d 0x63 0x6e 0x74 0x00 0x65 0x78 0x74 0x2d 0x74 0x61 0x6b 0x00 0x82 0xb0 0x01 0x90
|
||||
0x01 0x98 0x18 0xb1 0x01 0x98 0x01 0x38 0x02 0xb0 0xf7 0xe7 0x02 0xb0 0x70 0x47 0x30 0xb5 0x85 0xb0
|
||||
0x03 0x90 0x02 0x91 0x01 0x92 0x02 0x9a 0x03 0x9b 0x9a 0x42 0x1a 0xda 0x03 0x98 0x02 0x99 0x01 0x9a
|
||||
0x01 0x38 0xff 0xf7 0xf1 0xff 0x04 0x46 0x02 0x98 0x01 0x99 0x03 0x9a 0x01 0x38 0xff 0xf7 0xea 0xff
|
||||
0x05 0x46 0x01 0x98 0x03 0x99 0x02 0x9a 0x01 0x38 0xff 0xf7 0xe3 0xff 0x29 0x46 0x02 0x46 0x20 0x46
|
||||
0x05 0xb0 0xbd 0xe8 0x30 0x40 0xdb 0xe7 0x01 0x98 0x05 0xb0 0x30 0xbd 0x00 0x00 0x70 0xb5 0x01 0x29
|
||||
0x2d 0xed 0x02 0x8b 0x05 0x46 0x10 0x4c 0x18 0xd1 0xe3 0x6f 0x00 0x68 0x98 0x47 0xa0 0xb1 0xd4 0xf8
|
||||
0xac 0x30 0x98 0x47 0x63 0x6e 0x28 0x68 0xb0 0xee 0x40 0x8a 0x98 0x47 0xff 0xf7 0xb9 0xff 0xd4 0xf8
|
||||
0xac 0x30 0x98 0x47 0x30 0xee 0x48 0x0a 0xbd 0xec 0x02 0x8b 0xe3 0x6c 0xbd 0xe8 0x70 0x40 0x18 0x47
|
||||
0xbd 0xec 0x02 0x8b 0xd4 0xf8 0x94 0x00 0x70 0xbd 0x00 0xbf 0x00 0xf8 0x00 0x10 0x2d 0xe9 0xf0 0x41
|
||||
0x03 0x29 0x2d 0xed 0x02 0x8b 0x05 0x46 0x1e 0x4c 0x35 0xd1 0xe3 0x6f 0x00 0x68 0x98 0x47 0x00 0x28
|
||||
0x30 0xd0 0xe3 0x6f 0x68 0x68 0x98 0x47 0x60 0xb3 0xe3 0x6f 0xa8 0x68 0x98 0x47 0x40 0xb3 0xd4 0xf8
|
||||
0xac 0x30 0x98 0x47 0x63 0x6e 0x28 0x68 0xb0 0xee 0x40 0x8a 0x98 0x47 0x63 0x6e 0x06 0x46 0x68 0x68
|
||||
0x98 0x47 0x63 0x6e 0x07 0x46 0xa8 0x68 0x98 0x47 0x39 0x46 0x02 0x46 0x30 0x46 0xff 0xf7 0x86 0xff
|
||||
0xd4 0xf8 0xac 0x30 0x05 0x46 0x98 0x47 0x0b 0x48 0xd4 0xf8 0xb4 0x30 0xf0 0xee 0x40 0x8a 0x29 0x46
|
||||
0x78 0x44 0x98 0x47 0x38 0xee 0xc8 0x0a 0xbd 0xec 0x02 0x8b 0xe3 0x6c 0xbd 0xe8 0xf0 0x41 0x18 0x47
|
||||
0xbd 0xec 0x02 0x8b 0xd4 0xf8 0x94 0x00 0xbd 0xe8 0xf0 0x81 0x00 0xf8 0x00 0x10 0xbc 0xfe 0xff 0xff
|
||||
])
|
||||
|
||||
; Provides ext-dec-cnt and ext-tak which are implemented as recursive functions in C
|
||||
; like below. They take the same arguments, but return the number of seconds they took
|
||||
; to run
|
||||
(load-native-lib example)
|
||||
|
||||
(defun dec-cnt (x)
|
||||
(if (= x 0) 0 (dec-cnt (- x 1)))
|
||||
)
|
||||
|
||||
; Seems to run faster using match instead of if
|
||||
(defun dec-cnt2 (x)
|
||||
(match x (0 0) (_ (dec-cnt2 (- x 1))))
|
||||
)
|
||||
|
||||
(defun tak (x y z)
|
||||
(if (>= y x)
|
||||
z
|
||||
(tak
|
||||
(tak (- x 1) y z)
|
||||
(tak (- y 1) z x)
|
||||
(tak (- z 1) x y)
|
||||
)))
|
||||
|
||||
(print "\nTesting dec-cnt...")
|
||||
(looprange i 0 2
|
||||
(progn
|
||||
(print (str-from-n (+ i 1) "Try %d"))
|
||||
(print (str-from-n (ext-dec-cnt 100000) "C Time: %.5f s"))
|
||||
(define start (systime))
|
||||
(dec-cnt2 100000)
|
||||
(def time (secs-since start))
|
||||
(print (str-from-n time "LBM Time: %.3f s"))
|
||||
(print (str-from-n (/ time (ext-dec-cnt 100000)) "C Speed Diff: %.1f times"))
|
||||
(sleep 1)
|
||||
))
|
||||
|
||||
(print "\nTesting tak...")
|
||||
(looprange i 0 2
|
||||
(progn
|
||||
(print (str-from-n (+ i 1) "Try %d"))
|
||||
(print (str-from-n (ext-tak 18 12 6) "C Time: %.5f s"))
|
||||
(define start (systime))
|
||||
(define takres (tak 18 12 6))
|
||||
(def time (secs-since start))
|
||||
(print (str-from-n time "LBM Time: %.3f s"))
|
||||
(print (str-from-n (/ time (ext-tak 18 12 6)) "C Speed Diff: %.1f times"))
|
||||
(sleep 1)
|
||||
))
|
||||
```
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
Copyright 2022 Benjamin Vedder benjamin@vedder.se
|
||||
|
||||
This file is part of the VESC firmware.
|
||||
|
||||
The VESC firmware is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
The VESC firmware is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "vesc_c_if.h"
|
||||
|
||||
HEADER
|
||||
|
||||
static int dec_cnt(volatile int x) {
|
||||
if (x == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return dec_cnt(x - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static lbm_value ext_dec_cnt(lbm_value *args, lbm_uint argn) {
|
||||
if (argn != 1 || !VESC_IF->lbm_is_number(args[0])) {
|
||||
return VESC_IF->lbm_enc_sym_eerror;
|
||||
}
|
||||
|
||||
float t0 = VESC_IF->system_time();
|
||||
dec_cnt(VESC_IF->lbm_dec_as_i32(args[0]));
|
||||
float t1 = VESC_IF->system_time();
|
||||
|
||||
return VESC_IF->lbm_enc_float(t1 - t0);
|
||||
}
|
||||
|
||||
static int tak(volatile int x, volatile int y, volatile int z) {
|
||||
if (y >= x) {
|
||||
return z;
|
||||
} else {
|
||||
return tak(
|
||||
tak(x - 1, y, z),
|
||||
tak(y - 1, z, x),
|
||||
tak(z - 1, x, y));
|
||||
}
|
||||
}
|
||||
|
||||
static lbm_value ext_tak(lbm_value *args, lbm_uint argn) {
|
||||
if (argn != 3 ||
|
||||
!VESC_IF->lbm_is_number(args[0]) ||
|
||||
!VESC_IF->lbm_is_number(args[1]) ||
|
||||
!VESC_IF->lbm_is_number(args[2])) {
|
||||
return VESC_IF->lbm_enc_sym_eerror;
|
||||
}
|
||||
|
||||
float t0 = VESC_IF->system_time();
|
||||
int res = tak(VESC_IF->lbm_dec_as_i32(args[0]),
|
||||
VESC_IF->lbm_dec_as_i32(args[1]),
|
||||
VESC_IF->lbm_dec_as_i32(args[2]));
|
||||
float t1 = VESC_IF->system_time();
|
||||
|
||||
VESC_IF->printf("C TakRes: %d", res);
|
||||
|
||||
return VESC_IF->lbm_enc_float(t1 - t0);
|
||||
}
|
||||
|
||||
INIT_FUN(lib_info *info) {
|
||||
INIT_START
|
||||
|
||||
(void)info;
|
||||
VESC_IF->lbm_add_extension("ext-dec-cnt", ext_dec_cnt);
|
||||
VESC_IF->lbm_add_extension("ext-tak", ext_tak);
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue