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.

 

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.

 

Blog: 

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.

本网站上的所有内容和材料均“按原样”提供。赛普拉斯半导体公司及其各个供应商对这些材料用于任何用途的适用性不作陈述,并且对关于这些材料的所有担保和条件概不负责,包括但不限于有关适销性、针对特定用途之适用性、权利和不侵犯任何第三方知识产权的所有暗示担保和条件。赛普拉斯半导体公司不授予任何明示或暗示的许可(无论是以默许方式或是任何其他方式)。使用本网站上的信息可能需要第三方的许可,或赛普拉斯半导体公司的许可。

本网站上的内容可能包含或必须遵守关于使用的特定准则或限制。所有帖子和使用本网站上的内容都必须遵守本网站的条款与条件;使用这些内容的第三方必须同意遵守任何限制或准则,并遵守本网站的条款与条件。赛普拉斯半导体公司及其供应商保留随时对内容和材料、产品、计划和服务进行纠正、删除、修改、增强、改进或其他变更,或者移动或终止任何内容、产品、计划或服务的权利,恕不另行通知。