Jump to content
  • 0

gustavo345

Question

Recommended Posts

Hi, must be your lucky day - I'll take it nice and slow.

 

First of - how PWM works.

---------------------------------

If you connect a motor to a power supply it will spin at full speed/power.

 

If you switch the power supply on for 5 seconds and off for 5 seconds, it will spend half the time at full power, and half the time at zero power, and it will have an average power of 50%.

 

If you switch the power supply on for 1 seconds and off for 9 seconds, it will have an average power of 10%

 

The really important bit often overlooked is that the motor has full power while it is switched on, and so has nearly full torque. If you were to run it at a lower voltage the motor won't have nearly as much torque, making speed control impossible.

 

However, with a cycle time of 10 seconds, unless it is a very big and heavy motor (which takes a long time to spin up and down) it won't be too smooth. It is usual to use a PWM frequency of about 100Hz or 200HZ or so. (An aside - this is why cordless drills "hum" at low speed).

 

PWM is efficient, as the power is always on/always off - even with huge motors the electronics stay colde.If you "lower the voltage" using a linear voltage regulator the unused power is dissipated by the power supply, making it hot and a waste of power.

 

What is a H-Bridge?

================

When you connect a battery to the wires of a DC motor it will spin one way, when you flip the motor over it will spin the other.

 

A H-bridge is an arrangement of power transistors acting as switches that allows the "flipping" of connections without physically changing anything, The arrangement looks like an ''H', hence the name,

 

Usually the H-bridge is powered from a different power source from the control signals - this is why the PMOD-HB3 has the GND and VM connections - these supply power to the motor.

 

Putting them both together

=====================

Using a PWM with a H-Bridge allows you to control both the power and direction of the motor. It is important to note that we can't directly control the speed, as the speed of the motor depends on the load it is under.

 

Next - getting the FPGA design started.

Link to comment
Share on other sites

wow you are the best, but now I mark another mistake ........ this is the error
 
 
ERROR: MapLib: 30 - LOC constraint on btn0 G12 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on btn1 C11 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on btn2 M4 is invalid: No such site on the
    device. To bypass this mistake set the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on Btn3 A7 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on clk B8 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib 30 - LOC constraint on JA0 B 2 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on ja1 A3 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on LED0 M5 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on led1 M11 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
ERROR: MapLib: 30 - LOC constraint on sw1 L3 is invalid: No such site on the
    device. To bypass this mistake September the environment variable 'XIL_MAP_LOCWARN'.
Link to comment
Share on other sites

So all that is left is to get rid of the Whirrrr, Weee, Whirr, Weee....

 

To do this we speed up the PWM frequency from 0.5Hz to 50Hz. This is pretty easy to do, just remove the last two digits of the counter constants:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
 
entity pwm_test is
    Port ( clk       : in  STD_LOGIC;
           sw0, sw1  : in  STD_LOGIC;
           btn0, btn1, btn2, btn3 : in STD_LOGIC;
           ja0, ja1  : out STD_LOGIC;
           led0,led1 : out STD_LOGIC
         );
end pwm_test;
 
architecture Behavioral of pwm_test is
   signal pwm            : std_logic := '0';
   signal counter        : unsigned(19 downto 0) := (others => '0');
begin
   -- show the switches on the leds
   led0 <= sw0;
   led1 <= sw1 and pwm;
   -- Drive the pmod-hb3 signals
   ja0  <= sw0;
   ja1  <= sw1 and pwm;
 
clk_proc: process(clk)
   begin
      if rising_edge(clk) then
         pwm <= '0';
         
         if counter < 250000 and btn0 = '1' then
            pwm <= '1';
         end if;
         
         if counter < 500000 and btn1 = '1' then
            pwm <= '1';
         end if;
 
         if counter < 750000 and btn2 = '1' then
            pwm <= '1';
         end if;
 
         if btn3 = '1' then
            pwm <= '1';
         end if;
         
         if counter = 999999 then
            counter <= (others => '0');
         else
            counter <= counter+1;
         end if;
      end if;
   end process;
end Behavioral;

(Note that the size of counter has been reduced to 20 bits, as we only need to count to 999,999)

 

And finished - a PWM controller that will drive a motor at four different power levels, in two directions, depending on the switches and buttons.(and me most probably in trouble with many tutors!)

Link to comment
Share on other sites

Hi Gustavo345,

 

Actually posting the error text that is causing the build to stop would be more helpful than installing an older version of ISE, so I/we don't have to guess at your problem is and can guide you appropriately.Having said that, here is my guess at your problem...

 

One of the most common issues that people have with their first project is they select either the wrong FPGA or package type - it is not like with CPUs where the program will run on any CPU of the same processor architecture.

 

The top level of your project (in the "Hierarch" window) should show  "xc3s250e-5cp132" for a Basys2-250 or "xc3s100e-5cp132" for the Basys2-100. If ti doesn't, right-click on it, choose properties, and correct the wrong setting.

 

Mike

 

PS. I think I might actually have the wrong speed grade in there (-5 vs -4), but that won't really matter for such a simple project,..

Link to comment
Share on other sites

Great, Manual PWM isn't that effective. So now lets generate the PWM signal using the FPGA.

 

To make things visible, we will drive the signal at about 0.5Hz, and at 50%. To do this we'll need a clock. 

 

So add the Basys2's clock signal to your constraints file:

 

  NET "clk" LOC = "B8"; # Bank = 0, Signal name = MCLK
 
And we now need to update the code to use this clock. In this case we run a counter, which counts to 100,000,000 (which takes two seconds at 50MHz). This is then used to generate a signal called 'pwm'. This can then be 'AND'ed with the enable switch to drive it at half power when it is switched on.
 
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
 
entity pwm_test is
    Port ( clk       : in  STD_LOGIC;
           sw0, sw1  : in  STD_LOGIC;
           ja0, ja1  : out STD_LOGIC;
           led0,led1 : out STD_LOGIC
         );
end pwm_test;
 
architecture Behavioral of pwm_test is
   signal pwm            : std_logic := '0';
   signal counter        : unsigned(26 downto 0) := (others => '0');
begin
   -- show the switches on the leds
   led0 <= sw0;
   led1 <= sw1 and pwm;
   -- Drive the pmod-hb3 signals
   ja0  <= sw0;
   ja1  <= sw1 and pwm;
 
clk_proc: process(clk)
   begin
      if rising_edge(clk) then
         pwm <= '0';
         if counter < 50000000 then
            pwm <= '1';
         end if;
         
         if counter = 99999999 then
            counter <= (others => '0');
         else
            counter <= counter+1;
         end if;
      end if;
   end process;
end Behavioral;
 
Great - because of the 0.5Hz PWM frequency the motor should go Whirrrr... weeee, Whirrrr... weeee - On for a second, off for a second.
 
Next step, have more than one PWM setting....
 
Link to comment
Share on other sites

just to close the thread off - I've been communicating with email, and issues with the design that Gustavo345 faced were...

 

1) The project was not set to the correct device, so it wouldn't build correctly.

 

2) The last change in the code was to speed things to a sensible PWM frequency. The counter was reduced to 20 bits (0 -> 1,048,575), but in Gustavo345's design the constants were still left at 9,999,999,  5,000,000 and so on. As all the comparisons became constant.everything gets optimised away.

 

Cheers

 

Mike

Link to comment
Share on other sites

Hey guys, is there any way you can control motor in both ways just with signal. I have done motor control both direction using switches form nexys 3 board. But I need to control motor automatic. we are making a ball balancing system using motor. help will be appreciated. suggestion on C - language , we are using SDK?

Link to comment
Share on other sites

Hello Gustavo, 

 

Mike is correct, your question is extremely vague. It looks like you are trying to find a place to start?

If so, we had a design project that used stepper motors-- http://www.digilentinc.com/showcase/projects/shootinggallery.cfm

The project uses a Nexys2 and uses stepper motors for the targets. 

That may be helpful as a starting point.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...