Gremsy H16 gimbal
Python Class that works as the driver for the gimbal Gremsy H16.
The gimbal should be connected to the host computer through an TTL2USB connection (check "Manual A2GMeasurements").
It creates a thread (called here a gimbal thread) to handle the communication between the gimbal and this host computer.
Gimbal's rotational speed (both in yaw -azimuth-, and pitch -elevation-) depends on a value between [-100, 100] as this is the natural range of values in most Remote Controllers (i.e. FrSky X8R).
Gmbal's rotational speed dependence on the RC controlling interval is neither linear, nor symmetrical: 1. Non-linear: a change from 10 to 20 is not equivalent to a change from 20 to 30 2. Non-symmetrical: a change from 10 to 20 is not equivalent to a change from -10 to -20. A change from 10 to 20 (or -10 to -20) in yaw is not equivalent to a change from 10 to 20 (or -10 to -20) in pitch.
Gimbal's angle (either yaw or pitch) depends on: 1) the RC controlling interval and 2) the time the given RC control value is hold. This dependence is measured as described in "Manual A2GMeasurements".
Gimbal's angle can only be controlled by using the RC control value and the time the serial SBUS will hold that control value.
This class relies on heavily on the SBUSEncoder class, as that is the class decoding the sbus protocol from Gremsy.
This class is meant to be equivalent to GimbalRS2. Mainly by implementing the setPosControl for this gimbal.
Source code in a2gmeasurements.py
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 | |
__init__(speed_time_azimuth_table=None, speed_time_elevation_table=None)
Constructor for the class.
Loads the measured angle (yaw and pitch, each one separately) dependence on RC control value and time. Replace load_measured_data_august_2023 or reimplement it if newer/better measurements of this dependence are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
speed_time_azimuth_table |
ndarray
|
array with whose 3 columns are: 1. RC control value (equivalent to speed). 2. Time holding the RC value. 3. Measured azimuth (yaw). Defaults to None. |
None
|
speed_time_elevation_table |
ndarray
|
array with whose 3 columns are: 1. RC control value (equivalent to speed). 2. Time holding the RC value. 3. Measured elevation (pitch). Defaults to None. |
None
|
Source code in a2gmeasurements.py
change_gimbal_mode(mode='LOCK')
Changes Gremsy gimbal mode. Available modes are: 'Lock' and 'Follow' (and motors off). Brief description of the modes is on the manual of the gimbal provided by the manufacturer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode |
str
|
either |
'LOCK'
|
Source code in a2gmeasurements.py
control_power_motors(power='on')
Turns gremsy gimbal motors on or off. This function is a wrapper of turn_on_motors an turn_off_motors of the SBUSEncoder class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power |
str
|
|
'on'
|
Source code in a2gmeasurements.py
define_home_position()
Defines the center of the coordinated system (yaw, pitch). To do so, it checks if the gimbal is moving (in both azimuth and elevation) and if not, read the angles provided by the IMU and set them as the home position. Due to this operation, this function must be called in __init__ or before any setPosControl call.
As the gimbal controller from the manufacturer can't be accesed, imu readings are not available. An external IMU is required and any cheap raspberry pi pico is capable of providing decent IMU support. Potential magnetic interferences between the IMU readings of the raspberry pi pico and the motors of the gimbal have not been researched.
NOTE: this function requires to be further tested, if the Gremsy gimbal is to be used again as part of the channel sounder system. The reason is that when checking if the gimbal is moving, a good tolerated error (tol_err = abs(angle_now - angle_before)) must be set.
Source code in a2gmeasurements.py
fit_model_to_gimbal_angular_data(model='linear')
Fits a model to the measured angle (yaw, pitch) dependence on time and speed (RC control value). There are two models: a linear model and a gaussian regressor.
The linear model is suitable specific range of "speeds" and time, since the non-linear dependence can be linearized. However, this range must be defined.
For the gaussian regressor, more training samples (RC control value, time, angle) over the full range are required to avoid overfitting and bad predicting behaviour.
There is either a linear or gp model for the RC control positive values and another one for the RC control negative values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
str
|
either |
'linear'
|
Source code in a2gmeasurements.py
gremsy_angle(a_i, a_ip, b_i)
Computes the angle between the sides of the triangle given by a_i and a_i+1. The opposite side to the angle computed is the one defined by b_i.
A definition of this distances can be found in "Manual A2GMeasurements".
NOTE FOR DEVELOPERS: the experiment described in * "Manual A2GMeasurements" was done before acquiring the external IMU. With the use of the external IMU a much easier measurement of the yaw and pitch can be done using (and extending) the receive_imu_data function of this class*.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_i |
float
|
defined in "Manual A2GMeasurements". |
required |
a_ip |
float
|
defined in "Manual A2GMeasurements". |
required |
b_i |
float
|
defined in "Manual A2GMeasurements". |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Radians |
float
|
mentioned angle in radians. |
Source code in a2gmeasurements.py
load_measured_data_august_2023()
Loads a second set of measured data extracted from the experiment described on "Manual A2GMeasurements".
This table contains as columns the speed [-100, 100], time [s], and the angle (azimuth, elevation) computed from the 3 distances (a_{i}, a_{i+1}, b_{i}) described in "Manual A2GMeasurements".
NOTE FOR DEVELOPERS: the experiment described in "Manual A2GMeasurements" was done before acquiring the external IMU. With the use of the external IMU a much easier measurement of the yaw and pitch can be done using (and extending) the receive_imu_data function of this class.
Source code in a2gmeasurements.py
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 | |
load_measured_data_july_2023()
Loads a set of measured data extracted from the experiment described on "Manual A2GMeasurements".
This table contains as columns the speed [-100, 100], time [s], and the azimuth angle computed from the 3 distances (a_{i}, a_{i+1}, b_{i}) described in "Manual A2GMeasurements".
NOTE FOR DEVELOPERS: the experiment described in "Manual A2GMeasurements" was done before acquiring the external IMU. With the use of the external IMU a much easier measurement of the yaw and pitch can be done using (and extending) the receive_imu_data function of this class.
Source code in a2gmeasurements.py
load_measured_drifts()
Loads measured drift angles from the experiment described on "Manual A2GMeasurements".
Source code in a2gmeasurements.py
plot_linear_reg_on_near_domain(loaded='august')
Generates a figure with 3 subplots with measured values (default measured values or new values measured for more (speed, time) tuples given as a parameter to the class) and with linear regression model applied to the measured values.
- 3D Scatter Plot of (speed, time, angle) for speed > 0 and angle -> azimuth
- 3D Scatter Plot of (speed, time, angle) for speed < 0 and angle -> azimuth
- 3D Scatter Plot of (speed, time, angle) for speed < 0 and angle -> elevation
If loaded is august then the figure has 4 subplots, with the 4th being
4. 3D Scatter Plot of (speed, time, angle) for speed > 0 and angle -> elevation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loaded |
str
|
which table loaded: the one from |
'august'
|
Source code in a2gmeasurements.py
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 | |
receive_imu_data(stop_event)
Callback function for the imu thread. Read the yaw, pitch, roll angles and stores them in the attribute last_imu_reading of this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_event |
Event
|
when this is set, this function won't do anything. |
required |
Source code in a2gmeasurements.py
setPosControl(yaw, pitch, roll=0, mode=0, model='linear')
Moves Gremsy H16 gimbal by the input angle. This function works as the equivalent to the setPosControl function of the class GimbalRS2. If movement is desired in both yaw and pitch, the gimbal will move first in either axis (i.e. yaw) and after finishing the that movement, it will move in the other axis (i.e. pitch).
If the linear model is used: this function sets an RC control value ("speed") and finds the corresponding time, given the angle desired to be set.
If the gp model is used: this function calls setPosControlGPModel to get the RC control value and time, corresponding to the input angle.
For a more accurate gimbal angle movement:
- Provide a finer and equally sampled grid of RC control value, time with its corresponding measured angle.
- Modify
fit_model_to_gimbal_angular_datato tune parameters of the gp model (if gp model is chosen). - Modify
setPosControlGPModel(if gp model is chosen) if a better logic for getting RC control values and times from a given angle is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaw |
float
|
yaw angle (in degrees) to be set. Valid range between [-180, 180]. |
required |
pitch |
float
|
pitch angle (in degrees) to be set. Valid range between [-90, 90]. |
required |
roll |
int
|
roll angle (in degrees) to be set. Roll angle is not used because desired movement of the gimbal only requires yaw and pitch angles. Defaults to 0. |
0
|
mode |
hexadecimal
|
to be implemented. The idea is to have two modes as in the GimbalRS2 class: |
0
|
model |
str
|
|
'linear'
|
Source code in a2gmeasurements.py
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 | |
setPosControlGPModel(yaw=0, pitch=0)
Finds the RC control value and time giving the desired yaw (or pitch, or both separately) for the gaussian regressor.
Uses an iterative approach to find the RC control value and time to hold it by smartly searching in the grid composed by the RC control values and the times to hold it. The initial value of for the "speed" (RC control value) influences the convergence of the iterative method.
this function requires:
- a better grid of measured "speed", time, angle. With the actual training samples, the prediction does not give physical consistent results (i.e. the time it takes to move 60 degrees at speed 20 is smaller than the one it takes to move 60 degrees at a lower speed). This is because the grid is coarse and not equally sampled.
- this function should guarantee that the returned time is always positive. Negative times does not make physical sense. Furthermore, it should also guarantee that the time is above a certain threshold (i.e. 2 seconds), during which the gimbal will accelerate until reaching the desired speed. In a realistic gimbal, the acceleration of the gimbal is not infinite. On the contrary, gimbal's speed vs time dependence follows usually a trapezoidal curve, which means that there is some time required (the threshold) for the gimbal to reach the plateau of the trapezoid (desired speed).
- the caller of this function (
setPosControl) to handle when an exeception is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaw |
int
|
desired yaw angle to set in the interval [-180,180]. Defaults to 0. |
0
|
pitch |
int
|
desired pitch angle to set. Defaults to 0. |
0
|
Raises:
| Type | Description |
|---|---|
Exception
|
when the optimization local function |
Returns:
| Name | Type | Description |
|---|---|---|
speed_yaw |
float
|
required RC control value to set yaw. |
time_yaw |
float
|
required time to set yaw. |
speed_pitch |
float
|
required RC control value to set pitch |
time_pitch |
float
|
required time to set pitch. |
Source code in a2gmeasurements.py
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 | |
start_imu_thread(COM_PORT='COM21')
Connects to the IMU and creates a new thread (the imu thread) to read the angle data from the IMU.
:param COM_PORT: , defaults to 'COM21' :type COM_PORT: str, optional
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
COM_PORT |
str
|
port where the IMU is connected. If this host computer's OS is Windows it would be |
'COM21'
|
Source code in a2gmeasurements.py
start_thread_gimbal()
Creates an instance of the SBUSEncoder class responsible for encoding the actual serial signal used by sbus protocol to set an RC control value.
By creating such instance, the gremsy gimbal thread is created and started.
Source code in a2gmeasurements.py
stop_thread_gimbal()
stop_thread_imu()
Stops the imu thread and closed the serial port where the IMU is connected.