View unanswered posts | View active topics It is currently Sun Nov 17, 2019 8:11 am






Reply to topic  [ 4 posts ] 
Coding Moving Solar Panel with Light Sensors 
Author Message
Rookie

Joined: Thu Nov 20, 2014 2:34 pm
Posts: 2
Post Coding Moving Solar Panel with Light Sensors
Before I begin, I am using RobotC 3.6.2.

Hello all, first time poster here. Me and a partner are working on a project for a competition and would like a bit of help. We have tirelessly worked on a code to make the setup work. We have it set up with a VEX 269 Motor which spins a plate with 2 light sensors which detect the difference between each other to judge which way the motor spins.

Example:
Code:
 
if((SensorValue(leftLight)) > (SensorValue(rightLight)))                        
{
motor[motorMain] = -32;         
}


The plate can move 360 degrees, and because of that we would have a problem where the plate would want to go a full 360 and would constrict the wires and stress the motor. It has since been corrected (By use of a beam preventing the plate from moving 180 degrees), but now we have a new problem. You see, the plate has two light sensors, one on the left side and one on the right, it detects the difference between the two and moves the plate until both sensors have a set amount of light (In out case darkness must be less than 60). The problem here is that while finding the light the plate shakes a lot, I assume its from small differences in light yet while the main while statement's requirement is met it still shakes as though it is looking for light. I need to find a way to stop this shaking, and I'm not entirely sure how. Below is the full code any help at all with this is greatly appreciated.


Code:
#pragma config(Sensor, in7,    leftLight,      sensorReflection)
#pragma config(Sensor, in8,    rightLight,     sensorReflection)
#pragma config(Motor,  port1,           motorMain,     tmotorVex269, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main ()
{
while(true)
{         
while((SensorValue(leftLight)) + (SensorValue(rightLight))  > 60)      
{

if((SensorValue(leftLight)) > (SensorValue(rightLight)))                        
{
motor[motorMain] = -32;
}

if((SensorValue(leftLight)) < (SensorValue(rightLight)))                     
{
motor[motorMain] = 32;   
}

if((SensorValue(leftLight)) + (SensorValue(rightLight)) < 60)                  
{
motor[motorMain] = 0;
wait1Msec(1000);
}
motor[motorMain] = 0;
}                
motor[motorMain] = 0;
}          
}


Fri Nov 21, 2014 12:55 pm
Profile
Rookie

Joined: Wed Feb 24, 2010 11:51 pm
Posts: 31
Post Re: Coding Moving Solar Panel with Light Sensors
It sounds like what you want is proportional control.

Instead of always setting the motor power to +-32 based on thresholds, set the motor power based on the difference between the two light sensors.

Code:
int error = SensorValue(LeftLight) - SensorValue(RightLight);
float gain = 1.0;
motor[motorMain] = (int)(gain * error);


You might need to change gain to get the desired result without oscillating (shaking). If the motor always wants to run the wrong direction, then change the sign of error (use right - left), or use a negative gain.

Terry

_________________
Vex & FLL Coach and robotics instructor


Fri Nov 21, 2014 1:20 pm
Profile
Rookie

Joined: Thu Nov 20, 2014 2:34 pm
Posts: 2
Post Re: Coding Moving Solar Panel with Light Sensors
Thanks! With a bit of tinkering it works like a charm! I always wanted to try using differences to tell the motor how fast to go but never thought of using said difference as motor speed!


Fri Nov 21, 2014 2:39 pm
Profile
Rookie

Joined: Wed Feb 24, 2010 11:51 pm
Posts: 31
Post Re: Coding Moving Solar Panel with Light Sensors
theinsideoutcast wrote:
Thanks! With a bit of tinkering it works like a charm! I always wanted to try using differences to tell the motor how fast to go but never thought of using said difference as motor speed!

The technique of proportional control is the basis of all closed-loop controls. If you've heard of or seen "PID" used when talking about these things, what you've just written is the "P" part of that algorithm. For most applications, it gets the job done quite nicely. Only in the cases where P alone doesn't get it done, then you can look into using the I and/or D terms too. There's lots of information available online.

Terry

_________________
Vex & FLL Coach and robotics instructor


Fri Nov 21, 2014 5:40 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.