![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
LoadCell.cpp@16:691649d8a3da, 2018-09-05 (annotated)
- Committer:
- gimohd
- Date:
- Wed Sep 05 07:50:27 2018 +0000
- Revision:
- 16:691649d8a3da
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gimohd | 16:691649d8a3da | 1 | #include "LoadCell.h" |
gimohd | 16:691649d8a3da | 2 | |
gimohd | 16:691649d8a3da | 3 | LoadCell::LoadCell(PinName pin): ain(pin) |
gimohd | 16:691649d8a3da | 4 | { |
gimohd | 16:691649d8a3da | 5 | SAMPLE_AMOUNT = 2000; |
gimohd | 16:691649d8a3da | 6 | CALIBRATION_MASS = 843.56; // 274.19 |
gimohd | 16:691649d8a3da | 7 | TARE_VALUE = 0.743027; |
gimohd | 16:691649d8a3da | 8 | CALIBRATION_VALUE = 0.725960; |
gimohd | 16:691649d8a3da | 9 | OFFSET = TARE_VALUE-CALIBRATION_VALUE; |
gimohd | 16:691649d8a3da | 10 | DOWN_OFFSET = 0.023591; |
gimohd | 16:691649d8a3da | 11 | }; |
gimohd | 16:691649d8a3da | 12 | |
gimohd | 16:691649d8a3da | 13 | |
gimohd | 16:691649d8a3da | 14 | float LoadCell::analogRead() |
gimohd | 16:691649d8a3da | 15 | { |
gimohd | 16:691649d8a3da | 16 | float samples [SAMPLE_AMOUNT]; |
gimohd | 16:691649d8a3da | 17 | for(int i = 0; i < SAMPLE_AMOUNT; i++) { |
gimohd | 16:691649d8a3da | 18 | float read =ain.read(); |
gimohd | 16:691649d8a3da | 19 | samples [i] =read; |
gimohd | 16:691649d8a3da | 20 | } |
gimohd | 16:691649d8a3da | 21 | return Maths::mean(samples,0,SAMPLE_AMOUNT); |
gimohd | 16:691649d8a3da | 22 | } |
gimohd | 16:691649d8a3da | 23 | |
gimohd | 16:691649d8a3da | 24 | float LoadCell::simpleAnalogRead() |
gimohd | 16:691649d8a3da | 25 | { |
gimohd | 16:691649d8a3da | 26 | return ain.read(); |
gimohd | 16:691649d8a3da | 27 | } |
gimohd | 16:691649d8a3da | 28 | |
gimohd | 16:691649d8a3da | 29 | float LoadCell::calculateMass(float value) |
gimohd | 16:691649d8a3da | 30 | { |
gimohd | 16:691649d8a3da | 31 | return ((TARE_VALUE - value)*(CALIBRATION_MASS))/(OFFSET); |
gimohd | 16:691649d8a3da | 32 | } |
gimohd | 16:691649d8a3da | 33 | |
gimohd | 16:691649d8a3da | 34 | |
gimohd | 16:691649d8a3da | 35 | float LoadCell::tare() |
gimohd | 16:691649d8a3da | 36 | { |
gimohd | 16:691649d8a3da | 37 | TARE_VALUE = 0; |
gimohd | 16:691649d8a3da | 38 | float values[20]; |
gimohd | 16:691649d8a3da | 39 | for (int a = 0; a<20; a++) { |
gimohd | 16:691649d8a3da | 40 | values[a] = analogRead(); |
gimohd | 16:691649d8a3da | 41 | } |
gimohd | 16:691649d8a3da | 42 | |
gimohd | 16:691649d8a3da | 43 | TARE_VALUE = Maths::mean(values,0,20); |
gimohd | 16:691649d8a3da | 44 | return TARE_VALUE; |
gimohd | 16:691649d8a3da | 45 | } |
gimohd | 16:691649d8a3da | 46 | |
gimohd | 16:691649d8a3da | 47 | float LoadCell::tareDown() |
gimohd | 16:691649d8a3da | 48 | { |
gimohd | 16:691649d8a3da | 49 | TARE_VALUE = 0; |
gimohd | 16:691649d8a3da | 50 | float values[20]; |
gimohd | 16:691649d8a3da | 51 | for (int a = 0; a<20; a++) { |
gimohd | 16:691649d8a3da | 52 | values[a] = analogRead(); |
gimohd | 16:691649d8a3da | 53 | } |
gimohd | 16:691649d8a3da | 54 | |
gimohd | 16:691649d8a3da | 55 | TARE_VALUE = Maths::mean(values,0,20)- DOWN_OFFSET; |
gimohd | 16:691649d8a3da | 56 | return TARE_VALUE; |
gimohd | 16:691649d8a3da | 57 | } |
gimohd | 16:691649d8a3da | 58 | |
gimohd | 16:691649d8a3da | 59 | float LoadCell::callibrate() |
gimohd | 16:691649d8a3da | 60 | { |
gimohd | 16:691649d8a3da | 61 | CALIBRATION_VALUE = analogRead(); |
gimohd | 16:691649d8a3da | 62 | return CALIBRATION_VALUE; |
gimohd | 16:691649d8a3da | 63 | } |
gimohd | 16:691649d8a3da | 64 | |
gimohd | 16:691649d8a3da | 65 | |
gimohd | 16:691649d8a3da | 66 | float LoadCell::mass() |
gimohd | 16:691649d8a3da | 67 | { |
gimohd | 16:691649d8a3da | 68 | return calculateMass(analogRead()); |
gimohd | 16:691649d8a3da | 69 | } |
gimohd | 16:691649d8a3da | 70 | |
gimohd | 16:691649d8a3da | 71 | float LoadCell::simpleMass() |
gimohd | 16:691649d8a3da | 72 | { |
gimohd | 16:691649d8a3da | 73 | return calculateMass(simpleAnalogRead()); |
gimohd | 16:691649d8a3da | 74 | } |
gimohd | 16:691649d8a3da | 75 | |
gimohd | 16:691649d8a3da | 76 | |
gimohd | 16:691649d8a3da | 77 | void LoadCell::setCalibrationMass(int mass) |
gimohd | 16:691649d8a3da | 78 | { |
gimohd | 16:691649d8a3da | 79 | CALIBRATION_MASS = mass; |
gimohd | 16:691649d8a3da | 80 | } |
gimohd | 16:691649d8a3da | 81 | |
gimohd | 16:691649d8a3da | 82 | void LoadCell::setSampleAmount(int samples) |
gimohd | 16:691649d8a3da | 83 | { |
gimohd | 16:691649d8a3da | 84 | SAMPLE_AMOUNT = samples; |
gimohd | 16:691649d8a3da | 85 | } |
gimohd | 16:691649d8a3da | 86 | |
gimohd | 16:691649d8a3da | 87 |