I have the greatest admiration for anyone who can make sense of complex if statements. Even more so when the variables have obscure names like S0, S1 etc.
The first step to getting complex logic to work is writing it in a way that it can easily be understood.
Also the inverse (the ELSE part) of "if (S1 | S2 | S3 etc)" may not be at all what you expect and may in fact be hard to define.
I am familiar with some of them on "smiley micros" book and i was wondering if i could use them with arduino like the shift and hexadecimal strings which are quite handy. However, my sensors are spread on different ports, so I am not sure how I could assign this?
Later on, or most likelly tomorrow Ill have a look at the code again, in special the part that allows it to go straight.
Ill add proper comments (I removed most of them as I fiddled with the code) and a description of the hardware to make it a bit clearer along with photos.
Just as a side note:
i am currently using many sensors for the simple reason that I can not properly follow the line, so as to make sure there is only one sensor behind the line, as one of the conditions if for it to stop if both sensors are either 1 or 0, ideally if I am able to get it going straight I may need as little as only 3 sensors.
Regarding the digital inputs:
I am using an LCD to display the sensor values and what part of the code is being executed
As promised here is a description of the required circuit.
This is the track. Its quite large as it can be seen on the image. About 2x2m
The line is 4cm thick.
The photos show the dimensions of the white space. Upon reaching this it should stop.
These photos show my own design. It is composed of an Atmega 328 Running arduino. Supply voltage is 6V from 4xAA and I use a boost regulator to have a regulated 9V supply to power the sensors and the microcontroller. Motors are powered from the 4xAA unregulated 6V supply using PWM trough a mosfet each, with a relay currently being used to brake them.
I need to avoid using the brake as much as possible as the gearboxes on this are very week and often rip the gears apart. We are free to chose our own design but not the base and motors/gears.
This photo shows my sensor configuration with the 10 sensors. The 2 sensors on the middle are S4 and S5 and are positioned such that they should always stay under the black like
here is the last photo, showing the thing powered. My line, however is a bith thicker than the 4cm and hence 3 sensors are under it. The combination is invalid. I get this a lot when I define the combinations and its quite a problem. Any advice?
Anyway the main issue is that the microcontroller seems to take way too long to compensate when one sensor gets out of the line. perhaps its inertia or something so it goes three or more sensors out before it actually compensates, which makes the loop become very unstable until it reaches the end sensor, where the brakes are applied. I want to avoid applying the brakes as these gears are very fragil and I already damaged 2 sets of them.
The code is also attached here. This is only to go straight and has all the comends I could remember to put in there, so (I believe) it should be fairly easy to read when looking at the photos showing the sensors and the LCD readings. Any advice is welcome!
So you have a line of sensors across the front of your bot and moment to moment you have it check and correct based on a few exact sensor matches.
What does it do after it stops? The code still runs so I guess you move it back over the line and it goes?
If the sensor bar is tilted across the line, more sensors will be over it (unless you really have the sensors UNDER the line?) which is information that the sensor bar is not perpendicular to the line.
When you get to a bend in the line, are you prepared for that?
I mentioned before about setting bits in a variable and how the bits can work like pictures.
If you set 10 bits in an unsigned int to represent the row of sensors with 1 being over the line and 0 being off the line then:
your perfect straight ahead would be binary 0000110000 = 48 decimal.
anything less than 48 means the line is to the left, anything more means the line is to the right.
if more than 2 sensors show line ( 1 ) then the sensor bar is at an angle to the line, but may be with 2 sensors
If you keep some kind of history or status of what the bot is doing (turning, going straight, off to left or right) then it may help get more sense out of further data.
Using a bit picture you can track the bot going from line to one side and approach the center where a steering correction can be made based on how fast (many steps?) the sensor pattern moved and how many sensors are over the line (a bit crude given sensor spacing but geometrically effective).
Instead of tying immediate movement to every combination of reads, see about analyzing the pattern first. What happens when the line turns 90 degrees? The bits on one side will all be set. Suppose that your bot arrives at an angle to the line it is following? Now all the bits on one side may not be set but moment by moment the pattern changes, showing the line.
Try just running the bot straight and watching/collecting reads in different situations to get an idea of what you should see.
In different situations you may want the bot to respond to sensor patterns differently and for that you need to track status (process state, "what is determined to be going on"). Don't go to the trouble unless it helps, it's just a tool that you can use.
The white gap is the finish line. Its not required to do anything after that. But yes, if i was to move it forward into the black line again, yes it would continue for another run.
I was thinking... Could it be that because I am using PWM the motors are not allowed to pull a large current, hence making compensation slower? Should I, perhaps, use a variable PWM? Again i am afraid of breaking those butter gearboxes, but...
To be quite fair, this is a group design, with 3 other members of my group doing nothing. Altough i want to learn programming skills, right now I just want something that will finish the race, even if it gets there on last place. I am sure i can still get a high mark with the reports and the fact I am the only one working on this, which the teacher is aware, otherwise i am just risking myself to get behind in other subjects, which I am starting to already. This is too much or a workload for a single student.
Regarding the curve: The only way it detects a curve is if, as you say, at least 3 sensors on the edges detect black. in this case there is also a small delay, so it wont read the sensors and simply will continue to turn to avoid mistakes for a small period of time as I know that at a given speed it will take x time to correct that ammount of error, however I am using the brake, onde the end sensors sees black as to crtically correct the position and avoiding it to lose the line, as if this happens it will simply stop in the midle of the track and I get a penalty for pushing it to continue.
It would be so nice to find a way to ditch the lab parasites, wouldn't it?
Make sure to hand the prof the code and designs directly with only the name(s) of those who worked on them on the papers. Otherwise you will get the small credit if any. You are practicing tech, they practice "management".
I don't know what's slowing your code down unless the sensors are the I2C and getting 10 reads is the holdup.
You could time that by reading micros() into an unsigned long, running the scan and then subtracting the start micros() from micros() at the end (be aware that micros() is always to the nearest 4) in a test sketch.
I would wire sensors direct to pins and read the all sensors in 2 Port Reads. Just read 2 registers and combine them. It should take about a microsecond to give you 1 variable with a bit picture of all the sensors.
casemod:
The 2 sensors on the middle are S4 and S5 and are positioned such that they should always stay under the black like
Is it any wonder you can't figure it out with such crap names for things. What would be wrong with calling them sensorCentreRight and sensorCentreLeft and something equally appropriate for the other 8.
And why do you need 10, anyway. Wouldn't 4 be enough?
You want the ends of the line of sensors to extend well past the sides of a thick line so if/when the bar crosses the line at much of an angle they won't all show black.
GoForSmoke:
You want the ends of the line of sensors to extend well past the sides of a thick line so if/when the bar crosses the line at much of an angle they won't all show black.
So don't put the end sensors close to the middle sensors? Or maybe have 3 middle sensors of which two are always on the line and two outliers? I can't see an advantage to having more, and certainly not with the confusing way the OP seems to be managing them.
I'm still trying to figure out why you have more than 5 sensors. Or even 3. You can do line following pretty smoothly with 3 sensors (or 1 sensor if you don't mind it not being smooth).
Also, is there isn't any compensation from the fact motors don't behave identically, so you will always veer to one side over time unless you are very very lucky.
mirith:
I'm still trying to figure out why you have more than 5 sensors. Or even 3. You can do line following pretty smoothly with 3 sensors (or 1 sensor if you don't mind it not being smooth).
I made much the same point in the OPs other Thread on this subject - which is why they should be merged.
Why does this thread continue in "programming questions"?
The OP was about "if" statement , most of the comments on how to initialize and use "if" were ignored and we are way off in left field now. And even hardware suggestions ( less sensors) are brushed aside.
Back to "if".
Why is the initial if( all high or all low ) followed by all kinds of If / else tests if it is true?
Maybe because it is very unlikely it will ever be all high and when no sensors are active it will always be all low.
But you code does not make a distiction about that anyway.
I do not think your code is ready to actually run / operate your hardware. But that is your call.
Good luck.
GoForSmoke:
You want the ends of the line of sensors to extend well past the sides of a thick line so if/when the bar crosses the line at much of an angle they won't all show black.
So don't put the end sensors close to the middle sensors? Or maybe have 3 middle sensors of which two are always on the line and two outliers? I can't see an advantage to having more, and certainly not with the confusing way the OP seems to be managing them.
...R
Actually proportional sensors can tell more especially with some kind of history or running status on changes.
But there are successful line followers that use fewer sensors.
As to what the OP is doing, that's a different matter and a school project as well.
Why does this thread continue in "programming questions"?
Because no-one has asked for it to be moved, and I can't think of a better place for it to be?
**AWO for once, be honest, please.**Because YOU did not think it should be elsewhere - like "robotic".
NOW MAKE YOURSELF USEFUL AND DO SOMETHING ABOUT THIS POST .
"However I don't find it necessary for you to swear and get so angry about my terrible coding, isn't this a tool to help each other? Consequently your comments have helped me in no way."