Getting "unable to sync" when I use the "HX711.h" library with rosserial

asked 2019-11-01 07:23:23 -0600

Lowyal gravatar image

I want to messure weight with a load cell using the breakoutboard HX711 with an Arduino. After that, i want to publish the values. When I using the HX711.h library and the command "rosrun rosserial_arduino serial_node.py _port:=/dev/ttyACM0", I get the error "Unable to sync". I tried diffrent baud rates but nothing changed. Can someone help me pls?

Library: https://github.com/bogde/HX711/tree/m...

See my code below:

#include "HX711.h"
#include <ros.h>
#include <lift_action/Pressure8.h>

#define SCALE_FACTOR 71
#define OFFSETT 15000

#define COMPARE_VAL 5000

#define DAT_PIN0 1
#define SCK_PIN0 2

#define DAT_PIN1 3
#define SCK_PIN1 4

#define DAT_PIN2 5
#define SCK_PIN2 6

#define DAT_PIN3 7
#define SCK_PIN3 8

#define DAT_PIN4 9
#define SCK_PIN4 10

#define DAT_PIN5 11
#define SCK_PIN5 12

#define DAT_PIN6 13
#define SCK_PIN6 14

#define DAT_PIN7 15
#define SCK_PIN7 16


ros::NodeHandle nh;
lift_action::Pressure8 pressure8_msg;
ros::Publisher ardu_pressure8("data_array", &pressure8_msg);

long senVal;
bool sendFlag;
bool compare;

HX711 sen[0];

void setup() {
 // put your setup code here, to run once:

   nh.initNode();
   nh.advertise(ardu_pressure8);

   sen[0].begin(DAT_PIN0,SCK_PIN0);
   sen[1].begin(DAT_PIN1,SCK_PIN1);
   sen[2].begin(DAT_PIN2,SCK_PIN2);
   sen[3].begin(DAT_PIN3,SCK_PIN3);
   sen[4].begin(DAT_PIN4,SCK_PIN4);
   sen[5].begin(DAT_PIN5,SCK_PIN5);
   sen[6].begin(DAT_PIN6,SCK_PIN6);
   sen[7].begin(DAT_PIN7,SCK_PIN7);

   for(int i = 0; i < 8; i++) {

      sen[i].set_scale(SCALE_FACTOR);
      sen[i].set_offset(OFFSETT);
      pressure8_msg.sensor[i] = true;
  }
}

void loop() {
// put your main code here, to run repeatedly:
    sendFlag = false;

    for(int i = 0; i < 8; i++) {
       senVal = sen[i].get_units(10);
      compare = (senVal < COMPARE_VAL) ? false:true;

      if(compare != pressure8_msg.sensor[i]) {
         pressure8_msg.sensor[i] = compare;
         sendFlag = true;
     }
   } 
   if(sendFlag != false) {
      ardu_pressure8.publish(&pressure8_msg);
   }
   nh.spinOnce();
}
edit retag flag offensive close merge delete