Show Posts
Pages: 1 [2] 3 4 5
16  Using Arduino / Programming Questions / Re: Arduino Mega 2560 PWM Setting on: July 18, 2013, 05:12:56 am
They won't be identical. You'll have to haul out the datasheet and do some reading.

I have read the datasheet, they looks identical..
17  Using Arduino / Programming Questions / Arduino Mega 2560 PWM Setting on: July 18, 2013, 02:26:42 am
Hi Everyone, i want to ask about PWM setting on Arduino Mega 2560.

Previously, i use this PWM setting for my Arduino Board that based on ATMEga 328p microcontroller.

Code:
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);
  TCCR0B = _BV(CS00);
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
  TCCR1B = _BV(CS10);
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(CS20);

Can i use same PWM setting in Arduino Mega 2560? Or the PWM setting on Arduino Mega 2560 is different?
18  Using Arduino / Programming Questions / Re: count++ on the Loop Section on: July 09, 2013, 09:28:54 am
count++; increments the count variable by 1. Later it's being tested if it matches some value (e.g. 21) and if so there's some operation performed.


Thanks for replying JarkkoL..
So count++ will increment the count variable by 1. But, how quick it will count? Is it based on looping frequency? And what the different if i not use "count" on the loop section?
19  Using Arduino / Programming Questions / count++ on the Loop Section on: July 06, 2013, 02:47:17 pm
Hello everyone, i have found this sketch on the internet:

Code:
int count=0;
void loop()
{
  count++;

  sampleTimePID = (micros()-timer)/1000000.0/CC_FACTOR; // in Seconds!
  timer = micros();
   
  // Update raw Gyro
  updateRawGyroData(&gyroRoll,&gyroPitch);
   
  // Update ACC data approximately at 50Hz to save calculation time.
  if(count == 20)
  {
    mpu.getAcceleration(&x_val,&y_val,&z_val);
 
    sampleTimeACC = (micros()-timerACC)/1000.0/CC_FACTOR; // in Seconds * 1000.0 to account for factor 1000 in parameters
    timerACC=timer;
    //{Serial.print(sampleTimeACC,5);Serial.print(" ");Serial.println(sampleTimePID,5);} 
  }
  if(count == 21) rollAngleACC =atan2(-y_val,-z_val)*57.2957795;
  if(count == 22)
  {
     pitchAngleACC =-atan2(-x_val,-z_val)*57.2957795;
     count=0;
  }
 
  //Serial.println( (micros()-timer)/CC_FACTOR);
   
  gyroRoll = gyroRoll + config.accelWeight * (rollAngleACC - rollSetpoint)/sampleTimeACC;
  gyroPitch = gyroPitch + config.accelWeight * (pitchAngleACC - pitchSetpoint)/sampleTimeACC;
  pitchPID = ComputePID(sampleTimePID,gyroPitch,0.0, &pitchErrorSum, &pitchErrorOld,config.gyroPitchKp,config.gyroPitchKi,config.gyroPitchKd,maxDegPerSecondPitch);
  rollPID = ComputePID(sampleTimePID,gyroRoll,0.0, &rollErrorSum, &rollErrorOld,config.gyroRollKp,config.gyroRollKi,config.gyroRollKd,maxDegPerSecondRoll);

  pitchDevider = constrain(maxDegPerSecondPitch / (pitchPID + 0.000001), -15000,15000);
  pitchDirection = sgn(pitchDevider) * config.dirMotorPitch;
  rollDevider = constrain(maxDegPerSecondRoll / (rollPID + 0.000001), -15000,15000);
  rollDirection = sgn(rollDevider) * config.dirMotorRoll;
//  Serial.println(freeRam ());

  sCmd.readSerial();
}

The Sketch main function is to read MPU6050 data. But from that code, I can not understand about "count'". On the Loop section, there is some "count" code, like:
Code:
count++;
if(count == 21) rollAngleACC =atan2(-y_val,-z_val)*57.2957795;
  if(count == 22)
  {
     pitchAngleACC =-atan2(-x_val,-z_val)*57.2957795;
     count=0;
  }
count=0;

My question is: What the function of "count" code? And what is the different is we not use "count" on the loop section?

Thank You..

Oh, i found similiar code on the ISR function too. They are "freqCounter", "deviderCountPitch", and "deviderCountRoll" :

Code:
ISR( TIMER1_OVF_vect )
{
  freqCounter++;
  if(freqCounter==(CC_FACTOR/MOTORUPDATE_FREQ))
  {

    // Move pitch and roll Motor
    deviderCountPitch++;
    if(deviderCountPitch  >= abs(pitchDevider))
    {
      fastMoveMotor(MOTOR_PITCH, pitchDirection);
      deviderCountPitch=0;
    }
   
    deviderCountRoll++;
    if(deviderCountRoll >= abs(rollDevider))
    {
      fastMoveMotor(MOTOR_ROLL, rollDirection);
      deviderCountRoll=0;
    }
    freqCounter=0;
  }
}
20  Using Arduino / Sensors / Re: cmps10 ( Tilt Compensated Magnetic Compass) on: July 05, 2013, 09:02:14 am
I have modified the code from robot-electronics.com, and it's works:

Code:
/****************************************************************
*                  Arduino CMPS10 example code                  *
*                    CMPS10 running I2C mode                    *
*                    by James Henderson, 2012                   *
*****************************************************************/
#include <Wire.h>
//#include <SoftwareSerial.h>

#define ADDRESS 0x60                                          // Defines address of CMPS10

//#define LCD_RX              0x02                              // RX and TX pins used for Wire03 serial port
//#define LCD_TX              0x03
#define Wire_HIDE_CUR      0x04
#define Wire_CLEAR         0x0C
#define Wire_SET_CUR       0x02

//SoftwareSerial Wire =  SoftwareSerial(LCD_RX, LCD_TX);      // Defines software serial port for Wire

void setup(){
  Wire.begin();                                               // Conects I2C
  Serial.begin(9600);
  Wire.write(Wire_HIDE_CUR);
  Wire.write(Wire_CLEAR);
}

void loop(){
   byte highByte, lowByte, fine;              // highByte and lowByte store high and low bytes of the bearing and fine stores decimal place of bearing
   char pitch, roll;                          // Stores pitch and roll values of CMPS10, chars are used because they support signed value
   int bearing;                               // Stores full bearing
   
   Wire.beginTransmission(ADDRESS);           //starts communication with CMPS10
   Wire.write(2);                              //Sends the register we wish to start reading from
   Wire.endTransmission();

   Wire.requestFrom(ADDRESS, 4);              // Request 4 bytes from CMPS10
   while(Wire.available() < 4);               // Wait for bytes to become available
   highByte = Wire.read();           
   lowByte = Wire.read();           
   pitch = Wire.read();             
   roll = Wire.read();               
   
   bearing = ((highByte<<8)+lowByte)/10;      // Calculate full bearing
   fine = ((highByte<<8)+lowByte)%10;         // Calculate decimal place of bearing
   
   display_data(bearing, fine, pitch, roll);  // Display data to the Wire
   
   delay(100);
}

void display_data(int b, int f, int p, int r){    // pitch and roll (p, r) are recieved as ints instead oif bytes so that they will display corectly as signed values.
 
  Wire.write(Wire_SET_CUR);                     // Set the Wire cursor position
  Wire.write(1); 
  //Serial.print("CMPS10 Example V:");
  //Serial.print(soft_ver());                        // Display software version of the CMPS10
 
  delay(5);                                       // Delay to allow Wire to proscess data 
 
  Wire.write(Wire_SET_CUR);
  Wire.write(21); 
  Serial.print("Bearing = ");                      // Display the full bearing and fine bearing seperated by a decimal poin on the Wire
  Serial.print(b);                               
  Serial.print(".");
  Serial.print(f);
  Serial.print(" \n");

  delay(5);

  Wire.write(Wire_SET_CUR);                     // Display the Pitch value to the Wire
  Wire.write(41);
  //Serial.print("Pitch = ");
  //Serial.print(p);
  //Serial.print(" /t");

  delay(5);
 
  Wire.write(Wire_SET_CUR);                     // Display the roll value to the Wire
  Wire.write(61);
  //Serial.print("Roll = ");
  //Serial.print(r);
  //Serial.print(" /n");
}

/*
int soft_ver(){
   int data;                                      // Software version of  CMPS10 is read into data and then returned
   
   Wire.beginTransmission(ADDRESS);
   // Values of 0 being sent with write need to be masked as a byte so they are not misinterpreted as NULL this is a bug in arduino 1.0
   Wire.write((byte)0);                           // Sends the register we wish to start reading from
   Wire.endTransmission();

   Wire.requestFrom(ADDRESS, 1);                  // Request byte from CMPS10
   while(Wire.available() < 1);
   data = Wire.read();           
   
   return(data);
}
*/
21  Using Arduino / Motors, Mechanics, and Power / Re: Brushless Motor Become Slower when combined with the IMU on: June 15, 2013, 08:40:47 pm
Is there anybody can help me?
22  Using Arduino / Motors, Mechanics, and Power / Re: Brushless Motor Become Slower when combined with the IMU on: June 13, 2013, 08:04:50 am
Well, i try to use Interrupt, and the code is like this:

Code:
// constants won't change. They're used here to
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;

// Variables will change:
boolean direct = true; // direction true=forward, false=backward

int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function

 
//int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
//}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;

void initBLDC() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  
  //TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)
  //TCCR0B = TCCR0B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 5 and 6
  
  ICR1 = 255 ; // 8 bit resolution
  //ICR1 = 1023 ; // 10 bit resolution
  TIMSK2 |= _BV(TOIE1);
  sei();

  // Enable Timer1 Interrupt for Motor Control
  OCR0A = 0;
  OCR0B = 0;
  OCR1A = 0;
  OCR1B = 0;  
  OCR2A = 0;  
  OCR2B = 0;  
}


ISR(TIMER2_OVF_vect) {  
 if((millis() - lastMotorDelayTime) > 0)
{ // delay time passed, move one step
 
  if (direct==true)
  {
    increment = 1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
  
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
  
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  
  }
  
  
  if (direct==false)
  {
    increment = -1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
  
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
  
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  }
  
lastMotorDelayTime = millis();

}
  
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
  
};  

void setup() {
 
  initBLDC();
  
}


void loop() {

  //IMU Code Here
    
}
 

But, it is didn't works. Looks like the brushless motor not commutating, motor just hold it's position..

I know my code is wrong, but i can't find the wrong part, i think i need help here..
23  Using Arduino / Motors, Mechanics, and Power / Re: Brushless Motor Become Slower when combined with the IMU on: June 12, 2013, 08:00:01 pm
Could you explain more about the hardware - what kind of brushless motor?  What is your approach to commutation?
What pins are controlling / sensing what?  And what interrupts are you using?

I use this board:
https://www.hobbyking.com/hobbyking/store/__27033__MultiWii_328P_Flight_Controller_w_FTDI_DSM2_Port.html

And this is the motor:
http://www.rctimer.com/index.php?gOo=goods_details.dwt&goodsid=871&productname=

And i use L6234D as a motor driver:
http://www.st.com/st-web-ui/static/active/cn/resource/technical/document/application_note/CD00004062.pdf

I use 3 phase sine wave PWM signal to commutate the motor, i use pins 3, 6 and 5 for PWM, and I not use interrupts (may be this is the problem).


There are no interrupts being used in his code posted on GitHub.

You're going to need to be more clever in how you set up the scheduling of the various functions of your gimbal. It looks like in your code you considered implementing a timer interrupt and that is probably the best way to run things. Decide how quickly you want your gimbal motors to be able to turn and set up an interrupt so that those motors are always updated with that frequency. All of the other tasks of reading from the gyros/accelerometer or calculating the kalman filter, etc. should be considered secondary; you don't need to be doing everything on every loop().

Ok, i will try to use interrupts..
24  Using Arduino / Motors, Mechanics, and Power / Brushless Motor Become Slower when combined with the IMU on: June 12, 2013, 10:01:21 am
Hi Everyone, i have a problem with brushless motor programming. I am not using ESC, but use L6234D as a motor driver. So far, i have successfully make my brushless motor rotating well.

Here is my arduino sketch for rotating Brushless Motor foy my Gimbal:
Code:
// constants won't change. They're used here to
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;

// Variables will change:
boolean direct = true; // direction true=forward, false=backward

int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function

 
//int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
//}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;

void initBLDC() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  
  //TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)

 

  ICR1 = 255 ; // 8 bit resolution
  //ICR1 = 1023 ; // 10 bit resolution

}

 
void setup() {
  
  initBLDC();
  
}


void loop() {

BLDCmove();
    
}
 
 
 
void BLDCmove() {
if((millis() - lastMotorDelayTime) > 0)
{ // delay time passed, move one step
 
  if (direct==true)
  {
    increment = 1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
  
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
  
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  
  }
  
  
  if (direct==false)
  {
    increment = -1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
  
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
  
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  }
  
lastMotorDelayTime = millis();

}
  
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
  
}

Then, i combine the code above with my IMU sketch, The code is here:
https://github.com/mamette/Brushless/blob/master/brushless_IMU.ino

The result is my brushless motor rotating much slower. Is IMU processing affected my brushless motor speed?

Thank You..
25  Using Arduino / Motors, Mechanics, and Power / Re: Brushless Motor Basic Programming Question on: June 12, 2013, 09:47:30 am
Now i try to just use one of TCCR2B or TCCR1B and use 8 bit PWM resolution. When i use TCCR2B or TCCR1B, it's works well, but if i use TCCR0B, the brushless motor is not rotating.

The code is become like this:

Code:
// constants won't change. They're used here to
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;

// Variables will change:
boolean direct = true; // direction true=forward, false=backward

int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function

 
//int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
//}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;

void initBLDC() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
 
  //TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)
  //TCCR0B = TCCR0B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 5 and 6
 
  ICR1 = 255 ; // 8 bit resolution
}

 
void setup() {
 
  initBLDC();
 
}


void loop() {

BLDCmove();
   
}
 
 
 
void BLDCmove() {
if((millis() - lastMotorDelayTime) > 0)
{ // delay time passed, move one step
 
  if (direct==true)
  {
    increment = 1;
   
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
 
  }
   
   
  if (direct==false)
  {
    increment = -1;
   
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  }
 
lastMotorDelayTime = millis();

}
   
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
   
}

So, it is ok if i just use 1 PWM register?
26  Using Arduino / Motors, Mechanics, and Power / Re: Brushless Motor Basic Programming Question on: June 09, 2013, 10:55:31 am
timer0 controls PWM on pins 5 and 6 but also does the timer interrupts for millis(), so if you change its
clock then mills(), micros() and delay() will stop working properly.

timers 0, 1 and 2 are all different in details.  only timer1 can do more than 8 bits.

So, if i use TCCR0B, then mills(), micros() and delay() will stop working properly? So, using TCCR1B for pins 5 and 6 is OK?
27  Using Arduino / Motors, Mechanics, and Power / Brushless Motor Basic Programming Question on: June 09, 2013, 07:49:23 am
Hi everyone..

I have try code for rotating my brushless motor from here:
http://elabz.com/bldc-motor-with-arduino-circuit-and-software/

Because i just want to rotating my brushless motor, i modify the code to be like this:
Code:
/*
Driving a DVD drive spindle three-phase motor
 
This code was used for the stroboscope project
 
This example code is in the public domain. Based on several Arduino code samples
 
http://elabz.com/
 
 */
 
// constants won't change. They're used here to
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;
const int motorDelay=0; // together with pot controls the RPM
const int potState=0;       // controls the RPM speed

// Variables will change:
boolean direct = true; // direction true=forward, false=backward


/*
int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function
*/
 
int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;


 
void setup() {
 
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
 
  TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)

  //ICR1 = 255 ; // 8 bit resolution
  ICR1 = 1023 ; // 10 bit resolution




}
 
void loop() {

motorDelayActual =   potState * motorDelay / 100;
move();
   
}
 
 
 
void move() {
if((millis() - lastMotorDelayTime) >  motorDelayActual)
{ // delay time passed, move one step
 
  if (direct==true)
  {
    increment = 1;
  }
    else
    {
     increment = -1; 
    }
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
 
lastMotorDelayTime = millis();
}
   
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
   
}

And it's works..

But, there is something that i not understand. I use ATMega 328p and use pins 3,6 and 5 as PWM output. And i set the PWM register is like this:
Code:
TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)

  //ICR1 = 255 ; // 8 bit resolution
  ICR1 = 1023 ; // 10 bit resolution

TCCR1B is PWM register for pins 9 and 10 (OC1A and OC1B), then TCCR2B is PWM register for pins 11 and 3 (OC2A and OC2B). But, i use pins 3, 6 and 5. It is ok for TCCR1B because it is for pin 3, but how about TCCR2B? TCCR2B is for pin 9 and 10, not for pins 6 and 5. But the code is works well..

Then, i change the PWM register to be like this:
Code:
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)
  TCCR0B = TCCR0B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 5 and 6

I change TCCR1B with TCCR0B because TCCR0B is for pins 6 and 5 (OC0A and OC0B). But, the code is not works when i change it. This is confusing, when i use the right register, the code not works. But when i use wrong register, the code works well..

Does anybody know why this happen?

Thank You..
28  Using Arduino / Programming Questions / Re: What's the mean of "f" in this Code on: May 23, 2013, 03:15:16 am
This has a good answer, there are links to relevant info:
http://stackoverflow.com/questions/4828167/purpose-of-a-f-appended-to-a-number

Great..
Thank You..
29  Using Arduino / Programming Questions / Re: What's the mean of "f" in this Code on: May 23, 2013, 02:01:42 am
Possibly "float" but it depends how the definitions are used later on. In this code it's just definition for the precompiler.

How if i deleted "f" and just use the number? I found many definition like this, for example:

Code:
#define GRAVITY 16384.0f
#define SCALE_PID_PARAMS 1000.0f

But i still confused what is the mean of "f"..
30  Using Arduino / Programming Questions / Re: [Help] Hold Position Sytem Use Servo, IMU and PID on: May 17, 2013, 07:41:05 am
Quote
May be it is like a balancing robot
Sounds to me like it is EXACTLY like a self-balancing robot. Where do you see differences?

Quote
Am i need PID controller for this system?
Maybe. Maybe not. Depends on how fast you need the system to react, and how fast the system CAN react.

Quote
Is there any reference that i can learn?
No. Google will be down until you finish your project. So, hurry it up. Millions of people are waiting.

Did you have a programming question?

Well, so far i make close loop system like this:

Code:
Error = CenterVal - rollmap; //finds error between the wanted value and what it currently is
  rollServo += Error;
  roll.write(rollServo);


where,
CenterVal= 90 (90 degree), this is the value that i want to maintain
rollmap= angle that detected by IMU (current position), i have use Kalman Filter for rollmap.
Error = difference between  CenterVal and rollmap
rollServo = how far servo move

So, servo will move if error detected, servo will continue to move until Error value back to 0, which mean stable position has been reached. I have done it, servo will move until stable position reached, but servo move very slow. And the system not start smoothly. Firstly, servo will move back and forth continuously until rollServo value reach around 1500, not always 1500 but around 1500 (i print it on the serial monitor, i know servo move between 0-180, but i don't know why rollServo value has been reached around 1500 and still can make servo works), then system will works like i explain before.

I can't find out where the problem is. Can you help me?
Pages: 1 [2] 3 4 5