• 【luckfox】3、计算重量差


    前言

    本章结合之前的hx711驱动,实现读取质量,记录时间及剩余质量并存入csv文件,计算质量差并总计。

    代码

    luckfox-pico\project\app\test_app\hx711\hx711_app_addtime.c

    #include 
    #include 
    #include 
    #include 
    // #include 
    #include 
    #include 
    #include 
    
    #define IIO_DEVICE "/sys/bus/iio/devices/iio:device0"
    #define SENSOR_CALI_PATH_OFFSET "/root/hx711_cal_offset"
    #define SENSOR_CALI_PATH_SCALE "/root/hx711_cal_scale"
    
    static int cal_offset = 8500000;    // save raw value without test items
    
    static int cal_scale = 475;         // when set phone, 1g is 475
    static int cal_weight = 187;  // the weight of phone
    
    // static float weight = 0;
    static int weight = 0;
    
    
    //--------------- hx711 value process ---------------
    #define LIST_NUM_MAX 64
    #define CSV_PATH "/root/hx711.csv"
    
    int v1,v2;
    int flag_change;
    
    struct weight_data{
        int weight;
        time_t time;
    };
    
    struct weight_data list[LIST_NUM_MAX];
    int current_list_num=0;
    
    int drink_water=0;
    
    //--------------- hx711 value process ---------------
    
    // float convert_to_weight(int sensor_data) {
    int convert_to_weight(int sensor_data) {
        int weight;
        // weight = (float)(sensor_data - cal_offset) / cal_scale;
    
        // printf("\nsensor_raw=%d,cal_offset=%d,cal_scale=%d\n",sensor_data,cal_offset,cal_scale);
        if(cal_scale != 0)
            weight = (sensor_data - cal_offset) / cal_scale;
        else
            weight = 0;
    
        // printf("Sensor data: %.1f\n", weight);
        // printf("Sensor data: %d\n", weight);
    
        return weight;
    }
    
    
    int get_hx711_raw(){
        int fd;
        char buf[64];
        ssize_t num_read;
    
        fd = open(IIO_DEVICE "/in_voltage0_raw", O_RDONLY);
        if (fd < 0) {
            perror("Failed to open iio device");
            return 1;
        }
    
        num_read = read(fd, buf, sizeof(buf) - 1);
        if (num_read < 0) {
            perror("Failed to read sensor data");
            close(fd);
            return 1;
        }
     
        close(fd);
     
        buf[num_read] = '\0';
        int sensor_data = atoi(buf);
        // printf("  raw sensor_data=%d\n",sensor_data);
    
        return sensor_data;
    }
    
    // float get_hx711_value(){
    int get_hx711_value(){
    
        int sensor_data = get_hx711_raw();
    
        weight = convert_to_weight(sensor_data);
    
        return weight;
    }
    
    // save scale&offset to file 
    void set_cal_value(){
        int fd;
        char tmp_char[64];
    
        fd = open(SENSOR_CALI_PATH_OFFSET, O_CREAT|O_RDWR ,0777);
        if (fd < 0) {
            perror("Failed to open cal offset.");
            return;
        }
     
        // printf("-------\ncal_offset=%d\n",cal_offset);
        memset(tmp_char,0,sizeof(tmp_char));
        sprintf(tmp_char,"%d\0",cal_offset);
        // printf("xxx tmp_char=[%s]\n",tmp_char);
        write(fd, tmp_char, sizeof(tmp_char));
    
        close(fd);
    
        fd = open(SENSOR_CALI_PATH_SCALE, O_CREAT|O_RDWR ,0777);
        if (fd < 0) {
            perror("Failed to open cal offset.");
            return;
        }
    
        // printf("cal_scale=%d\n",cal_scale);
        memset(tmp_char,0,sizeof(tmp_char));
        sprintf(tmp_char,"%d\0",cal_scale) ;
        // printf("xxx tmp_char=[%s]\n-------\n",tmp_char);
     
        write(fd, tmp_char, sizeof(tmp_char)-1);
        close(fd);
    }
    
    void print_cal_value_and_raw(int sensor_raw_tmp){
        printf("cal&raw:\n");
        printf("   cal_offset=%d sensor_raw=%d\n", cal_offset, sensor_raw_tmp);
        printf("   test_offset\t%d\n   cal_weight\t%d\n   cal_scale\t%d\n",
                sensor_raw_tmp - cal_offset, cal_weight, cal_scale);
        printf("\n");
    }
    
    void print_cal_value(){
        printf("hx711 calibration value\n");
        printf("   cal_offset\t%d\n   cal_weight\t%d\n   cal_scale\t%d\n",
                cal_offset, cal_weight, cal_scale);
        printf("\n");
    }
    
    void sns_calibration(){
        int cal_test_num = 10;
        int cal_average = 0;
        int cal_test_tmp = 0;
        int cal_scale_raw = 0;
    
        // test 10 times to get offset average
        for(int i=0; i<cal_test_num; i++){
            cal_test_tmp = get_hx711_raw();
            usleep(10);
            cal_average = (cal_average * i + cal_test_tmp)/(i+1);
        }
    
        cal_offset=cal_average;
    
        usleep(20);
    
        printf("!!! Please put test items on the board whose weight same with cmd3\nWaiting input char to continue ...\n");
        getchar();
    
        cal_test_tmp = get_hx711_raw();
        cal_scale_raw = cal_test_tmp - cal_offset;
        cal_scale = (cal_scale_raw)/cal_weight;
    
        print_cal_value_and_raw(cal_test_tmp);
    
        set_cal_value();
    }
    
    
    void get_cal_value(){
        int tmp_offset;
        int tmp_scale;
        char tmp_file_value[64];
        int fd;
    
        // printf("get_cal_value\n");
    
        fd = open(SENSOR_CALI_PATH_OFFSET, O_RDWR,0777);
        if (fd < 0) {
            perror("Failed to open cal offset.");
            return;
        }
    
        read(fd, tmp_file_value, sizeof(tmp_file_value) - 1);
        // printf("tmp_file_value=%s\n",tmp_file_value);
        tmp_offset = atoi(tmp_file_value);
        // printf("tmp_offset=%d\n",tmp_offset);
     
        close(fd);
    
        fd = open(SENSOR_CALI_PATH_SCALE, O_RDWR,0777);
        if (fd < 0) {
            perror("Failed to open cal offset.");
            return;
        }
     
        memset(tmp_file_value,0,sizeof(tmp_file_value));
        read(fd, tmp_file_value, sizeof(tmp_file_value) - 1);
        tmp_scale = atoi(tmp_file_value);
        // printf("tmp_offset=%d\n",tmp_scale);
     
        close(fd);
    
        cal_offset = tmp_offset;
        cal_scale = tmp_scale;
    }
    
    #define LEN_MAX 30
    
    void save_to_csv(struct weight_data value)
    {
        char tmp_c[LEN_MAX];
        char * tmp;
    
        FILE *fp = fopen(CSV_PATH, "a+");
        if (fp == NULL) {
            fprintf(stderr, "fopen() failed.\n");
            exit(EXIT_FAILURE);
        }
    
        struct tm *tm_t;
        tm_t = localtime(&value.time);
        strftime(tmp_c,LEN_MAX,"%F %T",tm_t);
        printf("time:%s\t",tmp_c);
    
        fprintf(fp, tmp_c);
        fprintf(fp, " | ");
    
        memset(tmp_c,0,LEN_MAX);
        sprintf(tmp_c, "%d", value.weight);
        printf("weight:%s\n",tmp_c);
        fprintf(fp, tmp_c);
        fprintf(fp, "\n");
    
        fclose(fp);
    }
    
    
    int value_changed(int value1, int value2)
    {
        if(value1 != value2)
        {
            flag_change = 1;
            // printf("change value v1=%d  v2=%d\n",value1,value2);
        }else{
            if(flag_change == 1 && value1 != 0){
                // save value
                // printf("change value %d\n",value1);
                list[current_list_num].weight = value1;
                // printf("change value %d\n",list[current_list_num].weight);
    
                // save time
                time_t tnow = time(0);
                // printf("当前时间为:%ld\r\n",tnow);
                list[current_list_num].time = tnow;
    
                if(list[current_list_num].weight < list[current_list_num-1].weight){
                    drink_water = drink_water + 
                        list[current_list_num-1].weight - list[current_list_num].weight;
                    printf("== drink %dmL\n",drink_water);
                }
                // save value to file
                save_to_csv(list[current_list_num]);
    
                current_list_num++;
    
                flag_change = 0;
            }
        }
        return flag_change;
    }
    
    
    int get_value()
    {
        int value = 0;
    
        // get value
        value = get_hx711_value();
    
        // save value to v1&v2
        v1 = v2;
        v2 = value;
    
        // judge
        value_changed(v1,v2);
    
        return value;
    }
    
    
    int main(int argc, char *argv[]) {
    
        char cmd1[16];
        char cmd2[16];
        char cmd3[16];
        int ret;
        int val_tmp=0;
    
        // calibration: put the items whose weight is known. weight sends to cmd3
        // ./hx771_app -c 187
        if(argc == 3){
            strcpy(cmd2,argv[1]);
            strcpy(cmd3,argv[2]);
            printf("cmd2=%s cmd3=%s\n",cmd2,cmd3);
            if(strcmp(cmd2, "-c") == 0){
                printf("get cal cal_weight %s\n",cmd3);
                cal_weight=atoi(cmd3);        // save the weight of cal items
            } else {
                printf("hx711 no cal_weight\n");
                return 0;
            }
    
            sns_calibration();
    
    		sleep(1);
    
            // test the calibration result
            val_tmp = get_hx711_value();
            printf("sensor value: %d\n", val_tmp);
    
    		return 0;
        }
    
        printf("-------------test-------------\n");
    
        get_cal_value();
        print_cal_value();
    
        int sensor_data;
        while(1){
            // val_tmp = get_hx711_value();
            val_tmp = get_value();
            // if(val_tmp != 0)
            //     printf("%02d: %d\n",50 - test_num,val_tmp);
            sleep(1);
        }
    
        printf("--------------------------\n");
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348

    编译

    luckfox-pico\project\app\test_app\hx711\build.sh

    export PATH=/home/youkai/0_pro/luckfox/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin:$PATH
    source ~/.bashrc  
    cd ~/0_pro/luckfox/luckfox-pico/project/app/test_app/hx711
    arm-rockchip830-linux-uclibcgnueabihf-gcc hx711_app_addtime.c -o hx711_app_addtime
    
    • 1
    • 2
    • 3
    • 4

    运行

    运行bat可以进行快速测试,放在windows本地

    time_get_hx711.bat

    scp youkai@192.168.206.130:/home/youkai/0_pro/luckfox/luckfox-pico/project/app/test_app/hx711/hx711_app_addtime .
    
    adb push hx711_app_addtime /root/
    adb shell "chmod 777 /root/hx711_app_addtime"
    adb shell "./root/hx711_app_addtime"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    结果

    代码实现了测试重量,并计算出喝水的毫升数。
    在这里插入图片描述

    读取保存的时间和重量。

    # cat hx711.csv
    2023-11-15 19:42:55 | 68
    2023-11-15 19:43:07 | 194
    2023-11-15 19:43:14 | 3
    2023-11-15 19:43:27 | 68
    2023-11-15 19:43:38 | 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    十四届蓝桥青少组模拟赛Python-20221108
    [数据集][VOC]高质量的目标检测数据集合集(持续更新)
    Vue 2 生命周期与 Vue 3 生命周期:介绍与差别对比
    RabbitMQ: SpringBoot 整合 RabbitMQ
    ConcurrentHashMap 的并发度是什么?
    SpringCloud链路追踪SkyWalking-第三章-接入微服务
    D. Cyclic Operations Codeforces Round 897 (Div. 2)
    安装k8s集群
    MQTT 基础--MQTT 发布、订阅和取消订阅 :第 4 部分
    Redis性能测试:redis-benchmark
  • 原文地址:https://blog.csdn.net/qq_38091632/article/details/134427773