Topic: Slow jerky servo response to slider input
Hi
I am trying to control two servos from sliders on my Android phone and communicating using BLE-HM10.
Everything functions but I have an irritating issue:
The response to my slider input is slow/stuttering. When I smoothly swipe along the Slider on the GUI, the servo moves to the correct position but it does so in jerky steps -just as if their position signal is only being updated at around 3 or 4 Hz. If I swipe slower then the jerkiness is less pronounced (as would be expected from slow updating). It feels like my Arduino code is taking 200ms to 300ms for each loop!
Any ideas? Does RemoteXY just take that long to run each time? Have I got a library with a Delay() in it somewhere?
My code is below -note I also have code to read some thermocouples and report that to my RemoteXY GUI -that all works fine.
//This program allows position control of the servos via RemoteXY client.
#include <Wire.h>
#include <RunningAverage.h>
#include <Servo.h>
#include <SPI.h> //http://arduino.cc/en/Reference/SPI
#include <Thermocouple.h> //http://github.com/JChristensen/Thermocouple
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,2,0,24,0,153,0,8,13,0,
130,1,2,35,96,27,5,67,4,50,
4,20,5,2,26,6,67,4,50,10,
20,5,2,26,6,67,4,50,16,20,
5,2,26,6,67,4,50,22,20,5,
2,26,6,4,128,68,51,26,6,2,
26,4,128,3,51,62,6,2,26,129,
0,9,41,22,6,17,84,104,114,111,
116,116,108,101,0,129,0,67,42,18,
6,17,67,104,111,107,101,0,129,0,
19,4,20,6,17,69,110,103,105,110,
101,0,129,0,19,10,23,6,17,69,
120,104,97,117,115,116,0,129,0,19,
16,28,6,17,87,97,116,101,114,32,
79,117,116,0,129,0,19,22,23,6,
17,87,97,116,101,114,32,73,110,0 };
// this structure defines all the variables of your control interface
struct {
// input variable
int8_t choke; // =0..100 slider position
int8_t throttle; // =0..100 slider position
// output variable
char TempEngine[6]; // string UTF8 end zero
char TempExhaust[6]; // string UTF8 end zero
char TempWaterOut[6]; // string UTF8 end zero
char TempWaterIn[6]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
//MAX6675 SPI pin definitions
#define csTC1 6 //chip select for MAX6675 #1
#define csTC2 7 //chip select for MAX6675 #2
#define csTC3 8 //chip select for MAX6675 #3
//#define csTC4 9 //chip select for MAX6675 #4
//Additionally, connect the MAX6675s as follows:
//MISO Arduino pin 12 //master in slave out
//SCK Arduino pin 13 //serial clock
Thermocouple tc_engine = Thermocouple(csTC1); //instantiate the thermocouple objects
Thermocouple tc_exhaust = Thermocouple(csTC2);
Thermocouple tc_waterin = Thermocouple(csTC3);
//Thermocouple tc_waterout = Thermocouple(csTC4);
float EngineTemp;
float ExhaustTemp;
float WaterInTemp;
float WaterOutTemp;
char EngineTempChar[6];
char ExhaustTempChar[6];
char WaterInTempChar[6];
char WaterOutTempChar[6];
//define Servos
Servo ServoThrottle;
Servo ServoChoke;
//Thermocouple Read Timer
const int read_interval = 500;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousRead_Millis = 0;
//SETUP--------------------------------------------------------------------------------------------
void setup()
{
RemoteXY_Init ();
Serial.begin(9600); //initialize Serial
ServoThrottle.attach(9);
ServoChoke.attach(10);
delay (20);
RemoteXY.throttle = 50; //sets GUI slider to mid
RemoteXY.choke = 50; //sets GUI slider to mid
}
//LOOP---------------------------------------------------------------------------------------------
void loop()
{
RemoteXY_Handler ();
ServoControl();
ReadThermo();
currentMillis = millis();
}
//---------------------------------------------------------------------------------------------
void ServoControl()
{
int ms = RemoteXY.throttle*20+500;
ServoThrottle.writeMicroseconds(ms);
int ms2 = RemoteXY.choke*20+500;
ServoChoke.writeMicroseconds(ms2);
}
//---------------------------------------------------------------------------------------------
void ReadThermo()
{
if (currentMillis - previousRead_Millis >= read_interval) //only call when timer runs out
{
//read thermocouple temperatures as float
EngineTemp = tc_engine.readC();
ExhaustTemp = tc_exhaust.readC();
WaterInTemp = tc_waterin.readC();
//WaterOutTemp = tc_waterout.readC();
//send to RemoteXY app
dtostrf(EngineTemp,3,2,RemoteXY.TempEngine);
dtostrf(ExhaustTemp,3,2,RemoteXY.TempExhaust);
dtostrf(WaterInTemp,3,2,RemoteXY.TempWaterIn);
dtostrf(WaterOutTemp,3,2,RemoteXY.TempWaterOut);
//reset timer
previousRead_Millis = currentMillis;
}
}