Best location to add some arduino code / torque disengagement

Hi,

so i am trying to bring in some torque measurement into the arduino code and got a little bit stuck. But first things first:

Overview:

Using 4.3.1 UDP, MD13s, Phidgets and dual antenna.
Wired in a 1-Channel Relay for cutting off Phidgets completely via PWM2.
Works fine till here.

Got inspired by some topics the other day to do some “torque disengagement”.
Especially the things brought up by @Bennet and @Juliennorth
So brought in a current sensor ACS712 which is wired to the ADS1115 on A2.
ADS returns some nicer readings of the ACS712 output values imo.
Also installed a poti which is wired to A3 of the Nano.

The idea is to bring certain levels of allowed amperage at certain pwm outputs.
Fine tuning it by allowing a little bit “extra” amperage defined via poti.
So imo the code would be best placed in the autosteer nano with having the pwm values at hand.

When testing the code, it works.
But the big disadvantage is reaction time is way longer than on the original code.
Best example: Manual auto steering and setting it 0°, to 5°, 0° etc. etc.
(feels like 0.5 sec doing nothing, then does the steering)
Loading in original code, everything is fine. Except torque disengagement… :slight_smile:

I know i am doing something wrong here, but where ? :slight_smile:

Question is: did i exceed the cycle time at some point ?
Does the arduino have some hp left for some more code?
I did use an array with 10 elements of doubles to average out the amperage.
That might help to slow it down… :slight_smile:

Trying to optimize it. Maybe someone had similar problems.

Almost forgot the code. :slight_smile:
Its very beginner… ish.

1 2

Rather than a picture of your code. Can you edit the post and Copy/paste the code including the following format instruction.

To format code properly in discourse, use 3 backticks (```) on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work.:grinning:

Then we can help easier.

1 Like
void Overload()
{
  
    //Calc if overload on steering
    const byte pinPoti = A1;
    int16_t  ampsInput=0;
    double  ampsCalc=0;
    double ampSum = 0;
    double amps[5];
    double amp =0;

    double multiInput = 0;
    double multi = 0;

    double limit = 0;

    multiInput = analogRead(pinPoti);
    ampsInput = ads.readADC_SingleEnded(2);
    ampsInput = (ampsInput >> 2);

    if (ampsInput > 3372)
    {
       ampsInput = (ampsInput-3372);
    }
    else
    {
      ampsInput= (3372-ampsInput);
    }

    ampsCalc = ampsInput;
    ampsCalc = 30*ampsCalc/3372;


    // Berechneten Stromwert aus Umrechnung in erste Strom-Arraystelle kopieren
    amps[0] = ampsCalc;

    // Strom-Array um eine Stelle aufrücken.
    for (int i = 4; i!=0; i--)
    {
      amps[i] = amps [i-1];
//    Serial.print (i);
//    Serial.print (": ");
//    Serial.println (amps [i]);  
    }

    // Mittelwert aus Strom-Arrayelementen berechnen und abspeichern
    for (int i = 1; i<5; i++)
    {
     ampSum = ampSum + amps[i];  
    }
    amp = ampSum /4;

    //Umrechnung Multiplier
    multiInput = (multiInput * -1)+1023; // Drehrichtung umdrehen
    multi = (10*(multiInput)/1023);    // Auf 10 skalieren

    //Berechnung Stromlimit
    if (pwmDrive < 50)
    {
      limit = multi*1.0;
    }
    else if (pwmDrive < 100)
    {
      limit = multi*1.0;
    }
    else if (pwmDrive < 150)
    {
      limit = multi*1.5;
    }
    else
    {
      limit = multi*1.5;
    }
//
//   Serial.print ("Multiplier: ");
//   Serial.print (multi); 
//   Serial.print ("      Limit: ");
//   Serial.print (limit);   
//   Serial.print ("      Strom: ");
//  Serial.println (amp);      

   if (amp > limit)
    {
      pulseCount++;
    }    
}

looks like you were only sampling from the adc once, reckon the array is not really needed.
Try this. not tested as we don’t have current sensor or rest of your .ino

-	double ampSum = 0;
-	double amps[5];

-	ampsInput = ads.readADC_SingleEnded(2);
-	ampsInput = (ampsInput >> 2);
 
-	if (ampsInput > 3372)
-	{
-	   ampsInput = (ampsInput-3372);
-	}
-	else
-	{
-	  ampsInput= (3372-ampsInput);
-	}
-
-	ampsCalc = ampsInput;
-	ampsCalc = 30*ampsCalc/3372;
-
-
-	// Berechneten Stromwert aus Umrechnung in erste Strom-Arraystelle kopieren
-	amps[0] = ampsCalc;
-
-	// Strom-Array um eine Stelle aufrücken.
-	for (int i = 4; i!=0; i--)
-	{
-	  amps[i] = amps [i-1];
-//	  Serial.print (i);
-//	  Serial.print (": ");
-//	  Serial.println (amps [i]);  
-	}
-
-	// Mittelwert aus Strom-Arrayelementen berechnen und abspeichern
-	for (int i = 1; i<5; i++)
-	{
-	 ampSum = ampSum + amps[i];  
-	}
-	amp = ampSum /4;

+	for (int i = 0; i < 5; i++) {
+		int16_t input = (ads.readADC_SingleEnded(2)) >> 2;
+
+		if (input > 3372) {
+			input = (input - 3372);
+		} else {
+			input = (3372 - input);
+		}
+
+		ampsInput += input;
+	}
+
+	ampsCalc = ampsInput / 5;
+	amp = 30 * ampsCalc / 3372;
void Overload()
{  
	//Calc if overload on steering
	const byte pinPoti = A1;
	int16_t  ampsInput=0;
	double	ampsCalc=0;
	double amp =0;

	double multiInput = 0;
	double multi = 0;

	double limit = 0;

	multiInput = analogRead(pinPoti);

	for (int i = 0; i < 5; i++) {
		int16_t input = (ads.readADC_SingleEnded(2)) >> 2;

		if (input > 3372) {
			input = (input - 3372);
		} else {
			input = (3372 - input);
		}

		ampsInput += input;
	}

	ampsCalc = ampsInput / 5;
	amp = 30 * ampsCalc / 3372;

	//Umrechnung Multiplier
	multiInput = (multiInput * -1)+1023; // Drehrichtung umdrehen
	multi = (10*(multiInput)/1023);    // Auf 10 skalieren

	//Berechnung Stromlimit
	if (pwmDrive < 50) {
	  limit = multi*1.0;
	} else if (pwmDrive < 100) {
	  limit = multi*1.0;
	} else if (pwmDrive < 150) {
	  limit = multi*1.5;
	} else {
		limit = multi*1.5;	
	}

//	Serial.print ("Multiplier: ");
//	Serial.print (multi); 
//	Serial.print ("	  Limit: ");
//	Serial.print (limit);	 
//	Serial.print ("	  Strom: ");
//	Serial.println (amp);	   

  if (amp > limit) {
	  pulseCount++;
	}	 
}
1 Like

Thanks @Apm !
Tested the code today and it was even worse. :smiley:

So that got me thinking.
Your code is sampling the adc 5 times every cycle to eliminate the array. Cool idea.
My version did it once every cycle. So made a comment out of it and it was back to good old speed.

Then i noticed a “delay(10)” at the end of the main loop in the Autosteer_UDP ino.
image

Thought: “hmm…”, commented it out, put my code back in and it runs at lightning speed… (bit overestimated… but hey. :smiley: )

So i am wondering if i did something deeply wrong here ?! Dunno. But runs.
Maybe someone can explain what the delay is meant to do. :slight_smile:

Greetings.

Hello, i would like to use your code in my tractor for disengaging the motor from the steereing wheel. I am using a servo to move the phidget.
I am having trouble with the readings from the amperometer, they are out of scale like always 23 amps even with nothing connected. I have tried the sensor with a test sketch and it is working as it should.
I have connected the sensor to aux1 on pcb v2, is it correct?
could you explain more your setup?
Thank you

A2 of the ADS should be AUX1. Using KaupoiModV2 here.
Sorry for confusion.

When at 0 Ampere the sensor input should be ~ 3372.
When at - Ampere the sensor input should below 3372.
When at + Ampere the sensor input should above 3372.

Thank you, but I did not manage to have usable readings from aux1(A2) adc. Dont know why, always in between 22 or 23 amps when nothing is connected. So I am using directly the nano for readings, I have connected asc712 to A0 and change a little bit the code, works pretty good.
Could you make a video of your setup working?
This is mine AgOpenGps Case JXU 115 auto disengage motor - YouTube

Can do, when i have some free time.
But looks functionwise at lot like yours. :slight_smile:
Looks cool with the automatic engagement!

It takes a second to notice the rise in amperage.
But thats fine for me.
Fine tuning when finding some time. :slight_smile:
Maybe adding some “tune-mode”, recording amperage for different speeds, steering from full left to right and back? Dunno. Thats whats hovering in my mind. :smiley:
Should add some sensitivity.

I need more time too for fine tuning the number of pulse and the limit with potentiometer. it is a bit difficult without the serial monitor.
Thank you for sharing your code!

1 Like

Just an idea, and probably not able to program it myself, but: would it be possible to use the “Count number” for the encoder turn of from AOG and use it to multiply/divide the output from ADS1115, and that way get possibility to adjust ampere turn off setpoint, directly from AOG?

Just run in the background. aog minimized. i used serial monitor for “rough tuning”.

I think i dont fully understand what you are thinking about.
System doesnt use an encoder at the motor, just using the encoder count to shut off autosteer when steering-amperage is over allowed setpoint.

But thinking about it… (and maybe thats what you meant :))
Why not use a simple encoder at the motor to generate a following error.
Would be immediate response. But also more knickknack to go wrong.

So… not sure about it. Fine tuning ACS seems to be simplest solution.

What I mean is, if you want to hit 3372, I want to use the number you can set in AOG, let’s say 6 and then have the number 562 in the program. If you want the program to cut off at higher ampere set 8 in AOG and program shut off at 4496

1 Like

I got it. Thanks!

Cool idea. So you dont have to install a poti.
Neat!

I should definitely have a look at it.

Hi! thank you first for sharing your ideas. I have followed the discussion with interest because it coincides with my idea to disconnect the steer (ACS712, steer arduino. ButI like the option of ​​regulating it through a poti). Your idea of ​​reading with ADS sounds great to me. My problem is ignorance of the code. So, if you would be so kind, upload a downloadable .ino file.
I´ve tried but can’t find a place to put your code and make it work.
Thanks!

Try at own risk. :slight_smile:
Used the 20A ACS712.

Thanks! i´ll try out it!

Hi Lui, I need your help again, I can’t disengage auto steer:
My communication is via USB. I´ve copied your modifications to my Autosteer_USB_cmps14.ino but it didn’t get it to work.
The readings from my ACS 712 20A (connected to A2 of the ADC and conected on the power line (+) of the Cytron) range from 2.32, without load, to 2.45V, stopped the motor. Those of the Poti from 0 to 10V (to the A1 of the Arduino). Without disengagemnt at any case.
My notions of C ++ are very basic; I don’t understand how the disengagement occurs: directly from D6, auxiliary relay parallel to the steer switch?
Where can my mistake be?

Thanks!