Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Feb 16 07:02:56 2019 +0000
Revision:
1:eb499e2a1b9b
Parent:
0:765cf978c3e5
Child:
2:61a0169765bf
Scale is ready. Calibration is ready.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hi1000 0:765cf978c3e5 1 #include "mbed.h"
hi1000 1:eb499e2a1b9b 2 #include <HX711.h>
hi1000 0:765cf978c3e5 3
hi1000 0:765cf978c3e5 4 CAN can1(PD_0, PD_1);
hi1000 0:765cf978c3e5 5 CAN can2(PB_5, PB_6);
hi1000 1:eb499e2a1b9b 6 DigitalOut led1(LED1);
hi1000 1:eb499e2a1b9b 7 DigitalOut led2(LED2);
hi1000 1:eb499e2a1b9b 8 //FlashIAP flashIAP;
hi1000 1:eb499e2a1b9b 9
hi1000 1:eb499e2a1b9b 10 /* scale */
hi1000 1:eb499e2a1b9b 11 HX711 hx711(PB_11, PB_10);
hi1000 1:eb499e2a1b9b 12
hi1000 1:eb499e2a1b9b 13 struct ScaleCalibrationData {
hi1000 1:eb499e2a1b9b 14 unsigned int calibrationWeight; // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000.
hi1000 1:eb499e2a1b9b 15 long offsetValue; // the value for scale offset
hi1000 1:eb499e2a1b9b 16 float scaleValue; // the ADC increment for 1g
hi1000 1:eb499e2a1b9b 17 unsigned char checksum;
hi1000 1:eb499e2a1b9b 18 };
hi1000 1:eb499e2a1b9b 19 unsigned int calibration_ADC_value;
hi1000 1:eb499e2a1b9b 20 //#define CALIBRATION_VALUE 10 // 10g as the calibration weight
hi1000 1:eb499e2a1b9b 21 #define WEIGHT_DIFFERENCE 200 // 10g ADC value minimum difference
hi1000 1:eb499e2a1b9b 22 #define CALIBRATION_WEIGHT 2000 // calibration weight
hi1000 1:eb499e2a1b9b 23
hi1000 1:eb499e2a1b9b 24 ScaleCalibrationData customVar;
hi1000 1:eb499e2a1b9b 25
hi1000 1:eb499e2a1b9b 26 long zero_value;
hi1000 1:eb499e2a1b9b 27 long calibration_value;
hi1000 1:eb499e2a1b9b 28 unsigned int calibration_times; // must calibration 3 times
hi1000 1:eb499e2a1b9b 29 unsigned int calibration_done = 0;
hi1000 1:eb499e2a1b9b 30 float scale_value;
hi1000 1:eb499e2a1b9b 31
hi1000 1:eb499e2a1b9b 32 /* scale */
hi1000 1:eb499e2a1b9b 33 void scaleCalibration()
hi1000 1:eb499e2a1b9b 34 {
hi1000 1:eb499e2a1b9b 35 printf("Start Calibration.\r\n");
hi1000 1:eb499e2a1b9b 36 calibration_done = 0;
hi1000 1:eb499e2a1b9b 37 if (!calibration_done)
hi1000 1:eb499e2a1b9b 38 {
hi1000 1:eb499e2a1b9b 39 led1 = 1;
hi1000 1:eb499e2a1b9b 40 led2 = 0;
hi1000 1:eb499e2a1b9b 41 customVar.calibrationWeight = CALIBRATION_WEIGHT;
hi1000 1:eb499e2a1b9b 42 zero_value = hx711.averageValue(10); // skip first 10 readings
hi1000 1:eb499e2a1b9b 43 zero_value = hx711.averageValue(20);
hi1000 1:eb499e2a1b9b 44 printf("zero_value=%d \r\n", zero_value);
hi1000 1:eb499e2a1b9b 45 calibration_value = 0;
hi1000 1:eb499e2a1b9b 46 scale_value = 0;
hi1000 1:eb499e2a1b9b 47 calibration_times = 0;
hi1000 1:eb499e2a1b9b 48
hi1000 1:eb499e2a1b9b 49 while (( calibration_times < 5))
hi1000 1:eb499e2a1b9b 50 {
hi1000 1:eb499e2a1b9b 51
hi1000 1:eb499e2a1b9b 52 calibration_value = hx711.averageValue(20);
hi1000 1:eb499e2a1b9b 53 if (calibration_value > (zero_value + WEIGHT_DIFFERENCE))
hi1000 1:eb499e2a1b9b 54 {
hi1000 1:eb499e2a1b9b 55 calibration_times++;
hi1000 1:eb499e2a1b9b 56 }
hi1000 1:eb499e2a1b9b 57 else
hi1000 1:eb499e2a1b9b 58 calibration_times = 0;
hi1000 1:eb499e2a1b9b 59 }
hi1000 1:eb499e2a1b9b 60 printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times);
hi1000 1:eb499e2a1b9b 61 if (calibration_times >=5)
hi1000 1:eb499e2a1b9b 62 {
hi1000 1:eb499e2a1b9b 63 // calibration is OK
hi1000 1:eb499e2a1b9b 64 led1 = 0;
hi1000 1:eb499e2a1b9b 65 led2 = 1;
hi1000 1:eb499e2a1b9b 66 calibration_times = 0;
hi1000 1:eb499e2a1b9b 67 scale_value = (calibration_value - zero_value) / customVar.calibrationWeight;
hi1000 1:eb499e2a1b9b 68 customVar.offsetValue = zero_value;
hi1000 1:eb499e2a1b9b 69 customVar.scaleValue = scale_value;
hi1000 1:eb499e2a1b9b 70 // EEPROM.put(0x00, customVar);
hi1000 1:eb499e2a1b9b 71 hx711.setOffset(zero_value);
hi1000 1:eb499e2a1b9b 72 hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details
hi1000 1:eb499e2a1b9b 73 calibration_done = 1;
hi1000 1:eb499e2a1b9b 74 }
hi1000 1:eb499e2a1b9b 75 }
hi1000 1:eb499e2a1b9b 76 }
hi1000 0:765cf978c3e5 77
hi1000 0:765cf978c3e5 78 int a = 0;
hi1000 0:765cf978c3e5 79 int b = 0;
hi1000 0:765cf978c3e5 80
hi1000 0:765cf978c3e5 81 void print_char(char c = '*')
hi1000 0:765cf978c3e5 82 {
hi1000 0:765cf978c3e5 83 printf("%c\r\n", c);
hi1000 0:765cf978c3e5 84 fflush(stdout);
hi1000 0:765cf978c3e5 85 }
hi1000 0:765cf978c3e5 86
hi1000 0:765cf978c3e5 87 Thread thread;
hi1000 0:765cf978c3e5 88
hi1000 1:eb499e2a1b9b 89
hi1000 0:765cf978c3e5 90 CANMessage msg;
hi1000 0:765cf978c3e5 91
hi1000 0:765cf978c3e5 92
hi1000 0:765cf978c3e5 93 InterruptIn button1(USER_BUTTON);
hi1000 0:765cf978c3e5 94 volatile bool button1_pressed = false; // Used in the main loop
hi1000 0:765cf978c3e5 95 volatile bool button1_enabled = true; // Used for debouncing
hi1000 0:765cf978c3e5 96 Timeout button1_timeout; // Used for debouncing
hi1000 0:765cf978c3e5 97
hi1000 0:765cf978c3e5 98 // Enables button when bouncing is over
hi1000 0:765cf978c3e5 99 void button1_enabled_cb(void)
hi1000 0:765cf978c3e5 100 {
hi1000 0:765cf978c3e5 101 button1_enabled = true;
hi1000 0:765cf978c3e5 102 }
hi1000 0:765cf978c3e5 103
hi1000 0:765cf978c3e5 104 // ISR handling button pressed event
hi1000 0:765cf978c3e5 105 void button1_onpressed_cb(void)
hi1000 0:765cf978c3e5 106 {
hi1000 0:765cf978c3e5 107 if (button1_enabled) { // Disabled while the button is bouncing
hi1000 0:765cf978c3e5 108 button1_enabled = false;
hi1000 0:765cf978c3e5 109 button1_pressed = true; // To be read by the main loop
hi1000 0:765cf978c3e5 110 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 0:765cf978c3e5 111 }
hi1000 0:765cf978c3e5 112 }
hi1000 0:765cf978c3e5 113
hi1000 0:765cf978c3e5 114 void print_thread()
hi1000 0:765cf978c3e5 115 {
hi1000 0:765cf978c3e5 116 while (true) {
hi1000 0:765cf978c3e5 117 #if 1
hi1000 0:765cf978c3e5 118 if(can1.read(msg)) {
hi1000 0:765cf978c3e5 119 print_char();
hi1000 0:765cf978c3e5 120 printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
hi1000 0:765cf978c3e5 121 // b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 122 b = msg.data[0];
hi1000 0:765cf978c3e5 123 printf("got data %d 0x%08x \r\n", b, b);
hi1000 0:765cf978c3e5 124 if(msg.id == 1337) {
hi1000 0:765cf978c3e5 125 led2 = !led2;
hi1000 0:765cf978c3e5 126
hi1000 0:765cf978c3e5 127 b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 128 printf("got message %d\r\n", b);
hi1000 0:765cf978c3e5 129 if(b % 5 == 0)
hi1000 0:765cf978c3e5 130 led2 = !led2;
hi1000 0:765cf978c3e5 131 }
hi1000 0:765cf978c3e5 132 }
hi1000 0:765cf978c3e5 133 // wait(0.2);
hi1000 0:765cf978c3e5 134 #endif
hi1000 0:765cf978c3e5 135 }
hi1000 0:765cf978c3e5 136 }
hi1000 0:765cf978c3e5 137
hi1000 0:765cf978c3e5 138 int main()
hi1000 0:765cf978c3e5 139 {
hi1000 0:765cf978c3e5 140 printf("\n\n*** RTOS basic example ***\n");
hi1000 1:eb499e2a1b9b 141 scaleCalibration();
hi1000 1:eb499e2a1b9b 142 thread.start(print_thread);
hi1000 0:765cf978c3e5 143
hi1000 1:eb499e2a1b9b 144 // flashIAP.init();
hi1000 1:eb499e2a1b9b 145 // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
hi1000 0:765cf978c3e5 146 // can1.reset();
hi1000 0:765cf978c3e5 147 // can2.reset();
hi1000 0:765cf978c3e5 148 // can1.frequency(100000);
hi1000 0:765cf978c3e5 149 // can2.frequency(100000);
hi1000 0:765cf978c3e5 150 //button1.mode(PullUp); // Activate pull-up
hi1000 1:eb499e2a1b9b 151 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 0:765cf978c3e5 152
hi1000 0:765cf978c3e5 153 int idx = 0; // Just for printf below
hi1000 0:765cf978c3e5 154
hi1000 0:765cf978c3e5 155 while(1) {
hi1000 0:765cf978c3e5 156 if (button1_pressed) { // Set when button is pressed
hi1000 1:eb499e2a1b9b 157 printf("scale value %f. \r\n", hx711.getGram());
hi1000 0:765cf978c3e5 158 button1_pressed = false;
hi1000 0:765cf978c3e5 159 printf("Button pressed %d\n", idx++);
hi1000 0:765cf978c3e5 160 can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 161 led1 = !led1;
hi1000 0:765cf978c3e5 162 a++;
hi1000 0:765cf978c3e5 163 }
hi1000 0:765cf978c3e5 164 }
hi1000 0:765cf978c3e5 165 #if 0
hi1000 0:765cf978c3e5 166 while(1) {
hi1000 0:765cf978c3e5 167 // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
hi1000 0:765cf978c3e5 168 #if
hi1000 0:765cf978c3e5 169 can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 170 #endif
hi1000 0:765cf978c3e5 171 printf("loop a=%d\n", a);
hi1000 0:765cf978c3e5 172 led1 = !led1;
hi1000 0:765cf978c3e5 173 a++;
hi1000 0:765cf978c3e5 174 wait(0.2);
hi1000 0:765cf978c3e5 175 }
hi1000 0:765cf978c3e5 176 #endif
hi1000 0:765cf978c3e5 177 }