Commit f37894bd authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Add encoder handling to ball_and_beam-2018

parent cbd863ab
......@@ -18,10 +18,11 @@
* AI 2 -- Accelerometer X
* AI 3 -- Accelerometer Y
* AI 4 -- Accelerometer Z
* EI 5 -- Motor Position
*
* AO 0 -- Motor Speed
*/
* EI 0 -- Motor Position
*
*/
/*
* Used I/O pins
......@@ -70,7 +71,6 @@ struct values {
int32_t motorEncoder;
int8_t motorThermalFlag;
int8_t ballOnBeam;
uint8_t timer;
};
volatile struct values global;
......@@ -79,12 +79,6 @@ SIGNAL(ADC_vect)
unsigned char channel = ADMUX & 0x0f;
unsigned int value = ADCW;
if (global.timer) {
global.timer--;
PORTD &= ~0x40;
} else {
PORTD |= 0x40;
}
switch (channel) {
case 0: {
channel = 1;
......@@ -121,7 +115,6 @@ SIGNAL(USART_RXC_vect)
{
char ch = UDR;
global.timer = 0xff;
switch (serialio_RXC(ch)) {
case serialio_clearbit: {
switch (serialio_channel) {
......@@ -169,6 +162,39 @@ SIGNAL(USART_RXC_vect)
}
}
SIGNAL(TIMER1_OVF_vect)
{
static unsigned char encoder;
unsigned char e;
e = PIND & 0x0c;
// Ignore Z for now
switch ((encoder) | (e >> 2)) {
case 0x0:
case 0x5:
case 0xa:
case 0xf: {
// No change
} break;
case 0x1:
case 0x7:
case 0xe:
case 0x8: {
global.motorEncoder++;
} break;
case 0x2:
case 0x4:
case 0xd:
case 0xb: {
global.motorEncoder--;
} break;
default: {
// error = 1;
} break;
}
encoder = e;
}
int main()
{
serialio_init();
......@@ -181,11 +207,11 @@ int main()
DDRB = 0x0a; // PortB, bits 1 & 3 outputs
PORTC = 0x00; // PortC, pull-ups / initial values
DDRC = 0x80; // PortC, bit 7 output
PORTD = 0x00; // PortD, pull-ups / initial values
PORTD = 0x0c; // PortD, pull-ups / initial values
DDRD = 0x20; // PortD, bit 5 output
TCCR0 = 0x05; // Timer0, Clock / 1024
TCCR1A = 0x83; // OC1A 10 bit PWM (fast), active high
TCCR1B = 0x09; // Clock / 1
TIMSK = 0x04; // Enable Timer1 interrupts
OCR1A = 0 & 0x3ff;
UCSRA = 0x00; // USART:
UCSRB = 0x98; // USART: RxIntEnable|RxEnable|TxEnable
......@@ -196,6 +222,8 @@ int main()
ADMUX = 0x0e; // Vref = external, right adjust, read 1.22V (Vbg)
ADCSRA = 0xcf; // Enable ADC interrupts, Clock/128
global.motorEncoder = 0x80000000;
SREG = 0x80; // Global interrupt enable
while (1) {
unsigned char bits, channels, config;
......
......@@ -6,7 +6,7 @@ Model {
NumRootInports 0
NumRootOutports 0
ParameterArgumentNames ""
ComputedModelVersion "1.9"
ComputedModelVersion "1.11"
NumModelReferences 0
NumTestPointedSignals 0
}
......@@ -28,9 +28,9 @@ Model {
ModifiedByFormat "%<Auto>"
LastModifiedBy "andersb"
ModifiedDateFormat "%<Auto>"
LastModifiedDate "Fri Jul 27 17:25:58 2018"
RTWModifiedTimeStamp 454541743
ModelVersionFormat "1.%<AutoIncrement:9>"
LastModifiedDate "Thu Sep 13 13:46:49 2018"
RTWModifiedTimeStamp 458747140
ModelVersionFormat "1.%<AutoIncrement:11>"
ConfigurationManager "None"
SampleTimeColors off
SampleTimeAnnotations off
......@@ -690,7 +690,7 @@ Model {
}
System {
Name "process_test"
Location [774, 280, 1489, 926]
Location [764, 206, 1479, 852]
Open on
ModelBrowserVisibility off
ModelBrowserWidth 200
......@@ -704,12 +704,12 @@ Model {
ShowPageBoundaries off
ZoomFactor "100"
ReportName "simulink-default.rpt"
SIDHighWatermark "21"
SIDHighWatermark "25"
Block {
BlockType Constant
Name "Constant1"
SID "2"
Position [375, 255, 400, 275]
Position [375, 335, 400, 355]
Value "2.5"
}
Block {
......@@ -737,7 +737,16 @@ Model {
Name "Display"
SID "5"
Ports [1]
Position [510, 317, 605, 393]
Position [510, 397, 605, 473]
Decimation "1"
Lockdown off
}
Block {
BlockType Display
Name "Display1"
SID "24"
Ports [1]
Position [555, 127, 650, 203]
Decimation "1"
Lockdown off
}
......@@ -746,10 +755,10 @@ Model {
Name "Inputs"
SID "6"
Ports [1]
Position [590, 200, 620, 230]
Position [590, 280, 620, 310]
Floating off
Location [103, 106, 863, 567]
Open on
Location [73, 93, 843, 579]
Open off
NumInputPorts "1"
TickLabels "on"
List {
......@@ -772,6 +781,38 @@ Model {
YMax "10"
SampleTime "0"
}
Block {
BlockType Scope
Name "Inputs1"
SID "25"
Ports [1]
Position [590, 55, 620, 85]
Floating off
Location [217, 355, 977, 816]
Open off
NumInputPorts "1"
TickLabels "on"
List {
ListType AxesTitles
axes1 "%<SignalLabel>"
}
List {
ListType ScopeGraphics
FigureColor "[0.5 0.5 0.5]"
AxesColor "[0 0 0]"
AxesTickColor "[1 1 1]"
LineColors "[1 1 0;1 0 1;0 1 1;1 0 0;0 1 0;0 0 1]"
LineStyles "-|-|-|-|-|-"
LineWidths "[0.5 0.5 0.5 0.5 0.5 0.5]"
MarkerStyles "none|none|none|none|none|none"
}
ShowLegends off
TimeRange "100"
YMin "-50"
YMax "60"
SaveName "ScopeData1"
SampleTime "0"
}
Block {
BlockType Mux
Name "Mux1"
......@@ -785,7 +826,7 @@ Model {
Name "Mux2"
SID "9"
Ports [2, 1]
Position [530, 197, 565, 233]
Position [530, 277, 565, 313]
Inputs "2"
}
Block {
......@@ -803,8 +844,8 @@ Model {
Ports [1]
Position [290, 240, 320, 270]
Floating off
Location [39, 560, 495, 794]
Open on
Location [19, 544, 475, 778]
Open off
NumInputPorts "1"
TickLabels "on"
List {
......@@ -831,7 +872,7 @@ Model {
Name "S-Function"
SID "12"
Ports [1, 2]
Position [425, 156, 485, 189]
Position [425, 236, 485, 269]
FunctionName "analogin"
Parameters "h [30 31 32 33 34]"
EnableBusSupport off
......@@ -856,6 +897,16 @@ Model {
Parameters "h"
EnableBusSupport off
}
Block {
BlockType S-Function
Name "S-Function3"
SID "22"
Ports [1, 2]
Position [420, 141, 480, 174]
FunctionName "encoderin"
Parameters "h [30]"
EnableBusSupport off
}
Block {
BlockType S-Function
Name "S-Function4"
......@@ -871,7 +922,7 @@ Model {
Name "S-Function5"
SID "16"
Ports [1, 2]
Position [420, 246, 480, 279]
Position [420, 326, 480, 359]
FunctionName "digitalin"
Parameters "h [30 31]"
EnableBusSupport off
......@@ -903,7 +954,7 @@ Model {
Ports [0, 1]
Position [15, 80, 45, 110]
Amplitude "10"
Frequency "0.1"
Frequency "0.5"
SampleTime "h"
}
Block {
......@@ -945,13 +996,6 @@ Model {
DstBlock "Outputs"
DstPort 1
}
Line {
SrcBlock "S-Function"
SrcPort 2
Points [10, 0; 0, 25]
DstBlock "Mux2"
DstPort 1
}
Line {
SrcBlock "Mux2"
SrcPort 1
......@@ -965,13 +1009,6 @@ Model {
DstBlock "S-Function"
DstPort 1
}
Line {
SrcBlock "S-Function"
SrcPort 1
Points [0, -40; -275, 0]
DstBlock "S-Function1"
DstPort 1
}
Line {
SrcBlock "S-Function1"
SrcPort 1
......@@ -1012,5 +1049,40 @@ Model {
DstBlock "S-Function5"
DstPort 1
}
Line {
SrcBlock "S-Function"
SrcPort 1
Points [0, -50; -85, 0]
DstBlock "S-Function3"
DstPort 1
}
Line {
SrcBlock "S-Function3"
SrcPort 1
Points [0, -35; -270, 0]
DstBlock "S-Function1"
DstPort 1
}
Line {
SrcBlock "S-Function3"
SrcPort 2
Points [30, 0]
Branch {
DstBlock "Display1"
DstPort 1
}
Branch {
Points [0, -95]
DstBlock "Inputs1"
DstPort 1
}
}
Line {
SrcBlock "S-Function"
SrcPort 2
Points [10, 0; 0, 25]
DstBlock "Mux2"
DstPort 1
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment