Software > Software
Controlling motors [Urgent]
Mastermime:
Thanks for the response
--- Quote ---I have no idea what sending '0 as triangle' means lol
--- End quote ---
Ah sorry. I forgot to explain. When I press triangle on the PS2 controller, it sent 0.
--- Quote ---yeap
Rather than giving you the answer, I'll tell you how to debug software like this. What you need to do is add output data that you can see.
Add in rprintf commands here and there in your code, and then view it with Hyperterminal using your FTDI cable. For example:
--- End quote ---
Ok I will do this and report back tomorrow night.
--- Quote ---You can also use the numbered LED to show a certain number when it's running a certain subset of code. This lets you know what the software is doing behind the scenes.
And just skimming through your code I noticed this very serious bug:
int motorRTarVal;
motorRTarVal=192+motorInt;
The int type only ranges from −128 to 127, so having 192 + anything means your variable is going out of bounds.
--- End quote ---
I dont even know what I was thinking...
Admin:
I prefer to use these types to avoid silly mistakes:
int8_t
uint8_t
int16_t
uint16_t
int32_t
etc. etc.
It's also more compatible with other compilers, as I know of some that consider an 'int' as 16 bit.
jwatte:
--- Quote from: Admin on February 06, 2013, 09:02:32 PM ---The int type only ranges from −128 to 127, so having 192 + anything means your variable is going out of bounds. :P
--- End quote ---
Which compiler are you using?
Using avr-gcc (which is also used by Arduino) the sizes are:
char, unsigned char: 1 byte [-128,127] [0,255]
short, unsigned short, int, unsigned int, pointer: 2 bytes [-32768,32767] [0,65535]
long, unsigned long, float, double: 4 bytes [-2-bazillion,2-bazillion] [0,4-bazillion] [float-range-is-really-large]
Admin:
I'm using avr gcc. An int is 8 bits. A long int is 16 bits. :P
I don't use Arduino, but perhaps their library does it differently? The compilers I used for C++ on my PC would make an int as 16 bits and a long int as 32 bits.
The reason I prefer using int8_t (etc.) is that it prevents this confusion.
Mastermime:
Ah ok. I just hooked up my ftdi cable to my corresponding Xbee on the controller because in my code it says send rprintf to XbeeSendByte. It says "value received: -17404" Where the heck is that number coming from and how can I fix it? I should probably set my rprintf output to UART3 because Xbees are half duplex, therefore how can it transmit and receive data at the same time, right?
Updated code
--- Code: ---#include "hardware.h"
#define XBee_Controlled
//#define USB_Controlled
#define UART0_TX_BUFFER_SIZE 2
#define UART0_RX_BUFFER_SIZE 2
#define TRIANGLE 256
#define START 257
#define L2 258
#define R2 259
#define MOTOR_L_MIN 1
#define MOTOR_L_STOP 64
#define MOTOR_L_MAX 127
#define MOTOR_R_MIN 128
#define MOTOR_R_STOP 192
#define MOTOR_R_MAX 255
#define SIGNAL_MOTORS_OFF 0
uint16_t motorLCurVal;
uint16_t motorRCurVal;
uint16_t motorLTarVal;
uint16_t motorRTarVal;
uint16_t motorVal;
// Initialise the hardware
void appInitHardware(void) {
initHardware();
#ifdef XBee_Controlled
rprintfInit(XbeeSendByte);
#endif
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
motorLCurVal = motorLTarVal = MOTOR_L_STOP;
motorRCurVal = motorRTarVal = MOTOR_R_STOP;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
uint16_t tempbyte = NULL;
#ifdef XBee_Controlled
//tempbyte=XbeeGetByte;
if(!uartReceiveBufferIsEmpty(UART0)) {
tempbyte = uartGetByte(UART0);
rprintf(“Value recieved is: %d \n”);
delay_ms(200);
}
#endif
if (tempbyte != NULL) {
if (tempbyte >= MOTOR_L_MIN && tempbyte <= MOTOR_R_MAX)
{
setMotorSpeed(tempbyte);
}
else if (tempbyte==START)
{
//pin_low(Remoteswitch_relay);
}
else
{
motorStopMotor();
}
delay_ms(100);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
TICK_COUNT ms = loopStart / 1000; // Get current time in ms
int16_t now = ms % (TICK_COUNT)10000; // 10 sec for a full swing
if(now >= (int16_t)5000){ // Goes from 0ms...5000ms
now = (int16_t)10000 - now; // then 5000ms...0ms
}
return 0;
}
//motorXCurVal is the current speed.
//motorXTarVal is where we want the speed to be.
//motorVal, in this case, is running on a scale of 1 to 10. Thus, 1 is 10% of maximum //speed, 2 is 20%, and so on. NOT //USED
void motorStopMotor()
{
do {
if (motorLCurVal<MOTOR_L_STOP)
{
motorLCurVal=motorLCurVal+1;
}
else if (motorLCurVal>MOTOR_L_STOP)
{
motorLCurVal=motorLCurVal-1;
}
Sabertooth_uartSendByte(motorLCurVal);
delay_ms(50);
if (motorRCurVal<MOTOR_R_STOP)
{
motorRCurVal=motorRCurVal+1;
}
else if (motorRCurVal>MOTOR_R_STOP)
{
motorRCurVal=motorRCurVal-1;
}
Sabertooth_uartSendByte(motorRCurVal);
delay_ms(50);
} while (motorLCurVal!=MOTOR_L_STOP && motorRCurVal!=MOTOR_R_STOP);
}
void setMotorSpeed(uint16_t byte)
{
if (byte >= MOTOR_L_MIN && byte <= MOTOR_L_MAX)
{
motorLTarVal = byte;
do {
if (motorLCurVal < motorLTarVal)
{
Sabertooth_uartSendByte(++motorLCurVal);
}
else
{
Sabertooth_uartSendByte(--motorLCurVal);
}
delay_ms(100);
} while (motorLCurVal != motorLTarVal);
}
else if (byte >= MOTOR_R_MIN && byte <= MOTOR_R_MAX)
{
motorRTarVal = byte;
do {
if (motorRCurVal < motorRTarVal)
{
Sabertooth_uartSendByte(++motorRCurVal);
}
else
{
Sabertooth_uartSendByte(--motorRCurVal);
}
delay_ms(100);
} while (motorRCurVal != motorRTarVal);
}
}
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version