|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +float weightsA1 = 0.0084914244; |
| 4 | +float weightsA2 = -0.0182792591; |
| 5 | +float weightsA3 = 0.0115852731; |
| 6 | +float weightsA4 = 0.0051548949; |
| 7 | +const float TORQUE_CALIBRATION_SLOPE = 0.0108; |
| 8 | +const float TORQUE_CALIBRATION_OFFSET = -7.7948; |
| 9 | + |
| 10 | +float measurement1; |
| 11 | +float measurement2; |
| 12 | +float measurement3; |
| 13 | +float measurement4; |
| 14 | +float measurement5; |
| 15 | + |
| 16 | +int torqueSensor = A4; |
| 17 | +int thrustSensor1 = A0; |
| 18 | +int thrustSensor2 = A3; |
| 19 | +int thrustSensor3 = A1; |
| 20 | +int thrustSensor4 = A2; |
| 21 | +float temp1 = 0; |
| 22 | +float temp2 = 0; |
| 23 | +float temp3 = 0; |
| 24 | +float temp4 = 0; |
| 25 | +float temp5 = 0; |
| 26 | + |
| 27 | +float currentForce = 1e3; |
| 28 | +float forcenm1 = 1e6; |
| 29 | +float forcenm2; |
| 30 | +float currentAvg; |
| 31 | +float lastAvg; |
| 32 | +float forceConvergenceThreshold = 0.01; |
| 33 | + |
| 34 | +float currentTorque = 1e3; |
| 35 | +float torquenm1 = 1e6; |
| 36 | +float torquenm2; |
| 37 | +float currentTorqueAvg; |
| 38 | +float lastTorqueAvg; |
| 39 | +float torqueConvergenceThreshold = 0.01; |
| 40 | + |
| 41 | +float tareWeight = 0; |
| 42 | +float tareTorque = 0; |
| 43 | + |
| 44 | +void setup() { |
| 45 | + // put your setup code here, to run once: |
| 46 | + Serial.begin(115200); |
| 47 | + |
| 48 | + pinMode(torqueSensor, INPUT); |
| 49 | + pinMode(thrustSensor1, INPUT); |
| 50 | + pinMode(thrustSensor2, INPUT); |
| 51 | + pinMode(thrustSensor3, INPUT); |
| 52 | + pinMode(thrustSensor4, INPUT); |
| 53 | + |
| 54 | + tareWeight = getForce(); |
| 55 | + tareTorque = getTorque(); |
| 56 | +} |
| 57 | + |
| 58 | +void loop() { |
| 59 | + // put your main code here, to run repeatedly: |
| 60 | + float force = getForce(); |
| 61 | + float torque = getTorque(); |
| 62 | + |
| 63 | + Serial.print(force, 3); Serial.print(","); Serial.println(torque, 3); |
| 64 | + Serial.flush(); |
| 65 | + delay(10); |
| 66 | +} |
| 67 | + |
| 68 | +float getTorque() { |
| 69 | + float force; |
| 70 | + float moment_arm = 0.1158875; |
| 71 | + |
| 72 | + force = analogRead(torqueSensor) * TORQUE_CALIBRATION_SLOPE - TORQUE_CALIBRATION_OFFSET; |
| 73 | + |
| 74 | + return (force * moment_arm) - tareTorque; |
| 75 | +} |
| 76 | + |
| 77 | +float getForce() { |
| 78 | + float mass; |
| 79 | + for (int i = 0; i < 10; i++) { |
| 80 | + measurement1 = analogRead(thrustSensor1) * weightsA1; |
| 81 | + temp1 = temp1 + measurement1; |
| 82 | + |
| 83 | + measurement2 = analogRead(thrustSensor2) * weightsA2; |
| 84 | + temp2 = temp2 + measurement2; |
| 85 | + |
| 86 | + measurement3 = analogRead(thrustSensor3) * weightsA3; |
| 87 | + temp3 = temp3 + measurement3; |
| 88 | + |
| 89 | + measurement4 = analogRead(thrustSensor4) * weightsA4; |
| 90 | + temp4 = temp4 + measurement4; |
| 91 | + } |
| 92 | + temp1 = temp1 / 10; |
| 93 | + temp2 = temp2 / 10; |
| 94 | + temp3 = temp3 / 10; |
| 95 | + temp4 = temp4 / 10; |
| 96 | + mass = temp1 + temp2 + temp3 + temp4; |
| 97 | + return (mass * 9.81) - tareWeight; |
| 98 | +} |
0 commit comments