forked from jcannon6032/ME_330-TSwift_Technicians
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdit_This_One.c
132 lines (98 loc) · 3.3 KB
/
Edit_This_One.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
/*
* File: BaseMobility.c
* Author: svoss
*
* Created on October 24, 2017, 10:51 AM
*/
#pragma config FNOSC = LPFRC
#include "xc.h"
enum {LOOKING_FOR_CORNER, GOING_TO_CORNER, GOING_TO_CENTER, EXTENDING_RAMP, LOOKING_FOR_GOAL, SHOOTING, POISION_BALL} state;
//interrupt for switches and poision ball detect
void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
{
_CNIF=0;
//CN interrupt for when the robot hits the corner
if (state == GOING_TO_CORNER)
{
state = GOING_TO_CENTER;
PR1 = num_PWM_pulses_wheel (2420); //distance to center
_LATB9 = 1; //move forward
_LATB8 = 0; //move forward
TMR1=0;
}
//this is where the poison ball detect CN will go
}
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
_T1IF = 0;
TMR1=0;
//this state is triggered when the robot reaches the center of the arena
if (state == GOING_TO_CENTER)
{
_LATB7 = 0; //turn off (sleep) wheel motor driver
state = EXTENDING_RAMP;
/*
turn on ramp motor
pr1 = ???
*/
}
TMR1=0;
}
void wheel_PWM_config (void)
{
OC1CON1 = 0; //pin 14, controls both motors PWM
OC1CON2 = 0;
OC1RS = 1999; //this is a PWM frequency of 125 Hz (250,000/2000)
OC1R = 1000;
OC1CON1bits.OCTSEL = 0b111;
OC1CON2bits.SYNCSEL = 0x1F;
OC1CON2bits.OCTRIG = 0;
OC1CON1bits.OCM = 0b110;
//Direction control, pins 12, 13
TRISBbits.TRISB9=0; //setting pin #13, direction control for ?right? motor
TRISBbits.TRISB8=0; //Setting pin #12, direction control for ?left? motor
LATBbits.LATB9=0; // _LATB9=0 causes the ?right? motor to go ?forward? VERFIY THESE
LATBbits.LATB8=1; // _LATB8= 1 causes the ?left? motor to go ?forward?
//Setting up pin 11, the wheel motor sleep pin
_TRISB7 = 0;
_LATB7 = 1; //motor active
}
//this function returns the number of PWM pulses required for the motor to turn a specified number of degrees
int num_PWM_pulses_wheel (int Degrees_Turned)
{
TimeActive = Degrees_Turned * 1.0 / 225.0; // for full step size
//Modify this equation with motor parameters to determine how long the motors will be on (TimeActive) in order to rotate a desired angle (DegreesTurned)
//250000 (clock time) / 2000 (PWM frequency) = steps per second *1.8 (degrees per step if full step) = degrees per second = 225
Pulses = TimeActive*3906.0;
return Pulses;
}
//timer 1 is the wheel timer
void wheel_timer_config (void)
{
_TON = 1;
_TCS = 0;
_TCKPS = 0b10;
_T1IP = 4; //priority
_T1IE = 1; //enable
_T1IF = 0; //flag
PR1 = num_PWM_pulses_wheel (360*5);
TMR1=0;
}
//the corner detection switches are connected to pin 13
void switches_config (void)
{
_CN13IE = 1; //look at pin 13 to sense a change?
_CN13PUE = 0;
_CNIP = 6; //priority
_CNIE = 1; //enable
_CNIF = 0; //flag
}
int main(void) {
//as currently coded, these functions turn on the motors and motor timer and start watching for the switches
wheel_PWM_config();
wheel_timer_config ();
switches_config ()
while (1)
{}
return 0;
}