View unanswered posts | View active topics It is currently Thu Aug 28, 2014 11:12 am






Reply to topic  [ 9 posts ] 
coding for failed Segway 
Author Message
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post coding for failed Segway
This is the coding for my failed ultrasonic-segway.

Code:
#pragma config(Sensor, S2,     SonarSensor,         sensorSONAR)
#pragma config(Sensor, S3,     TouchSensor,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main(){

   int reading1;
   int reading2;
   int reading3;
   int reading4;
   int reading5;
   int total;
   int straightValue;
   int distanceToGround;
   int distanceDifference;
   int doubleDifference;
   int speed;
   int finalSpeed;

   while (SensorValue (TouchSensor) == 0){
     nxtDisplayCenteredTextLine(3, "Balance robot");
     nxtDisplayCenteredTextLine(4, "and press button");
   }

   eraseDisplay();

   reading1 = SensorValue(SonarSensor);
   wait10Msec(2);
   reading2 = SensorValue(SonarSensor);
   wait10Msec(2);
   reading3 = SensorValue(SonarSensor);
   wait10Msec(2);
   reading4 = SensorValue(SonarSensor);
   wait10Msec(2);
   reading5 = SensorValue(SonarSensor);
   total = reading1 + reading2 + reading3 + reading4 + reading5;
   straightValue = total/5;
   PlaySound(soundBeepBeep);

   while (true){
      reading1 = SensorValue(SonarSensor);
      wait1Msec(2);
       reading2 = SensorValue(SonarSensor);
       wait1Msec(2);
       reading3 = SensorValue(SonarSensor);
       wait1Msec(2);
       reading4 = SensorValue(SonarSensor);
       wait1Msec(2);
       reading5 = SensorValue(SonarSensor);
       total = reading1 + reading2 + reading3 + reading4 + reading5;
       total = SensorValue(SonarSensor);
      distanceToGround = total/5;
      distanceToGround = total;

       distanceDifference = distanceToGround - straightValue;
       doubleDifference = distanceDifference + distanceDifference;
       speed = distanceDifference - doubleDifference;
       finalSpeed = speed*2;

       motor[motorA] = finalSpeed;
       motor[motorC] = finalSpeed;
   }
}


Wanna know more about the ultrasonic segway? check the following post(s):
Ultrasonic Segway Experiment

_________________
leonoverweel.com


Sun Oct 17, 2010 2:14 pm
Profile WWW
Rookie

Joined: Tue Sep 07, 2010 10:56 am
Posts: 43
Post Re: coding for failed Segway
Hi,
I saw your code, and there are some things, that i think you should know:
In a segway, you have to use a PID control, not onli the Proporcional (as you did) but the Integral, that fix the past erro and the Derivate, that predicts the next erro.
One problem with the use of ultrasonic sensort for that thing, is that one centimeter it's a very big value... it should mesure in millimiters, because a segway is a very precise thing, if everything is not setup very well, everything crash. It includes the correct use of the PID, the correct sensor and the correct robot.
I'm not sayng that is impossible, but here are some thing that you should improve:
-Search in google, for a PID algoritm, it will improve yours robot's very well...
-Maybe, the ultrasonic sensor placed with a 45º would be more precise, because it would return a better value, as the angle of the robot will be more precise in that angle...(understand?)
-After reading what's a PID control, try to do it with the code, it's not so dificult, and you can share the code, and we can help you in it (try first)
I hope it helps, and Good luck for you! i want to see that segway in your blog not "Failed" but "Perfect"
Ivan

_________________
My creations: techlego.blogspot.com


Sun Oct 17, 2010 9:43 pm
Profile WWW
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: coding for failed Segway
Thanks, man! I have abandoned the ultrasonic one, but I started making another one just today. I'm going to try to reprogram that one using your tips, but I'm going to be away from my PC for a while (holiday), so it might take some time. I'll post (here and on my blog) it when I'm done with it, though. But thanks a lot anyways!

DiMastero

_________________
leonoverweel.com


Mon Oct 18, 2010 5:22 am
Profile WWW
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: coding for failed Segway
Hi there,

I've been reading a few articles on PID control, and get how they work. I don't really know how to accurately code it, though. Could you post an example? I also found the variables I need:
Code:
float Time=0;
float Sum_err = 0;
float Sum_err_End = 0;
float err = 0;
float d_err = 0;
float last_err=0;
float d_val_0= 0;
float d_val_1= 0;
float P = 0;
float I = 0;
float D = 0;
float pid = 0;
float I_old = 0;
float rest_I = 0;
float delta_I=0;

Is that correct, or am I missing some/ having too many?

thx,
DiMastero

_________________
leonoverweel.com


Mon Oct 18, 2010 5:47 am
Profile WWW
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: coding for failed Segway
Ok, I wrote a more PID-like program for it, but it doesn't make much of a difference :(. Here it is:
Code:
#pragma config(Sensor, S3,     TouchSensor,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main(){

   int desiredAngle = 0;
   int actualAngle = 0;
   int error = 0;
   int speed = 0;
   
   nMotorEncoder[motorB] = 0;
   
   while (SensorValue (TouchSensor) == 0){}
   
   desiredAngle = nMotorEncoder[motorB];
   PlaySound(soundBeepBeep);
   
   while(true){
    actualAngle = nMotorEncoder[motorB];
    error = actualAngle - desiredAngle;
    speed = error*10;
   
    motor [motorA] = -speed;
    motor [motorC] = -speed;
   }
   
}


One advantage of it is that it's smaller, but it doesn't make much of a difference. I know this isn't a real full PID control, but I don't really know how to write one. Any suggestions?

thx,
DiMastero

_________________
leonoverweel.com


Mon Oct 18, 2010 8:47 am
Profile WWW
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post Re: coding for failed Segway
I noticed that you take THE mean of 5 US readings, each reading being 2 msec apart. THE US sensor updates every 70 msec only, you should take this into account. Also THE mean is stores as an integer, making it à float could add some precision. Last thing that could help is to filter out the 255 readings, these indicate a failed reading but will have a big effect on the calculated mean.

I do have some worries about the update frequency of the US. Maybe once every 70 msec won't be enough for this purpose. But I do not have any experience with segways, so I can't tell. There is a possibility to change the update frequency in the I2C protocol of this sensor, this could help. But then you have to keep the speed of sound in mind. A sonar bleep will need a chance to be detected before a new one is issued. I also lack any experience with modifying the US sensor through I2C.

_________________
My most recent blog: A grain of sugar


Mon Oct 18, 2010 9:48 am
Profile WWW
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: coding for failed Segway
Thank you, Aswin! I don't really have any segway experience either, and (as I don't have a gyro sensor) I've been trying to use different sensors for making my own, including the US sensor. I like your idea to filter out 255 (or even 70+) readings, which might stabilize it; I'll try to do that the next time I build it (I took the Ultrasonic one apart this morning because it just started reacting to any change too late - the readings weren't accurate enough, and, as you just said, there aren't enough readings per second for my program to work accurately). I didn't EVER do anything with I2C, so I won't be able to change the frequency of the readings :(. I have tried again, though, using a different sensor (rotation sensor on motors), which I posted about on my blog (see below). So, thank's a lot for the tips; I'll keep 'em in mind the next time I build (a segway) using the US sensor :).

DiMastero

_________________
leonoverweel.com


Mon Oct 18, 2010 11:02 am
Profile WWW
Rookie

Joined: Tue Sep 07, 2010 10:56 am
Posts: 43
Post Re: coding for failed Segway
Hi, good that you have read about PID... I at first, tought that would onli be the value readed, multiplied by a constant, and put it to the robot... it works for many tings as a line follower, and a simple "coast" motor, but for a segway, we have to use the I and D.
Basicly, the Proporcional is the value of the reading multiplied by a constant, but in a segway, he "tends" to go for a side, and we have to "predict" what side is that, and how much is he going to fell, to have an accurrace reading, that's the Derivate, it makes a prediction based on the past values.
We have to use the Integral, to fiz small errors, what does the Integral does? it stores all the errors in a variable, and, it's made to fix constant errors.
Imagine a line follower, it is making a curve, and it's always at a side, the sensor storing that error value, is going to tend to go faster with one whell to fix that problem, that's what the Integral does.
I saw a nice page explaining very well that, i will see if i can find it.
One important point, at creating a segway with the US, place him at ~45º, because you will get a more accurrace value...
Good luck,
Ivan

_________________
My creations: techlego.blogspot.com


Mon Oct 18, 2010 11:59 am
Profile WWW
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: coding for failed Segway
Thank you! Ok, I've done some testing, and the robot tends to fall backwards. Could anyone tell me how to program the I (remembring and adding up different errors, right?)

DiMastero

_________________
leonoverweel.com


Mon Oct 18, 2010 1:36 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.