Interfacing With DC Pololu Motor
Interfacing With DC Pololu Motor
Port B_6
Port B_7
Controller
Speed:
• The speed can be measured by counting the rising and falling edges of the
outputs A and B. Using just a single edge of one channel results in 16
counts per revolution of the motor shaft, so the frequency of the A output
in the above oscilloscope capture is 16 times the motor rotation
frequency.
• After making the motor rotate, a program is required to measure the
speed of the motor. The speed of the motor can be measured by the
pulses. The encoder records the amount of high to low pulses and using
the formulas below converts the pulses into speed:
• To know the speed of the motor from the pulses…
𝑆𝑒𝑛𝑠𝑜𝑟 𝑝𝑢𝑙𝑠𝑒𝑠
SpeedMotor = × 60 𝑠𝑒𝑐𝑜𝑛𝑑𝑠
16𝑐𝑜𝑢𝑛𝑡𝑠 × 131(𝑔𝑒𝑎𝑟 𝑟𝑎𝑡𝑖𝑜)
𝑆𝑒𝑡𝑆𝑝𝑒𝑒𝑑
setPWM = ×1023
𝑀𝑎𝑥𝑖 𝑅𝑃𝑀
Codes:
//To set the speed as per user input and PWM calculation accordingly.
motor_speed =userinput/max_rpm;
motor_speed=motor_speed*1023;
// PWM Configurations
setup_timer_2(T2_DIV_BY_1,255,1); // Set PWM frequency
setup_ccp1(CCP_PWM); // Configure CCP1 to PWM mode
set_pwm1_duty(motor_speed); // full speed
-------------
//Infra-Red Function
int IR_sensor(){
if(!input(IR1)){ //if only IR on Right is low
return(1);
}
else if(!input(IR2)){ //if only IR in centre is low
return(2);
}
else if(!input(IR3)){ //if only IR on left is low
return(3);
}
}
void main()
{
forward(); //Robot to move Forward at 78% PWM
---some codes for obstacle detection and take action as per distance from obst.
switch (IR_sensor()) // Call IR detection function and check status)
{
case 1:
right(); // Take right
break;
case 2:
forward(); // Go forward
break;
case 3:
left(); // Take left
break;
default:
forward(); // by default forward
break;
}
}
//forward function
void forward() {
set_pwm1_duty(800L); //PWM for Motor B(right) @ 78%
set_pwm2_duty(800L); //PWM for Motor A(left) @ 78%
output_low(motorA_1); output_high(motorA_2); //Motor on left
output_low(motorB_1); output_high(motorB_2); //motor on right
}
Think!!!!
//stop function
void stop() { How to
set_pwm1_duty(0L); //PWM for Motor B(right) @ 0% take motor
set_pwm2_duty(0L); //PWM for Motor A(left) @ 0%
output_low(motorA_1); output_low(motorA_2); //Motor on left
reverse!
output_low(motorB_1); output_low(motorB_2); //Motor on right
}
void left() {
set_pwm1_duty(900L); //PWM for Motor B(right) @ 88%
set_pwm2_duty(0L); //PWM for Motor A(left) @ 0%
output_low(motorA_1); output_high(motorA_2); //Motor on left
output_low(motorB_1); output_high(motorB_2); //Motor on right
}
void right() {
set_pwm1_duty(0L); //PWM for Motor B(right) @ 0%
set_pwm2_duty(850L); //PWM for Motor A(left) @ 83%
output_low(motorA_1); output_high(motorA_2); //Motor on left
output_low(motorB_1); output_high(motorB_2); //Motor on right
}
Think logic to work with FIVE sensors’ inputs