It is currently 20 Apr 2018 07:37

All times are UTC + 1 hour

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 09 Oct 2017 03:45 

Joined: 06 Oct 2017 20:32
Posts: 2
Hi!, first post here.

uC: dsPIC33FJ12GP202
Simulator: Proteus ISIS
Compiler ver.: 6.2

So I have a question. I'm developing some code that communicates by UART with the computer in order to move a stepper
motor with precision, so by starting the program it asks for 1.-Direction (Left/Right). 2.-Degrees(0-360). 3.-Speed(0-120rpm max).

And by this point everything works, I managed to configure everything, dave the data and get the code structure in order, but
I have one issue..

When the UART finishes receiving the correct data, it calls a function in order to manipulate the data it got, and move the stepper motor the way
its desired. but the code stops running in the simulation just after the data is received, and the pins that correspond to the stepper motor never
change state, (said motor is conected throught a L298N H-Bridge controller).

Now, I have used that motor driver and said logic before, so first, as I didn't have experience with C fuctions, I made another
version of the program that gets rid of the function for troubleshooting and after doing that I reduced the problem to this secion of code:

//"Problematic section"------------------------------
count=(grad/degperstep); //Establecer grados->cuentas (cuantos pasos vamos a avanzar)
pos=0; //Conteo de excitación
vueltaspseg=veloc/60; //Establecer rpm->us
//End of section---------------------------------------

Note: Certain operations have what I consider an implicit trunk, like ones "int=float/int" or such, this for
getting those variables to take part in the variable delays in the for loop and such.

Now it's not inside a function, so calling and receiving the correct data was not the problem, now, when I try to debug inside mikroC,
it stops right in the first instruction of that block of code and gets to a routine so called "long2float" that never gets out of.

So, after trying to eliminate the need for calculations, I set the variables "count", "time", and "pos" to see if I have and output
and voilá, I have an output to the motor, so, my question is:

->Are those calculations too complex to do for the microcontroller or am I running out of memory? I can't very much understand what could be
causing the issue since this microcontrollers are supossed to have pretty strong RAM and math power.

Now this are my variables and the type they are:

const char right[]={0b1100,0b1100,0b0100,0b0110,0b0010,0b0011,0b0001,0b1001}; //Excitacion derecha
const char left[]={0b1001,0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1100}; //Excitacion izquierda
int pos; //Para controlar la posición de la excitación (1~8)
char salidas; //Pines PORTB para motor
int aa; //Para recibir de la UART

int grad, sent, veloc; //Peticiones de datos en enteros

float degperstep; //->
int stepperrev, count; //Para control de ángulo

float vueltaspseg; //->
float pasosporvuelta; //->
int time; //Para control de la velocidad

int xx; //Control de flujo de cuestiones/acciones

int i,j; //For statements

I haven't tried changing them all to one type (all float/all int) but that would really mess the process and I have change one or two (the ones that it uses
on the first line of calculations, for example)
so I doubt that really solves anything.

The following code is the moving routine and I have succesfully implemented it in the past for moving said motor:

//Start of the routine---------------------------------
if(sent==1) //Derecha
for(i=0; i<count; i++)
if(pos==8) {pos=0;}
salidas=right[pos]; //Establecemos la posición de la excitación
Hi(LATB)=salidas; //Movemos motor
for(j=0; j<time; j++) {delay_us(1);} //Velocidad requerida
}//Fin pasos
}//Fin Derecha
else //Izquierda
for(i=0; i<count; i++)
if(pos==8) {pos=0;}
salidas=left[pos]; //Establecemos la posición de la excitación
Hi(LATB)=salidas; //Movemos motor
for(j=0; j<time; j++) {delay_us(1);} //Velocidad requerida
}//Fin pasos
}//Fin Izquierda
//End of move motor routine-----------------------------

I need the variable times and degrees for the cas when another motor with different specifications is used.

The compiler gives no errors and evreything works but the section I give.

How can I approach to solve this issue?, precision with variables is not a problem (I don't need 10 decimals in anything) but indeed
at least doing 45/1.8 and get a coherent result would be more than enough since it's the main goal of the program.

PostPosted: 10 Oct 2017 17:12 
User avatar

Joined: 21 Mar 2017 16:57
Posts: 527

I have tested the divisions you were using and they seem to work just fine, when dividing float with int the result stored in another int was correct (of course, rounded to int). Maybe you could attach the project you are working on, or a piece which deals with this issue here, so we can take a look - maybe something else causes troubles...

Best regards

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: