Go Down

Topic: Stepper motor actuation along with sensor reading (Read 108 times) previous topic - next topic

Lehcim

Hi guys,

In my current project I built a box in which I control temperature and humidity. In the box I would like to do some motor actuation. However, I ran into a few problems.

For measuring temperature and humidity I'm using the SHT11 sensor. Using simple ON/OFF control I'm able to control the climate. For driving the stepper motor I'm using the big easy driver board. Individually, both work fine.

Now, when combining both systems I ran into some trouble. Polling the sensor does take a fair amount of time, which does not allow for climate control during actuation, as I require a smooth movement.

My code is as follows:


Code: [Select]
//Libraries
#include <AccelStepper.h>
#include <stdlib.h>
#include <Timer.h>
#include <SHT1x.h>

//Pins
#define pinMotorStep              0  // define pin for stepping
#define pinMotorDirection         1  // define pin for the motor direction
#define pinMotorOnOff             2  // switch for starting the friction measurements
#define pinSensorClock            5  // current relative humidity and temperature measured by the sensor
#define pinSensorData             6  // pin used for clock sensor
#define pinRelayHumidifier        7  // switch for putting on/off the ultrasonic humidifier

//Create instances
AccelStepper stepperMotor(1,pinMotorStep,pinMotorDirection);             // 1: Dedicated driver board
Timer T;
SHT1x sht1x(pinSensorData, pinSensorClock);

//Climate Settings
int setRelativeHumidity = 60;

//Drive setting
double stepsPerRevolution  = 3200;
double pitchSpindle        = 4;
double slidingSpeed        = 0.2;
double slidingLength       = 32;
double pauseTime           = 0;
double slidingDirection    = 1;

//Run once
void setup(){
 
  pinMode(pinRelayHumidifier, OUTPUT);
  pinMode(pinSensorClock    , INPUT);
  pinMode(pinSensorData     , INPUT);
 
  stepperMotor.setMaxSpeed((slidingSpeed/pitchSpindle)*stepsPerRevolution);
  stepperMotor.setAcceleration((slidingSpeed/pitchSpindle)*stepsPerRevolution);
 
  digitalWrite(pinRelayHumidifier, LOW);

  T.every(2000,humidifierOnOff);

  Serial.begin(9600);
 
}

//Main program
void loop(){
 
  if (digitalRead(pinMotorOnOff)==LOW)
    {
      T.update();
    }
 
  if (digitalRead(pinMotorOnOff)==HIGH)
    {
    if (stepperMotor.distanceToGo()==0)
      {
      stepperMotor.moveTo(slidingDirection*(slidingLength/pitchSpindle)*stepsPerRevolution/2);
      slidingDirection = -slidingDirection;
      }
    stepperMotor.run(); 
    }
 
}

//Subfunctions

void humidifierOnOff(){

  //Get current state
  float currentTemperature = sht1x.readTemperatureC();
  float currentRelativeHumidity = sht1x.readHumidity();
 
  Serial.print(currentTemperature);
  Serial.print('\t');
  Serial.print(currentRelativeHumidity);
  Serial.print('\n');
 
  //Compare with set value   
 
  if (setRelativeHumidity-currentRelativeHumidity>2.0)
    {
    digitalWrite(pinRelayHumidifier, HIGH);
    }
  else
    {
    digitalWrite(pinRelayHumidifier, LOW);
    }
}


Do you have any suggestions how I can combine both systems, such that I still have a smooth movement and climate control during this actuation?

I tryed to move the actuation part to an AtTiny2313 but this gave errors during compiling:
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr25/crttn2313.o:(.init9+0x0): relocation truncated to fit: R_AVR_13_PCREL against symbol `main' defined in .text.main section in core.a(main.cpp.o)
c:/program files

...

... Arduino\libraries\AccelStepper/AccelStepper.cpp:406: additional relocation overflows omitted from the output

What does this error mean? When I remove the AccelStepper instance I don't get compiling errors.

I've googled whether it is possible to use the Attiny in combination with the AccelStepper library, but I couldn't find anything about this subject.

Best regards

PaulS

Code: [Select]
#define pinMotorStep              0  // define pin for stepping
#define pinMotorDirection         1  // define pin for the motor direction

AccelStepper stepperMotor(1,pinMotorStep,pinMotorDirection);             // 1: Dedicated driver board

  Serial.begin(9600);

All in the same code? No!

Code: [Select]
  if (digitalRead(pinMotorOnOff)==LOW)
  if (digitalRead(pinMotorOnOff)==HIGH)

Are you planning to add code to deal with the other possible return values?

I'm missing something. You have not said what the stepper motor is moving. You are reading the temperature and humidity every 2 seconds. Those functions are blocking, and they take some time. If it is too dry you turn the humidifier on. Otherwise, you turn it off.

The rest of the time, the stepper motor seems to be simply sweeping something back and forth.

There are other sensors, and other libraries, that allow starting the reading of the temperature and humidity, and being called back when the data is available. The DHT11 comes to mind. That may be more useful to you.

Lehcim

Code: [Select]
#define pinMotorStep              0  // define pin for stepping
#define pinMotorDirection         1  // define pin for the motor direction

AccelStepper stepperMotor(1,pinMotorStep,pinMotorDirection);             // 1: Dedicated driver board

  Serial.begin(9600);

All in the same code? No!

Code: [Select]
  if (digitalRead(pinMotorOnOff)==LOW)
  if (digitalRead(pinMotorOnOff)==HIGH)

Are you planning to add code to deal with the other possible return values?

I'm missing something. You have not said what the stepper motor is moving. You are reading the temperature and humidity every 2 seconds. Those functions are blocking, and they take some time. If it is too dry you turn the humidifier on. Otherwise, you turn it off.

The rest of the time, the stepper motor seems to be simply sweeping something back and forth.

There are other sensors, and other libraries, that allow starting the reading of the temperature and humidity, and being called back when the data is available. The DHT11 comes to mind. That may be more useful to you.
I use a mechanical switch to switch the actuation of the motor on or off, so I believe those are the only possible states.

The stepper motor is indeed simply sweeping back and forth. It is a tribological setup in which I measure normal and lateral forces.

I'll take a look at the other sensor which does not block.

Should I not place those parts in the same code? I do not understand what is wrong with that.

Another possibility I have is to move the SHT part to an Attiny.


PaulS

Quote
I use a mechanical switch to switch the actuation of the motor on or off, so I believe those are the only possible states.
Exactly. So, if it isn't in one state, it MUST be in the other state.

Code: [Select]
if(digitalRead(pinMotorOnOff)==LOW)
{
   // Do the LOW thing
}
else
{
   // The pin state MUST be HIGH, so do the HIGH thing
}


Quote
Should I not place those parts in the same code? I do not understand what is wrong with that.
The Serial object is an instance of the HardwareSerial class. That object is tied to the hardware serial pins, pins 0 and 1. If you are using Serial, those pins are not available for other purposes.

Quote
Another possibility I have is to move the SHT part to an Attiny.
Anything to get rid of the blocking while getting temperature and humidity data.

Lehcim

Exactly. So, if it isn't in one state, it MUST be in the other state.

Code: [Select]
if(digitalRead(pinMotorOnOff)==LOW)
{
   // Do the LOW thing
}
else
{
   // The pin state MUST be HIGH, so do the HIGH thing
}

The Serial object is an instance of the HardwareSerial class. That object is tied to the hardware serial pins, pins 0 and 1. If you are using Serial, those pins are not available for other purposes.
Anything to get rid of the blocking while getting temperature and humidity data.
Ah I didn't know about the pins 0 and 1 being serial pins. New thing learned today :-).

Considering the LOW or HIGH state you're right. If/Else looks neater.

Thanks.

TomGeorge

Hi,
What arduino platform did you get each of the sketches working separately. UNO Mega etc?
Was this the platform that will not run both sketches together?

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up