You are here

Cypress Maker Lab - Barc lives! | Cypress Semiconductor

Cypress Maker Lab - Barc lives!

Last week I verified that my hardware was working. So it's time to connect the PSoC kit to the motors and breathe a little life into the old dog!  This requires a little re-wiring on the bread board. I started by connecting up the power and ground to both the motor driver and the PSoC kit, then the SLP (sleep) and FLT (fault) pins were wired to P2.3 and P2.0 respectively, and finally four new wires from pins P2.4 through P2.7 to the AIN and BIN inputs to the H-bridge.

It looks like this - I am afraid it is a little messy because I am running low on suitable length wires. Note that I am running power and ground from one side of the breadboard to the other because the GND and VDD pins happen to be on that side.

Oblique view of assembled Barc - PSoC-powered robot dog Top view of assembled Barc - PSoC-powered robot dog

I wantonly ignored the Adafuit suggestion that the battery pack should go on the "top shelf" because I wanted access to the breadboard (and to show off my design to people). This looks better but has two drawbacks. Firstly, the wires from the motors and the battery pack are not very long and so it is a bit of a squeeze to make the connections. Secondly, I do not really have an off switch and popping a battery out from the lower level might turn out to be a real pain. Time will tell!

Here is a handy table of the pins I chose. Note that I picked them purely for my convenience, the PSoC places zero constraints on which pins to use for GPIO or PWM outputs. Also, I am not using the FLT signal today - I will add a hardware check for over-voltage or over-heating problems in the driver in my next post.

Barc pin choices - PSoC-powered robot dog

 

Finally, it was time to make a real application and take the dog for a walk! Here is my first program. I used the pin component to access the GPIOs. I could have written directly to the pins from firmware but this is easier and, as you shall see, I will be modifying and reusing these pins components later.

Barc Pins - PSoC-powered robot dog

 

Here is the code. It just turns on the motors then repeatedly runs them forward and backward for a second.

#include "project.h"

int main(void)
{
    Pin_SLP_Write( 1 );         // Power on the motors

    for(;;)
    {
        /* Full speed ahead */
        Pin_AIN1_Write( 1 );
        Pin_AIN2_Write( 0 );
        Pin_BIN1_Write( 0 );
        Pin_BIN2_Write( 1 );

        CyDelay( 1000 );        // Run for 1s
     
        /* Full speed back */
        Pin_AIN1_Write( 0 );
        Pin_AIN2_Write( 1 );
        Pin_BIN1_Write( 1 );
        Pin_BIN2_Write( 0 );

        CyDelay( 1000 );        // Run for 1s
    }
}

 

This works great but highlighted an urgent new requirement. I need a way to turn it all off or I will be chasing the dog around the room every time I need to re-program him. There is a switch called SW1 on P2.2 and so I extended the design to use it to toggle SLP on/off. I used an input pin this time and configured it in resistive pull-up mode so that it would read a high when the switch is not pressed and low when pressed. I also changed the Pin_SLP to have a HW connection and put an inverter between the pair. This means that the motors only run when I press the switch - and I did not have to modify the C code at all.

Barc Pins - Using a switch to control the motors - PSoC-powered robot dog

 

This is much better but the alert amongst you will have already foreseen a problem. A dog that requires its owner to hold a button down on his head before he'll do anything is a pretty useless pet. Maybe I should be making a robot goldfish?

Never mind, I just added a toggle flip-flop and solved the problem. I left the not gate in so that the initial state is off. With this design I can turn him on and off with the press of the switch and I have the best behaved dog in America.

Barc pins - toggling the motors with a flip-flop - PSoC-powered robot dog

 

Here is a video of it all working. Note that I am powering the PSoC and the motors from the USB cable instead of batteries (Barc does not like his "USB leash"). It is a little underpowered but a convenient way to iteratively program and test the designs.

Barc Moves.MOV

 

I was feeling pretty good about the project at this point and so decided to add the ability to change speed and direction with a little more granularity. Time for a PWM. I dragged a PWM into the project and attached a clock. I figured it is not good to drive motors with a really fast signal so I changed the clock frequency to 50kHz. Luckily PSoC Creator figures out how to generate that clock signal and all I have to do is pick the speed (if you really care, I looked in the Clocks page of the Resources file and it puts a divider of 480 onto the 24MHz default MASTER_CLK source). With an 8-bit PWM and a period of 100, strictly 99 because it is a zero-based counter, that gives the motors an input frequency of 500Hz - lush! Here is the PWM setup. I named it "PWM_Speed" and am using both channels. I set the period to 99 and the initial compare values to zero.

PSoC PWM Customizer dialog

 

With a less-than comparison that means the PWM signals are initially off, as you can see from the flat waveform in the dialog, which seems the sensible thing to do! Lastly I enabled the hardware connections in the AIN1 and BIN2 pins and connected the PWM outputs. The other two pins are still firmware controlled and I just set them low in main().

PWM driving Barc motors - PSoC-powered robot dog

 

Lastly I changed the code so that Barc chases his tail. He starts off in a really tight left turn, then switches to the right, and  then runs in a wider circle for a while. That shows how to control his rate of turn by applying different compare values to each PWM channel.

#include "project.h"

int main(void)
{  
    /* Hold the non-PWM pins low */
    Pin_AIN2_Write( 0 );
    Pin_BIN1_Write( 0 );
     
    Pin_SLP_Write( 1 );         // Power on the motors
 
    PWM_Speed_Start();          // Start the PWM
  
    for(;;)
    {
        /* Spin left fast */
        PWM_Speed_WriteCompare1( 0 );
        PWM_Speed_WriteCompare2( 100 );

        CyDelay( 1000 );
      
        /* Spin right fast */
        PWM_Speed_WriteCompare1( 100 );
        PWM_Speed_WriteCompare2( 0 ) ; 

        CyDelay( 1000 );
     
        /* Run in a circle */
        PWM_Speed_WriteCompare1( 80 );
        PWM_Speed_WriteCompare2( 50 );

        CyDelay( 5000 );
    }
}

I can now make Barc run around at any speed I like and in any direction. Except backwards. So I will add the ability to reverse next time, and figure out how to use that fault pin.

 

Comments

douglasswain_2494316's picture

Well, see? Issue was in the code. Good that you managed to change the way the tail works. You need to apply more diverse contrast values with each PWM channel. Should you ever have code problem, order a paper code solution here. One of programmers will help you.

csjoshi.editsoftdigital_2593831's picture

More About the Concept of an Afterlife: The only part of you that could in any way survive into an afterlife would have to be an immaterial part of you since when you snuff it, your material remains go nowhere. But any immaterial part of you would have no sensory apparatus so how could you enjoy an afterlife when you couldn't experience it? On the other hand, if your life is but a computer simulation, then equally so could your afterlife be another simulation. One software program ends (death) and another begins (afterlife).

 

Colin Cooper's picture

Tissot is known for planning lively and rich timepieces. Its hustling uk replica watches gathering got popularity over the entire world. The T-Trend gathering likewise draws out the pattern of the time through the plans of the timepieces.

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.