Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
uint8_t received = the number from 0 to 255 that you receivedDRIVE_SPEED speed = interpolate(received, 0, 255, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
#include "hardware.h"#define XBee_Controlled#define MIN(a,b) (((a)<(b))?(a):(b))#define MAX(a,b) (((a)>(b))?(a):(b))#define UART0_TX_BUFFER_SIZE 80#define UART0_RX_BUFFER_SIZE 80#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// Initialize the hardwarevoid appInitHardware(void) { initHardware(); #ifdef XBee_Controlled rprintfInit(debugSendByte); delay_ms(10); #endif}// Initialise the softwareTICK_COUNT appInitSoftware(TICK_COUNT loopStart){}// This is the main loopTICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {uint8_t received = XbeeGetByte;DRIVE_SPEED speed = interpolate(received, 0, 255, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX); act_setSpeed(&motor_1, 100); act_setSpeed(&motor_2, 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;}
The act_setSpeed will convert your value of 100 into almost full power forward and send a command out to the uart. Since you are using a constant value it will/should(?) only send it once.
First check that you've got the DIP switches on the Sabertooth set up according to the picture in Project Designer for the Sabertooth - as this has an effect on the commands that are sent out (eg packetized vs simple mode).
#include "hardware.h"// Initialise the hardwarevoid appInitHardware(void) { initHardware();}// Initialise the softwareTICK_COUNT appInitSoftware(TICK_COUNT loopStart){ return 0;}// This is the main loopTICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) { // -------- End Digital Output------- // -------- Start Actuators ------- // To control your.motors/servos then see actuators.h in the manual // To retrieve the required speed of motor_1 use: // DRIVE_SPEED speed=act_getSpeed(motor_1); // To set the required speed of motor_1 use: // act_setSpeed(motor_1,speed); // This example will move the motors back and forth using the loopStart time: 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 } // Map it into DRIVE_SPEED range uint8_t received = XbeeGetByte; DRIVE_SPEED speed = interpolate(received, 0, 255, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX); // Set speed for all motors/servos act_setSpeed(&motor_1,100); act_setSpeed(&motor_2,100); // -------- End Actuators ------- return 0;}
act_setSpeed(&motor_1,speed);
#include "hardware.h"#define XBee_Controlled#define DEBUG 1#define UART0_TX_BUFFER_SIZE 80#define UART0_RX_BUFFER_SIZE 80#define ACCELERATION 5#define DECELERATION 5#define IDLE_LOOPS_TILL_STOP 10#define DRIVE_SPEED_MIN -128#define DRIVE_SPEED_MAX 128#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#ifndef max #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )#endif#ifndef min #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )#endifint16_t motorLCurVal;int16_t motorRCurVal;int16_t motorLTarVal;int16_t motorRTarVal;int16_t motorLIdle;int16_t motorRIdle;boolean receiving;#define PACKET_SIZE 3char packet[] = {0, 0, 0, 0};int curByte;// Initialize the hardwarevoid appInitHardware(void) { initHardware(); #ifdef XBee_Controlled rprintfInit(debugSendByte); delay_ms(10); #endif //rprintf("\nHardware initialized.\n\n");}// Initialise the softwareTICK_COUNT appInitSoftware(TICK_COUNT loopStart){ //rprintf("\nAxon Initiated.\n\n"); receiving = false; motorLCurVal = motorLTarVal = MOTOR_L_STOP; motorRCurVal = motorRTarVal = MOTOR_R_STOP; curByte = 0;}// This is the main loopTICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) { uint16_t tempbyte = NULL; #ifdef XBee_Controlled if(!uartReceiveBufferIsEmpty(UART0)) { char temp = uartGetByte(UART0); if (temp == 'A' && !receiving) { receiving = true; packet[3] = '\0'; curByte = 0; } else { packet[curByte] = temp; if (curByte == (PACKET_SIZE - 1) ) { tempbyte = atoi(packet); rprintf("Packet recieved: %d", tempbyte); receiving = false; } curByte++; } } #endif if (tempbyte != NULL) { if (tempbyte >= MOTOR_L_MIN && tempbyte <= MOTOR_L_MAX) { motorLTarVal = interpolate(tempbyte, 1, 127, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX); motorLIdle = 0; motorRIdle++; } else if (tempbyte >= MOTOR_L_MIN && tempbyte <= MOTOR_L_MAX) { motorRTarVal = interpolate(tempbyte, 128, 255, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX); motorRIdle = 0; motorLIdle++; } else { motorLIdle++; motorRIdle++; switch(tempbyte) { case TRIANGLE: break; case START: break; case L2: break; case R2: break; default: break; } } tempbyte = NULL; } adjustMotorSpeed(); //Only moves motor if current value != target value if (motorLIdle > IDLE_LOOPS_TILL_STOP) { motorLTarVal = 0; } if (motorRIdle > IDLE_LOOPS_TILL_STOP) { motorRTarVal = 0; } return 0;}void adjustMotorSpeed(){ if (motorLCurVal < motorLTarVal) { motorLCurVal = min((motorLCurVal + ACCELERATION), motorLTarVal); act_setSpeed(&motor_1, motorLCurVal); } if (motorLCurVal > motorLTarVal) { motorLCurVal = max((motorLCurVal - DECELERATION), motorLTarVal); act_setSpeed(&motor_1, motorLCurVal); } if (motorRCurVal < motorRTarVal) { motorRCurVal = min((motorRCurVal + ACCELERATION), motorRTarVal); act_setSpeed(&motor_2, motorRCurVal); } if (motorRCurVal > motorRTarVal) { motorRCurVal = max((motorRCurVal - DECELERATION), motorRTarVal); act_setSpeed(&motor_2, motorRCurVal); }}
Why are you redefining DRIVE_SPEED_MIN and DRIVE_SPEED_MAX - they are defined in Webbotlib (with different values to yours - so you will be confusing Webbotlib enormously).