Two Load Cells

I asked Sebi to provide some option to visualise forces read by those cells and gave him a brief idea what I had on my mind thinking that it will entertain him for some time. Meanwhile I took on my the Monkey-business job of the cable crimping.

Not too shabby, huh? 🙂

Anyway, before I’ve been able to finish my crimping task – Sebi reported his part done!

What you see on the picture above is simplified graphical interpretation of our testing rig with forces pulling in a corner. I’ve asked Sebi to tell you what’s going on in his words.

I took a picture of whole setup.

And asked Sebi to give away his Python and Arduino code (Python first).

import pygame
import math
from pygame.time import Clock
import serial

def map(value = float, fromStart = float, fromEnd = float, toStart = float, toEnd = float):
return toStart + (toEnd - toStart) * ((value - fromStart) / (fromEnd - fromStart))

# specifications

ARROW_HEAD_SCALE = 1
ARROW_LINE_SCALE = 1
TEXT_SCALE = 1
OUTPUT_DECIMALS = 4

BACKGROUND_COLOUR = (255, 255, 255)
ARROW_HEAD_COLOUR = (0,0,0)
ARROW_LINE_COLOUR = (0,0,0)
BODY_COLOUR = (0,0,0)
TEXT_COLOUR = (0,0,0)

TEXT_FONT = "arial"

pygame.init()
# of specific dimension..e(X, Y).  
MainWindow = pygame.display.set_mode((0, 0),
                   pygame.RESIZABLE, pygame.FULLSCREEN)  

pygame.font.init() # you have to call this at the start,
           # if you want to use this module.

# set the pygame window name  
pygame.display.set_caption('BPI')

def arrow(screen, lcolour, tricolour, start, end, trirad, thickness=2):
  rad = math.pi/180
  pygame.draw.line(screen, lcolour, start, end, thickness)
  rotation = (math.atan2(start[1] - end[1], end[0] - start[0])) + math.pi/2
  pygame.draw.polygon(screen, tricolour, ((end[0] + trirad * math.sin(rotation),
                    end[1] + trirad * math.cos(rotation)),
                     (end[0] + trirad * math.sin(rotation - 120*rad),
                    end[1] + trirad * math.cos(rotation - 120*rad)),
                     (end[0] + trirad * math.sin(rotation + 120*rad),
                    end[1] + trirad * math.cos(rotation + 120*rad))))

setattr(pygame.draw, 'arrow', arrow)

sensorOutput = 8 * [0.5]

serialPort = serial.Serial(port = "/dev/ttyACM0", baudrate=9600,
                           bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)

serialString = ""                           # Used to hold data coming over UART

# infinite loop  
while True:
  if(serialPort.in_waiting > 0):
    # Read data out of the buffer until a carraige return / new line is found
    serialString = serialPort.readline()

    # Print the contents of the serial data
    serialString = serialString[:-2].decode('Ascii')

    rawOutput = serialString.split(",")
    print(rawOutput)
    if len(rawOutput) == 2: # and isinstance(rawOutput[0], float) and isinstance(rawOutput[1],float)
      sensorOutput[1] = map(float(rawOutput[0]), 0, 2000, 0, 1)
      sensorOutput[2] = map(float(rawOutput[1]), 0, 2000, 0, 1)

  windowWidth, windowHeight = pygame.display.get_surface().get_size()

  MainWindow.fill(BACKGROUND_COLOUR)

  pygame.draw.rect(MainWindow, BODY_COLOUR, (windowWidth/3, windowHeight/3, windowWidth/3, windowHeight/3))

  # top left
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/3, windowHeight/2.4), (windowWidth/3-windowWidth/3*sensorOutput[0], windowHeight/2.4), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # top left centre
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/2.4, windowHeight/3), (windowWidth/2.4, windowHeight/3-windowHeight/3*sensorOutput[1]), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # top right centre
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/12*7, windowHeight/3), (windowWidth/12*7, windowHeight/3-windowHeight/3*sensorOutput[2]), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # top right
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/1.5-1, windowHeight/2.4), (windowWidth/1.5+windowWidth/3*sensorOutput[6], windowHeight/2.4), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # bottom left
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/3, windowHeight/12*7), (windowWidth/3-windowWidth/3*sensorOutput[4], windowHeight/12*7), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # bottom left centre
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/2.4, windowHeight/1.5-1), (windowWidth/2.4, windowHeight/1.5+windowHeight/3*sensorOutput[5]), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # bottom right centre
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/12*7, windowHeight/1.5-1), (windowWidth/12*7, windowHeight/1.5+windowHeight/3*sensorOutput[3]), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)
  # bottom right
  pygame.draw.arrow(MainWindow, ARROW_LINE_COLOUR, ARROW_HEAD_COLOUR, (windowWidth/1.5-1, windowHeight/12*7), (windowWidth/1.5+windowWidth/3*sensorOutput[7], windowHeight/12*7), round(windowWidth/24)*ARROW_HEAD_SCALE, round(windowWidth/24)*ARROW_LINE_SCALE)

  textFont = pygame.font.SysFont(TEXT_FONT, round((windowWidth/26)+(windowHeight/26)/2*TEXT_SCALE))

  topLeft = textFont.render(str(round(sensorOutput[0],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  topLeftCentre = textFont.render(str(round(sensorOutput[1],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  topRightCentre = textFont.render(str(round(sensorOutput[2],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  topRight = textFont.render(str(round(sensorOutput[3],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  bottomLeft = textFont.render(str(round(sensorOutput[4],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  bottomLeftCentre = textFont.render(str(round(sensorOutput[5],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  bottomRightCentre = textFont.render(str(round(sensorOutput[6],OUTPUT_DECIMALS)), True, TEXT_COLOUR)
  bottomRight = textFont.render(str(round(sensorOutput[7],OUTPUT_DECIMALS)), True, TEXT_COLOUR)

  MainWindow.blit(topLeft, dest=(0, windowHeight/3-topLeft.get_height()))
  MainWindow.blit(topLeftCentre, dest = (windowWidth/3-topLeftCentre.get_width(), 0))
  MainWindow.blit(topRightCentre, dest = (windowWidth/1.5, 0))
  MainWindow.blit(topRight, dest=(windowWidth-topRight.get_width(), windowHeight/3-topRight.get_height()))
  MainWindow.blit(bottomLeft, dest=(0, windowHeight/1.5))
  MainWindow.blit(bottomLeftCentre, dest=(windowWidth/3-bottomLeftCentre.get_width(), windowHeight-bottomLeftCentre.get_height()))
  MainWindow.blit(bottomRightCentre, dest=(windowWidth/1.5, windowHeight-bottomRightCentre.get_height()))
  MainWindow.blit(bottomRight, dest=(windowWidth-bottomRight.get_width(), windowHeight/1.5))

  pygame.display.update()
  Clock().tick_busy_loop(20) 

Now Arduino

#include "HX711.h"

#define calibration_factor -21600 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  3
#define LOADCELL_SCK_PIN  2

HX711 scale1;
HX711 scale2;

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale1.begin(3, 2);
  scale1.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale1.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  scale2.begin(5, 4);
  scale2.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale2.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");
}

void loop() {
  String full = String(scale1.get_units()*10)+", "+String(scale2.get_units()*10);
  Serial.println(full);
  delay(100);
}

Pretty nice, isn’t it? As always, let us know what you think about it 😉

One thought on “Two Load Cells

  1. I feel like I’m looking at a piece of modern art and a musical score. Go, team BBBlimp!

    Sent from iPhone by “either” Robin or Serge

    >

Leave a Reply