Asked — Edited
Resolved Resolved by DJ Sures!

H-Bridge Dc Motor Movement Script

I'm looking to see if I'm scripting correct.

I'm doing the power train to a BB-9. The way the drive is set up is it uses multiple DC planetary motors each with a single motor h-bridge. The motors are dome spin, dome tilt, ball rolling and motor tilt, which causes it to turn and a spin which lets it spin in place to face a different direction.

Since I can only add 1 h-bridge control, I want to use that for the ball rolling and motor tilt, so the other motor controls I'm creating scripts.

Am I doing this correct? Or is there an easier way?

Dome spin is pretty simple since it's a 360 degrees with no stops.


:start
goto(left)
goto(Start)

: Left
Set(d4, on)
Set(d5, off)
Return()
Halt()

I have a second script to spin it right, a third script to stop the spin and a fourth script that centers the dome to a specified ADC reading. If the spinning script is running, running the stop or centering script won't work unless I add ScriptStop to their script, is this correct?


ControlCommand("Script Manager", ScriptStop, "Dome Spin Right")
ControlCommand("Script Manager", ScriptStop, "Dome Spin Left")
:start
goto( Stop)
goto(Start)

: Stop
Set(d4, off)
Set(d5, off)
Halt()

My last question deals with the dome tilting front or back, it does have a range it can go. From my ADC readings, 93 is center, 139 and 38 are the max ranges front and back. Here is one of the tilt direction scripts, using this, will it stop correctly when it reaches the max 139?


$adcSpecified = 139
:start

$adcCurrent = GetADC(adc2)

if ($adcCurrent < $adcSpecified)
goto( front )
endif
if ($adcCurrent = $adcspecified)
goto( Stop)
endif
goto(Start)

: Front
Set(d1, on)
Set(d2, off)
Return()

: Stop
Set(d1, off)
Set(d2, off)
Halt()

This will also have 4 scripts, front, back, stop and center. Also I have 5v going to the EZ-B, D0 and D3 connect to the vcc and ground to the h-bridge and the signal from them I have it connecting to the PWM inputs of the H-Bridges which will be a question for later!

Thanks I added a video of the dome spin test.


ARC Pro

Upgrade to ARC Pro

Take control of your robot's destiny by subscribing to Synthiam ARC Pro, and watch it evolve into a versatile and responsive machine.

#2  

The custom command would work for movement, but I can only add one of them, I still need to use scripts for dome control, which is in addition to movement, that's why the scripts.

I have for movement- forward, reverse, left and right, which the h-bridge control will work for movement.

I also have in addition to movement- Dome spin, dome tilt, and body spin, which is the scripting I'm working on first.

The movement will be last because I still need a DC motor that is out of stock, once I get that I can assemble the drive completely.

Thanks.

PRO
Synthiam
#5  

okay awesome - as you continue reading with my code review, i highly recommend using Blockly and watching episodes of The Robot Program. Read the programs you write like a book or food recipe. If you have ever cooked a meal from a food recipe, programming is EXACTLY the same, serious.

A recipe contains step by step instructions to do something. So is programming. If you do randomly skip around the instructions, the recipe won't turn out. The same is with programming.

Read each line of your program and think about it for a second. If you were to ask someone to get you a glass of water, you're not going to ask them to get the water BEFORE the glass. Does that make sense? The order you do things is important.

I have some recommendations with this...


:start
goto(left)
goto(Start)

: Left
Set(d4, on)
Set(d5, off)
Return()
Halt()

It appears the code jumps around a lot, which uses a significant amount of processor resources. Also, the logic is really difficult to understand. The HALT() is never actually executed, because Return() is called before it. Also, the goto(Start) and goto(left) is a loop within a loop. It can be rewritten like this...


:start
Set(d4, on)
Set(d5, off)
goto(start)

I highly recommend to learn programming by watching The Robot Program and using Blockly. Here is the code created in Blockly, which visually shows you what a loop looks like...

User-inserted image

Now, I'm struggling with this piece of a code, bare with me. It looks similar to the first piece of code you posted, but there's ControlCommand() and a partial loop within a loop.


ControlCommand(&quot;Script Manager&quot;, ScriptStop, &quot;Dome Spin Right&quot;)
ControlCommand(&quot;Script Manager&quot;, ScriptStop, &quot;Dome Spin Left&quot;)
:start
goto( Stop)
goto(Start)

: Stop
Set(d4, off)
Set(d5, off)
Halt()

I'm not sure what it's doing, but i recommend writing it like this by removing the loops, because the loop didn't actually do anything in the original snippet, due to the Halt()


ControlCommand(&quot;Script Manager&quot;, ScriptStop, &quot;Dome Spin Right&quot;)
ControlCommand(&quot;Script Manager&quot;, ScriptStop, &quot;Dome Spin Left&quot;)
Set(d4, off)
Set(d5, off)

Lastly, let's take a look at this - which is actually pretty close to making sense and almost works, but not quite...


$adcSpecified = 139
:start

$adcCurrent = GetADC(adc2)

if ($adcCurrent &lt; $adcSpecified)
goto( front )
endif
if ($adcCurrent = $adcspecified)
goto( Stop)
endif
goto(Start)

: Front
Set(d1, on)
Set(d2, off)
Return()

: Stop
Set(d1, off)
Set(d2, off)
Halt()

Here is what i recommend, by using a RANGE for the ADC, since ADC is never perfect. Also, there are a lot of "goto" for no reason. So i changed the code to be more effective...


$adcSpecified = 139
:start

$adcCurrent = GetADC(adc2)

# If the adc value is within a range, stop the script
if ($adcCurrent &gt; $adcSpecified - 2 and $adcCurrent &lt; $adcSpecified + 2)

  Set(d1, off)
  Set(d2, off)

  halt()

elseif ($adcCurrent &lt; $adcSpecified)

  Set(d1, on)
  Set(d2, off)

elseif ($adcCurrent &gt; $adcSpecified)

  # I am only guessing here, but you want to reverse the direction?
  Set(d1, off)
  Set(d2, on)

endif

goto(start)

#6  

Great, thanks!

I had it more complicated and using up more resources than needed.

PRO
Synthiam
#7  

If you’d like, feel free to post the project in this thread and I can take a peak at it? See if there’s any pointers I have

#8  

OK, I'll attach it. I tested it and it works great, sort of.

Sometimes the head tilt goes past the stop point and slams into the frame. I have to turn the PWM to zero to stop it. It's intermittent. The other is the dome centering, it jerks back and forth and doesn't stop.

I have the h-Bridge loaded for forward and revese and it works fine, but I think I'll have to replace it with custom movement panel. Forward and reverse are just that, but the left and right, actually tilts the whole body and uses a pot to determine limits on left right and centering, like the dome tilt.

I'm waiting on a motor so I can finish assemble and then do the body tilt.

BB-9eEZB.EZB

Thanks, Dan