Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:motor:dc [2015/11/11 13:55] heikopikneren:examples:motor:dc [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-~~PB~~+<pagebreak>
 ====== DC motor ====== ====== DC motor ======
  
-//Necessary knowledge: [HW] [[en:hardware:homelab:motor]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:module:motor]], [LIB] [[en:software:homelab:library:delay]]//+//Necessary knowledge:  
 +[HW] [[en:hardware:homelab:digi]], [HW] [[en:hardware:homelab:combo]], 
 +[AVR] [[en:avr:timers]], [AVR] [[en:avr:adc]],  
 +[LIB] [[en:software:homelab:library:module:motor]], [LIB] [[en:software:homelab:library:adc]]//
  
 ===== Theory ===== ===== Theory =====
Line 58: Line 61:
 |  0        1        -        |  +        | The motor revolves in direction 2 | |  0        1        -        |  +        | The motor revolves in direction 2 |
  
-DC motors can be controlled by manipulating directly corresponding driver pins with microcontroller. Thoughspecial functions for driving the motor are in the library of the HomeLab.+For each motor that is connected to the H-bridge is operated by two of the digital output of the microcontroller. The motor speed is is controlled by timers that generate a continuous PWM signals to the H-bridgethe direction of rotation of the motor is controlled to the second terminal. Motor speed is controlled a relative values from 0 to 255, where 0 means that the motor is standing and 255 is the maximum moving speed of the motor. The following code describes a function’s, which are described in the HomeLab II (ATmega2561) library to control DC motors.
  
 <code c> <code c>
-// The setup of the pins driving pins.+// The setup of the pins driving pins
 static pin dcmotor_pins[4][2] = static pin dcmotor_pins[4][2] =
 { {
Line 69: Line 72:
  { PIN(D, 5), PIN(D, 4) }  { PIN(D, 5), PIN(D, 4) }
 }; };
- +static int motorindex[4][2] = 
-// Allowing the control of the chosen DC motor. +
-void dcmotor_init(unsigned char index) + { 0, 1 }, 
-{ + { 2, 3 }, 
 + { 4, 5 }, 
 + { 6, 7 } 
 +}; 
 +// Initializing a PWM to chosen motor 
 +void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler
 +{ 
 +  unsigned char i, pwm; 
 + 
  pin_setup_output(dcmotor_pins[index][0]);  pin_setup_output(dcmotor_pins[index][0]);
  pin_setup_output(dcmotor_pins[index][1]);  pin_setup_output(dcmotor_pins[index][1]);
-} 
    
-// Determining the operation and the direction of the chosen DC motor. + motor[index] = 1; 
-void dcmotor_drive(unsigned char index, signed char direction) +  pwm = PWMDEFAULT; 
-{  +  
- pin_set_to(dcmotor_pins[index][0], direction < 0);  +  // Starting all channels 
- pin_set_to(dcmotor_pins[index][1]direction > 0);+  for(i=0 ; i<CHMAX ; i++) 
 +  { 
 +      // PWM state variable initialization 
 +      compare[i] = pwm;            
 +      compbuff[i] = pwm;           
 +  } 
 +  
 +  // Starting Timer 2 to normal mode 
 +  timer2_init_normal(prescaler); 
 +  // Allow Timer 2 interrupt 
 +  timer2_overflow_interrupt_enable(true); 
 +  
 +  // Enable global interrupts 
 +  sei(); 
 +
 +// Generating a PWM for chosen motor 
 +void dcmotor_drive_pwm(unsigned char index, signed char direction,  
 +unsigned char speed)  
 +
 + if(direction == -1) 
 +
 + compbuff[motorindex[index][0]] = 0x00
 + compbuff[motorindex[index][1]] = speed; 
 +
 + if(direction == 1) 
 +
 + compbuff[motorindex[index][0]] = speed; 
 + compbuff[motorindex[index][1]] = 0x00; 
 +
 } }
 </code> </code>
  
-With the array //dcmotor_pins// in the library, the controlling pins of four motor-controllers are determined. Before controlling the motors, function //dcmotor_init// with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. For controlling is the function //dcmotor_drive//with it the negative //direction// parameter is used to give to the motor one revolving direction and other direction with positive parameter, and 0 if the motor is stopped.+The controlling pins of four motor-controllers are determined with the array dcmotor_pins in the library. Before controlling the motors, function dcmotor_drive_pwm_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. It should also set the timer prescaler, for HomeLab II timer2_prescale and for HomeLab III timer_prescale, which determines the frequency of the PWM signal. In case of HomeLab II, as the program does not have functions which are using timer, it is appropriate for the value TIMER2_NO_PRESCALE. When for example an ultrasound sensor are usedthen should be chosen TIMER2_PRESCALE 8, otherwise the controller performance may not be sufficient and the sensor readings may be corrupted. This is not applying in the HomeLab III. Higher values of the prescaler are not recommended, because it makes the motor rotation intermittent, and generates vibration. 
 + 
 +Function dcmotor_drive_pwm is for control motor speed. This function need three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 other direction and 0 for stop and thirdly, the speed range of 0-255. The speed value is not linked to a specific rotational speed, it is the relative value between minimal and maximal motor speed. Motor actual speed depends on the motor type, load and the supply voltage. Motor speed accuracy is 8-bits, which means that the minimum control accuracy is 1/255 of the maximum engine speed.
  
-The following is an example program which controls first and second DC motor so that they alter their revolving direction after every second. The speed could be controlled if one controlling pin were modulated with PWM signal.+The following is an example program which controls first and second DC motor so that first motor rotates half of the speed and the second motor speed is controlled by a potentiometer.
  
 <code c> <code c>
en/examples/motor/dc.1447250124.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0