diff --git a/ball_and_beam-2018/avr/ball_and_beam-2018.c b/ball_and_beam-2018/avr/ball_and_beam-2018.c index 7291b52ae9825c20c44c58b224b87fa69f09e7d7..c25dacf85792d65b337a497e77dd7442b1077944 100644 --- a/ball_and_beam-2018/avr/ball_and_beam-2018.c +++ b/ball_and_beam-2018/avr/ball_and_beam-2018.c @@ -13,11 +13,12 @@ * DO 0 -- Motor Brake * DO 1 -- Accelerometer Self-Test * - * AI 0 -- Ball Position + * AI 0 -- Ball Position (sigma-delta filtered) * AI 1 -- Motor Current * AI 2 -- Accelerometer X * AI 3 -- Accelerometer Y * AI 4 -- Accelerometer Z + * AI 5 -- Ball Position (raw) * * AO 0 -- Motor Speed * EI 0 -- Motor Position @@ -63,14 +64,15 @@ struct values { uint8_t accelerometerSelfTest; /* Inputs */ - int16_t ballPosition; - int16_t motorCurrent; - int16_t accelerometerX; - int16_t accelerometerY; - int16_t accelerometerZ; - int32_t motorEncoder; - int8_t motorThermalFlag; - int8_t ballOnBeam; + uint16_t ballPosition; + uint16_t motorCurrent; + uint16_t accelerometerX; + uint16_t accelerometerY; + uint16_t accelerometerZ; + uint16_t ballPositionRaw; + uint32_t motorEncoder; + uint8_t motorThermalFlag; + uint8_t ballOnBeam; }; volatile struct values global; @@ -82,7 +84,13 @@ SIGNAL(ADC_vect) switch (channel) { case 0: { channel = 1; - global.ballPosition = value; + global.ballPositionRaw = value; + uint16_t ballPosition = global.ballPosition; + if (global.ballPositionRaw > ballPosition) { + global.ballPosition++; + } else if (ballPosition > 0) { + global.ballPosition--; + } } break; case 1: { channel = 2; @@ -97,7 +105,7 @@ SIGNAL(ADC_vect) global.accelerometerY = value; } break; case 4: { - channel = 5; + channel = 0; global.accelerometerZ = value; } break; default: { @@ -105,7 +113,7 @@ SIGNAL(ADC_vect) } break; } ADMUX = 0x00 | channel; // Vref = external, right adjust - ADCSRA = 0xcf; // Enable ADC interrupts, Clock/128 + ADCSRA = 0xce; // Enable ADC interrupts, Clock/64 } typedef enum { cmd_clear_bit, cmd_set_bit, @@ -220,7 +228,7 @@ int main() UBRRL = 7; // USART: 115200 @ 14.7456MHz ADMUX = 0x00; // Vref = external, right adjust ADMUX = 0x0e; // Vref = external, right adjust, read 1.22V (Vbg) - ADCSRA = 0xcf; // Enable ADC interrupts, Clock/128 + ADCSRA = 0xce; // Enable ADC interrupts, Clock/64 global.motorEncoder = 0x80000000; @@ -243,7 +251,8 @@ int main() if (channels & 0x04) { serialio_putchannel(2, local.accelerometerX); } if (channels & 0x08) { serialio_putchannel(3, local.accelerometerY); } if (channels & 0x10) { serialio_putchannel(4, local.accelerometerZ); } - if (channels & 0x20) { serialio_putchannel(5, local.motorEncoder); } + if (channels & 0x20) { serialio_putchannel(5, local.ballPositionRaw); } + if (channels & 0x40) { serialio_putchannel(6, local.motorEncoder); } if (bits & 0x01) { // ballOnBeam serialio_putbit(0, PINB & 0x01); @@ -259,7 +268,7 @@ int main() CONF_DIGITAL_OUT(0, CONF_RESOLUTION(1)); // motorBrake CONF_DIGITAL_OUT(1, CONF_RESOLUTION(1)); // accelerometerSelfTest - CONF_ANALOG_IN(0, CONF_RESOLUTION(10)); // ballPosition + CONF_ANALOG_IN(0, CONF_RESOLUTION(10)); // ballPosition (filtered) CONF_ANALOG_IN(0, CONF_MIN(CONF_NEGATIVE_MILLIVOLT(10000))); CONF_ANALOG_IN(0, CONF_MAX(CONF_POSITIVE_MILLIVOLT(10000))); CONF_ANALOG_IN(1, CONF_RESOLUTION(10)); // motorCurrent @@ -274,7 +283,10 @@ int main() CONF_ANALOG_IN(4, CONF_RESOLUTION(10)); // accelerometerZ CONF_ANALOG_IN(4, CONF_MIN(CONF_NEGATIVE_MILLIVOLT(10000))); CONF_ANALOG_IN(4, CONF_MAX(CONF_POSITIVE_MILLIVOLT(10000))); - CONF_ENCODER_IN(5, CONF_RESOLUTION(32)); // motorPosition + CONF_ANALOG_IN(5, CONF_RESOLUTION(10)); // ballPosition (raw) + CONF_ANALOG_IN(5, CONF_MIN(CONF_NEGATIVE_MILLIVOLT(10000))); + CONF_ANALOG_IN(5, CONF_MAX(CONF_POSITIVE_MILLIVOLT(10000))); + CONF_ENCODER_IN(6, CONF_RESOLUTION(32)); // motorPosition CONF_ANALOG_OUT(0, CONF_RESOLUTION(11)); // motorSpeed CONF_ANALOG_OUT(0, CONF_MIN(CONF_NEGATIVE_MILLIVOLT(12000))); diff --git a/ball_and_beam-2018/matlab/process_test.mdl b/ball_and_beam-2018/matlab/process_test.mdl index e43c08e591f8cccfd5d9cb5ac6a38178293f34e7..53e5a3081716eb4ff331909b5ef59760a1aa695f 100644 --- a/ball_and_beam-2018/matlab/process_test.mdl +++ b/ball_and_beam-2018/matlab/process_test.mdl @@ -6,7 +6,7 @@ Model { NumRootInports 0 NumRootOutports 0 ParameterArgumentNames "" - ComputedModelVersion "1.11" + ComputedModelVersion "1.12" NumModelReferences 0 NumTestPointedSignals 0 } @@ -28,9 +28,9 @@ Model { ModifiedByFormat "%<Auto>" LastModifiedBy "andersb" ModifiedDateFormat "%<Auto>" - LastModifiedDate "Thu Sep 13 13:46:49 2018" - RTWModifiedTimeStamp 458747140 - ModelVersionFormat "1.%<AutoIncrement:11>" + LastModifiedDate "Tue Oct 16 17:05:14 2018" + RTWModifiedTimeStamp 461593970 + ModelVersionFormat "1.%<AutoIncrement:12>" ConfigurationManager "None" SampleTimeColors off SampleTimeAnnotations off @@ -690,7 +690,7 @@ Model { } System { Name "process_test" - Location [764, 206, 1479, 852] + Location [764, 206, 1675, 859] Open on ModelBrowserVisibility off ModelBrowserWidth 200 @@ -704,7 +704,7 @@ Model { ShowPageBoundaries off ZoomFactor "100" ReportName "simulink-default.rpt" - SIDHighWatermark "25" + SIDHighWatermark "26" Block { BlockType Constant Name "Constant1" @@ -750,6 +750,15 @@ Model { Decimation "1" Lockdown off } + Block { + BlockType Display + Name "Display2" + SID "26" + Ports [1] + Position [690, 430, 850, 600] + Decimation "1" + Lockdown off + } Block { BlockType Scope Name "Inputs" @@ -757,7 +766,7 @@ Model { Ports [1] Position [590, 280, 620, 310] Floating off - Location [73, 93, 843, 579] + Location [360, 169, 1130, 655] Open off NumInputPorts "1" TickLabels "on" @@ -776,7 +785,7 @@ Model { MarkerStyles "none|none|none|none|none|none" } ShowLegends off - TimeRange "100" + TimeRange "10" YMin "-10" YMax "10" SampleTime "0" @@ -788,7 +797,7 @@ Model { Ports [1] Position [590, 55, 620, 85] Floating off - Location [217, 355, 977, 816] + Location [207, 347, 967, 808] Open off NumInputPorts "1" TickLabels "on" @@ -874,7 +883,7 @@ Model { Ports [1, 2] Position [425, 236, 485, 269] FunctionName "analogin" - Parameters "h [30 31 32 33 34]" + Parameters "h [30 31 32 33 34 35]" EnableBusSupport off } Block { @@ -999,8 +1008,16 @@ Model { Line { SrcBlock "Mux2" SrcPort 1 - DstBlock "Inputs" - DstPort 1 + Points [5, 0] + Branch { + DstBlock "Inputs" + DstPort 1 + } + Branch { + Points [0, 55; 45, 0; 0, 165] + DstBlock "Display2" + DstPort 1 + } } Line { SrcBlock "S-Function5"