public
Last active

Code from "Paul Sumo 2", my winning entry in the LVBots sumo competition. This is an app for the Pololu Wixel that I compiled using the Wixel SDK.

  • Download Gist
paul-sumo-2.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#include <wixel.h>
#include <usb.h>
#include <usb_com.h>
#include <adc.h>
#include <stdio.h>
 
uint32 start_ms;
 
void resetTime()
{
start_ms = getMs();
}
 
uint32 elapsedTime()
{
return getMs() - start_ms;
}
 
uint8 buttonPressed()
{
setPort0PullType(HIGH);
setDigitalInput(00,PULLED);
setDigitalOutput(03,LOW);
return !isPinHigh(00);
}
 
void timer3Init()
{
// Start the timer in free-running mode and set the prescaler.
T3CTL = 0b01110000; // Prescaler 1:8, frequency = (24000 kHz)/8/256 = 11.7 kHz
//T3CTL = 0b01010000; // Use this line instead if you want 23.4 kHz (1:4)
 
// Set the duty cycles to zero.
T3CC0 = T3CC1 = 0;
 
// Enable PWM on both channels. We choose the mode where the channel
// goes high when the timer is at 0 and goes low when the timer value
// is equal to T3CCn.
T3CCTL0 = T3CCTL1 = 0b00100100;
 
// Configure Timer 3 to use Alternative 1 location, which is the default.
PERCFG &= ~(1<<5); // PERCFG.T3CFG = 0;
 
// Configure P1_3 and P1_4 to be controlled by a peripheral function (Timer 3)
// instead of being general purpose I/O.
P1SEL |= (1<<3) | (1<<4);
 
// After calling this function, you can set the duty cycles by simply writing
// to T3CC0 and T3CC1. A value of 255 results in a 100% duty cycle, and a
// value of N < 255 results in a duty cycle of N/256.
}
 
int16 position = 0;
int16 last_position = 0;
int16 diff = 0;
int16 pid = 0;
 
int8 rightSensor()
{
return !isPinHigh(10);
}
 
int8 leftSensor()
{
return !isPinHigh(11);
}
 
void setMotors(int16 left, int16 right)
{
if(left < 0)
{
T3CC1 = -left;
setDigitalOutput(16, 1);
}
else
{
T3CC1 = left;
setDigitalOutput(16, 0);
}
if(right < 0)
{
T3CC0 = -right;
setDigitalOutput(15, 1);
}
else
{
T3CC0 = right;
setDigitalOutput(15, 0);
}
}
 
typedef enum State
{
WAIT_BUTTON,
DELAY,
INITIAL_SPIN,
RUN,
SPIN,
ATTACK,
BACKUP
} State;
 
void updateState()
{
static State state = WAIT_BUTTON;
static uint32 state_start = 0;
static uint32 last_seen = 0;
static uint8 last_on_right = 0;
static int32 reverse;
static uint8 initial_random_turn = 0;
 
if(leftSensor() && !rightSensor())
last_on_right = 0;
if(rightSensor() && !leftSensor())
last_on_right = 1;
if(rightSensor() || leftSensor())
last_seen = getMs();
 
LED_RED(leftSensor());
LED_YELLOW(rightSensor());
 
switch(state)
{
case WAIT_BUTTON:
initial_random_turn = !initial_random_turn;
setMotors(0,0);
if(buttonPressed())
{
state_start = getMs();
state = DELAY;
}
break;
case DELAY:
setMotors(0,0);
if(getMs() - state_start > 5000)
{
state_start = getMs();
state = INITIAL_SPIN;
}
LED_YELLOW((getMs() - state_start) & 0x80);
LED_RED((getMs() - state_start) & 0x80);
break;
case INITIAL_SPIN:
if(getMs() - state_start > 80)
{
state_start = getMs();
state = RUN;
}
if(initial_random_turn)
setMotors(-255,255);
else
setMotors(255,-255);
break;
case RUN:
if(getMs() - state_start > 350)
{
state_start = getMs();
state = SPIN;
}
if(initial_random_turn)
setMotors(255,100);
else
setMotors(100,255);
break;
case SPIN:
if(last_on_right)
setMotors(255,-255);
else
setMotors(-255,255);
 
if(leftSensor() || rightSensor())
{
state = ATTACK;
state_start = getMs();
}
break;
case ATTACK:
if(getMs() - state_start > 750)
{
state_start = getMs();
state = BACKUP;
}
else if(leftSensor() && rightSensor())
setMotors(255,255);
else if(leftSensor())
setMotors(200,255);
else if(rightSensor())
setMotors(255,200);
else if(getMs() - last_seen > 50)
{
state_start = getMs();
state = SPIN;
}
break;
case BACKUP:
if(getMs() - state_start > 350)
{
state_start = getMs();
state = ATTACK;
}
else if(getMs() - state_start > 250)
setMotors(128,-128);
else
setMotors(-255,-64);
break;
}
}
 
void main()
{
systemInit();
usbInit();
timer3Init();
resetTime();
while(1)
{
boardService();
usbShowStatusWithGreenLed();
updateState();
usbComService();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.