Different motor noise with the same functional code in atmel studio 6.2 and arduino

Hi,

There is a strange motor noise when running this code in the Baby Orangutan 328P with the Arduino IDE

#include <OrangutanMotors.h>
#include <PololuWheelEncoders.h>

//Right Encoder, motor2 in BabyO
#define rea 7    //IO_D7 
#define reb 4    //IO_D4

int left_enc_cnt;
int left_speed = 30;

//Left Encoder, motor1 in BabyO
#define leb 9    //IO_B0 physically connected, changed to increasing count
#define lea 8    //IO_B1

int right_enc_cnt;
int right_speed = 30;

int motor_stop = 0;

void setup() {
  Serial.begin(115200);
  PololuWheelEncoders::init(lea, leb, rea, reb);
  delay(5000);
  OrangutanMotors::setSpeeds(left_speed, right_speed);
}

void loop() {
  left_enc_cnt = PololuWheelEncoders::getCountsM1();
  right_enc_cnt = PololuWheelEncoders::getCountsM2();
  Serial.println(left_enc_cnt);
  Serial.println(right_enc_cnt);
  Serial.println();
  delay(2000);
}

Arduino Program Strange Noise

I’ve also written the same functional code in Atmel Studio 6.2 and uploaded the code to the BabyO

/* rightANDleft_motors_encodersANDspeed - an application for the Pololu Baby Orangutan B
 *
 * This application uses the Pololu AVR C/C++ Library.  For help, see:
 * -User's guide: http://www.pololu.com/docs/0J20
 * -Command reference: http://www.pololu.com/docs/0J18
 *
 * Created: 11/3/2016 8:04:04 PM
 *  Author: Daniel
 */

#include <pololu/orangutan.h>

#define rea IO_D7		//LEFT MOTOR as pcb and connected to test in protoboard: set m2 speed with positive value means decreasing count of encoders and go forward
#define reb IO_D4		//After changing signals in program  //then a change of signals in program would be made to have a positive value, incrementing counts and go forward

int left_enc_cnt;
int left_speed = 30;
int left_stop = 0;

#define leb	IO_B1		//RIGHT motor goes faster than left motor at same speeds, but right_motor(m1_speed) value should be negative to go forward, no changes in the encoders signals
#define lea IO_B0		//was required, not in the pcb nor in the code(forward means incrementing count), if a positive right_motor(m1_speed) value is wanted the physical connections should be interchanged

int right_enc_cnt;
int right_speed = 30;

#define buffer_size 7
char send_buffer[buffer_size];

void wait_for_sending_to_finish(){		//avr_library_commands Section 10
	while(!serial_send_buffer_empty());	//pololu_avr_library Section 3.h
}

void send_data(uint16_t data){
	wait_for_sending_to_finish();
	send_buffer[4] = data % 10 + 48;				//unidades
	if(data < 10){									//decenas, centenas y miles son cero
		send_buffer[0] = 0 + 48;
		send_buffer[1] = 0 + 48;					//miles
		send_buffer[2] = 0 + 48;					//centenas
		send_buffer[3] = 0 + 48;					//decenas
	}
	else if(data < 100){
		send_buffer[0] = 0 + 48;
		send_buffer[1] = 0 + 48;					//miles
		send_buffer[2] = 0 + 48;					//centenas
		send_buffer[3] = (data / 10) % 10 + 48;		//decenas
	}
	else if(data < 1000){
		send_buffer[0] = 0 + 48;
		send_buffer[1] = 0 + 48;					//miles
		send_buffer[2] = (data / 100) % 10 + 48;			//centenas
		send_buffer[3] = (data / 10) % 10 + 48;		//decenas
	}
	else if(data < 10000){
		send_buffer[0] = 0 + 48;							//decenas de millar
		send_buffer[1] = (data / 1000) % 10 + 48;			//miles
		send_buffer[2] = (data / 100) % 10 + 48;			//centenas
		send_buffer[3] = (data / 10) % 10 + 48;				//decenas
	}
	else if(data < 100000){
		send_buffer[0] = (data / 10000) % 10 + 48;			//decenas de millar
		send_buffer[1] = (data / 1000) % 10 + 48;			//millar
		send_buffer[2] = (data / 100) % 10 + 48;			//centenas
		send_buffer[3] = (data / 10) % 10 + 48;				//decenas
	}
	else{
		
	}
	
	send_buffer[5] = 10;	//Salto de linea
	send_buffer[6] = 13;	//Retorno de carro
	serial_send(send_buffer, buffer_size);
}

int main(){
	serial_set_baud_rate(115200);			//Default baud rate comm in HC-05 after configuration
	encoders_init(lea, leb, rea, reb);
	delay_ms(5000);
	set_motors(left_speed, right_speed);
	
	while(1){
		left_enc_cnt = encoders_get_counts_m1();
		right_enc_cnt = encoders_get_counts_m2();
		send_data(left_enc_cnt);
		send_data(right_enc_cnt);
		
		delay(1000);
		
		left_enc_cnt = encoders_get_counts_m1();
		right_enc_cnt = encoders_get_counts_m2();
		send_data(left_enc_cnt);
		send_data(right_enc_cnt);
		send_data(0);
		delay_ms(1000);
	}
}

Atmel Studio Program Normal Noise

and the noise of the motors was the normal noise that hears commonly from them.

Here is a picture of the conections

Anyone has an idea, of what could be causing this noise?

Thanks in advance.

Hello.

The motor noise you are hearing is probably the PWM frequency of your motor outputs. The reason why you are seeing differences when programming the Baby Orangutan using the Arduino IDE and Atmel Studio is due to the limitation of the OrangutanMotors library. This limitation of the OrangutanMotors library is stated under the “OrangutanMotors - Motor Control Library” section in the Programming Orangutans and the 3pi Robot from the Arduino Environment guide.

The OrangutanMotors library uses both PWM outputs of Timer0 and Timer2. Since the Arduino environment relies on Timer0 for timing functions (e.g. millis() and delay()), the library cannot reconfigure those timers and the outputs are limited to a frequency of 1.25kHz. (Generally, using higher frequencies (around 20kHz) eliminate the high pitched whine that lower frequencies generate.)

- Amanda

Thanks for that great explanation Amanda!

Do you know if with the A* 32U4 I would have the same noise caused by the PWM limitation of 1.25 kHz when using the timing functions?

Daniel

The OrangutanMotors library will not run on the A-Star 32U4 microcontrollers. If you let me know which version of the A-Star 32U4 programmable controllers you are referring to and what motor library you plan on using, I can tell you if there would be any conflict or limitation.

- Amanda