Go Down

Topic: Motorshield and electromagnet ? (Read 1 time) previous topic - next topic

fliggygeek

Is it possible for you to try using digitalWrite(<pin>,HIGH) instead of outputting PWM? I would like to see of this changes anything as far as output power goes.
"Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent di

kuno86


jraskell

Your control for receiving input and controlling the car is a bit... wonky (no offense intended) and overcomplicated.  I can't see anything that will obviously cause the problems you are seeing, but a more simplified control scheme will not only make it easier to debug, but make it less likely that there will be a problem to debug from the start.

To that end, don't even bother checking for invalid characters.  Since you only perform actions based on valid characters, there's little benefit being gained by doing so.

Only set IO points when you receive a command packet.  There's no benefit to repeatedly setting IO points to the same values over and over and over.  Again, there shouldn't technically be anything wrong with doing so, but it can make diagnosing and debugging problems more of a headache than it already is.

So, as sort of some pseudo-code to go by:
Code: [Select]

if(packetavailable){
  receive packet
  perform drive control
  perform steering control
}


You can also easily reduce your drive control down to a single byte and your steering control down to a single byte.

Steering control byte would just be a three state control.
val1 = straight ahead
val2 = left
val3 = right

Drive control would only be slightly more complicated.  Using a signed char variable, you have a range of -127 to 127. if the val is less than 0, set you direction to reverse, otherwise set it to forward.  Take the absolute value of the variable, multiply it by 2 (then add 1 if you absolutely want the max speed possible, but the reality is the difference between 254 and 255 is likely negligible), and that's your speed.

kuno86

Thanks a lot jraskell, i deleted the in fact useless check-part and moved the controls into the the "if(packetsize)".

The stuttering is also solved now, it was propably caused by:
Code: [Select]
  if(packetSize==0)
    {
    digitalWrite(ledPin,LOW);
    analogWrite(E1,0);             
    analogWrite(E2,0);           
    }


this always activates when there are no packets, which is always the case right after a packet.
So i removed the two "analogWrite()" and it was gone :D



About making the controls bitwise, i had that in mind before, but i couldn't get the RAW-Data from UDP...

jraskell

Good to hear.  I never even noticed those lines from your original code, but that is clearly what was causing your problems.

As another side note, you may want to look at the difference between & and && as well as | and ||.  For what you're doing in your comparisons, the bitwise & and | operators are still accomplishing your goal, but you really should be using the boolean && and || operators in comparisons.

Go Up