View unanswered posts | View active topics It is currently Mon Oct 20, 2014 7:44 pm






Reply to topic  [ 8 posts ] 
how can i use 2 light sensors as pid control 
Author Message
Rookie

Joined: Sat Jan 21, 2012 6:10 pm
Posts: 3
Post how can i use 2 light sensors as pid control
how can i use 2 light sensors with pid control cause I've read through the link http://www.inpharmix.com/jps/​PID_Controller_For_Lego_Mindsto​rms_Robots.html
it works with nxt-G with only one sensor but i would like to know how can i use robotc to do like this with 2 sensor like this link
http://www.youtube.com/watch?v=nQ6wsAL6w9Y
thanks for your advices


Sat Jan 21, 2012 6:15 pm
Profile
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: how can i use 2 light sensors as pid control
PID doesn't really need a second light sensor, and I doubt it would improve your results a lot. What I would do is use one sensor for the line following and use the other one for something else (detecting colored dots on the line, maybe?)

_________________
leonoverweel.com


Sat Jan 21, 2012 7:28 pm
Profile WWW
Rookie

Joined: Sat Jan 21, 2012 6:10 pm
Posts: 3
Post Re: how can i use 2 light sensors as pid control
if 2 sensors do not work for pid could guide me to do for more sensors such as 4 sensors and i think only one sensors less smooth than more sensors but i do not know how to build some code to control nxt


Sat Jan 21, 2012 9:15 pm
Profile
Expert

Joined: Wed Jun 30, 2010 7:15 am
Posts: 181
Post Re: how can i use 2 light sensors as pid control
The smoothness of a PID controller really depends on how it's tuned. Here's some code that I wrote for my line follower, but it only uses one sensor. The result is very smooth though.
Code:
#pragma config(Sensor, S2,     MS,                  sensorNone) //Magnetic Field Sensor
#pragma config(Sensor, S3,     LS,                  sensorLightActive) //Light Sensor
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "drivers/HTMAG-driver.h"

task main(){
  float offset; //variables
  float Kp = 2;
  float Ki = 0.001;
  float Kd = 15;
  float Tp = 60;
  float error;
  float integral;
  float derivative;
  float lastError;
  float turn;
  float powerB;
  float powerC;
  int tooLow;
  int tooHigh;

  wait10Msec(200);
  PlaySound(soundBlip);
  tooLow = SensorValue(LS); //get black light value
  wait10Msec(200);
  tooHigh = SensorValue(LS); //get white light value
  PlaySound(soundBlip);
  offset = ((tooLow+tooHigh)/2)+2; //get light sensor average for offset
  wait10Msec(100);
  HTMAGstartCal(MS);
 
  nMotorPIDSpeedCtrl[motorB] = mtrNoReg; //turn motor B's PID off
  nMotorPIDSpeedCtrl[motorC] = mtrNoReg; //turn motor C's PID off

  while(true){ //start main PID loop
    if(HTMAGreadVal(MS) <= -20 || HTMAGreadVal(MS) >= 20){ //pauseing cycle
      ClearTimer(T1); //start timing
      while(HTMAGreadVal(MS) <= -20 || HTMAGreadVal(MS) >= 20){ //main pausing loop
        if(time100(T1) >= 40){
          StopAllTasks(); //Abort if timer gets over 4 seconds
        }
        motor[motorB] = 0; //turn motor B off
        motor[motorC] = 0; //turn motor C off
      }
    }
    error = SensorValue(LS)-offset; //calculate difference between offset and light value
    integral = integral+error; //calculate integral (remember the past)
    derivative = error-lastError; //calculate derviative (predict the future)
    turn = (Kp*error)+(Ki*integral)+(Kd*derivative); //summarize PID stuff
     powerB = Tp-turn; //calculate power for motor B
     powerC = Tp+turn; //calculate power for motor C
    motor[motorB] = -powerB; //set motor B to power B in reverse
    motor[motorC] = -powerC; //set motor C to power C in reverse
    lastError = error; //update memory
    wait1Msec(5);
  }
}

It has some stuff you don't need, but you can change it to fit your needs. I wrote this using the tutorial page you also talked about in your first post.

_________________
leonoverweel.com


Sat Jan 21, 2012 10:06 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: how can i use 2 light sensors as pid control
In theory, if you have more light sensors, you will be able to move faster and smoother without losing the line. If you have just one light sensors and you go too fast and overshoot a curve a lot, you may not find the line again, whereas if you have 7 light sensors spanning a wider width, you will know you are about to lose the line so you can slow way down to make the turn. There are may threads in forums that talk about this subject. You may want to search for them.


Sun Jan 22, 2012 12:38 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post Re: how can i use 2 light sensors as pid control
You may find this an interesting read: http://www.fll-freak.com/misc/01-jgray_report.pdf

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 22, 2012 3:07 am
Profile WWW
Rookie

Joined: Sat Jan 21, 2012 6:10 pm
Posts: 3
Post Re: how can i use 2 light sensors as pid control
i read some related topic but still do not understand how to config code that work with pid
if i want to use 2 light according to video link above could anyone guide me how to config code with robotc

thanks for your suggestion


Mon Jan 23, 2012 5:45 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: how can i use 2 light sensors as pid control
The following thread explains how to use 3 light sensors to do PID line following. Although the thread is in Vex/Cortex forum but it should be equally applicable to NXT Mindstorms.
viewtopic.php?f=11&t=3988


Mon Jan 23, 2012 2:38 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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.