System Management code
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
Revision 2:baeb80c778f7, committed 2014-10-05
- Comitter:
- martydd3
- Date:
- Sun Oct 05 17:35:20 2014 +0000
- Parent:
- 1:e02eb179aed3
- Child:
- 3:1345f882d8f3
- Commit message:
- Add preliminary code to control fans and pumps using PWM; To do: Decide CANMessage format for controlling fans and pumps; Debug and test code; Add to toggle DC-DC converter via CAN;
Changed in this revision
--- a/CANBuffer.lib Sat Oct 04 16:19:45 2014 +0000 +++ b/CANBuffer.lib Sun Oct 05 17:35:20 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Penn-Electric-Racing/code/CANBuffer/#6fc28f58f16e +http://developer.mbed.org/teams/Penn-Electric-Racing/code/CANBuffer/#36e62c1f7039
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CAN_Filter_LUT.h Sun Oct 05 17:35:20 2014 +0000 @@ -0,0 +1,65 @@ +/* +Code by Parth Patel, Penn Electric Racing 2014, 9/23/2014 + +This library provides an easy to use, buffered, hardware-filtered CAN interface for +high performance CAN applications. Provides automatic reception of messages via CAN RX interrupt +into a rx ring buffer. Provides automatic transmission of messages via CAN TX interrupt. + +@File CAN_Filter_LUT.h: Contains the formatted lookup tables to program the onboard CAN acceptance filters + +*/ +#ifndef _FILE_CAN_FILTER_LUT_H +#define _FILE_CAN_FILTER_LUT_H + +#define STDMASK 0x7FF +#define EXTMASK 0x1FFFFFFF + +// These arrays defines the CAN Controller Acceptance Filter Lookup Table. +// Follow notes below or else the chip's behaviour will be undefined +// MAX SIZE PERMITTED = 512 32bit ints total across all tables +// Note that AF_LUT_SEI is 16bit, divide #entries by 2 for this one +// Note that AF_LUT_EIR is 64bit, multipy #entries by 2 for this one + +const uint16_t AF_LUT_SEI[] = { +// !! ID's MUST BE IN ASCENDING ORDER (starting at 0x00) !! + +// STANDARD EXPLICIT IDs - CAN CONTROLLER 1 +//( 0xID & STDMASK), + +// STANDARD EXPLICIT IDs - CAN CONTROLLER 2 +//( 0xID & STDMASK) | 1<<13, +}; + +const uint32_t AF_LUT_SIR[] = { +// !! ID's MUST BE IN ASCENDING ORDER (starting at 0x00), NO OVERLAPPING RANGES !! + +// STANDARD ID RANGES - CAN CONTROLLER 1 +//( 0xLOWERBOUND & STDMASK) << 16 | ( 0xUPPERBOUND & STDMASK), lower/upperbounds are inclusive + +// STANDARD ID RANGES - CAN CONTROLLER 2 +//( 0xLOWERBOUND & STDMASK | 1<<13) << 16 | ( 0xUPPERBOUND & STDMASK | 1<<13), lower/upperbounds are inclusive + ( 0x400 & STDMASK | 1<<13) << 16 | ( 0x4FF & STDMASK | 1<<13), // Index1 +}; + +const uint32_t AF_LUT_EEI[] = { +// !! ID's MUST BE IN ASCENDING ORDER (starting at 0x00) !! + +// EXTENDED EXPLICIT IDs - CAN CONTROLLER 1 +//( 0xID & EXTMASK), + +// EXTENDED EXPLICIT IDs - CAN CONTROLLER 2 +//( 0xID & EXTMASK) | 1<<29, +}; + +const uint64_t AF_LUT_EIR[] = { +// !! ID's MUST BE IN ASCENDING ORDER (starting at 0x00), NO OVERLAPPING RANGES !! + +// EXTENDED ID RANGES - CAN CONTROLLER 1 +//( 0xLOWERBOUND & EXTMASK) << 32 | ( 0xUPPERBOUND & EXTMASK), lower/upperbounds are inclusive + +// EXTENDED ID RANGES - CAN CONTROLLER 2 +//( 0xLOWERBOUND & EXTMASK | 1<<29) << 32 | ( 0xUPPERBOUND & EXTMASK | 1<<29), lower/upperbounds are inclusive + +}; + +#endif \ No newline at end of file
--- a/LPCDigitalOut.lib Sat Oct 04 16:19:45 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/teams/Penn-Electric-Racing/code/LPCDigitalOut/#88059ca2c2d9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SysManagement_kiran_full.lib Sun Oct 05 17:35:20 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/martydd3/code/SystemManagement/#e02eb179aed3
--- a/SysMngmt.cpp Sat Oct 04 16:19:45 2014 +0000 +++ b/SysMngmt.cpp Sun Oct 05 17:35:20 2014 +0000 @@ -12,10 +12,8 @@ #include"XBee_Lib.h" #include"CANBuffer.h" - -RTCStore store; -CANBuffer RecieveBuffer(CAN1, MEDIUM); -XBee250x XbeeTx; +#include"mbed.h" +#include"rtos.h" //Possible problems in IMD coz change of counter //Possible problems in BatteryStatus coz change in library @@ -32,25 +30,21 @@ CAN interrupt Rx Interrupt Recieve CAN message into a buffer. Return Buffer values(as long as !empty) -> SD Card, Xbee -> remove element -*/ -/* extern "C" void CAN_IRQHandler(void) { CANMessage Rxmsg; CAN_SysM.read(Rxmsg); RecieveBuffer.add(Rxmsg); } -*/ -/* http://developer.mbed.org/users/AjK/notebook/getting-closer-to-the-hardware/ extern "C" means this is linked assuming it's C code C++ linker appearently adds extra crap with the function arguments keeping functions of this kind from linking properly Interrupt handler, This is probably linked to the Timer 2 interrupt request somewhere by the libraries -*/ + extern "C" void TIMER2_IRQHandler(void) { if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt @@ -79,10 +73,9 @@ } } -/* Appears to read a whole bunch of DigitalOut pins in void PollSwitch(), store the value in uint16_t Rxpoll, and write the result to the CAN bus -*/ + void Poll() { uint16_t Rxpoll; @@ -200,15 +193,14 @@ CAN_SysM.write(Txmsg_DCA_msec); } -/* Activates a whole crapload of functions and pins on the chip -*/ + void Init() { - /* + Timers to call various functions at different intervals These things behave weirdly when wait(ms) is involved. Probably have to rewrite - */ + //ReadIMD.attach(&IMD,0.1); //PollSDSwitch.attach(&Poll,0.1); @@ -216,7 +208,7 @@ //ReadTemperature.attach(&Temp,0.1); //ReadBatteryState.attach(&Battery,0.1); - /* + Initialize Timer2 for Battery State LPC_SC 0x400F C000 (System Control) @@ -235,14 +227,13 @@ ->MCR 0x4009 0014 (Match Control Register) What to do when Match Register matches the Timer Counter |= (1<<0); 0 Bit (Interrupt on MR0, interrupt generated when MR0 matches the value in TC) - */ + LPC_SC->PCONP |= (1<<22); //PoewerOn Timer/Counter2 LPC_SC->PCLKSEL1 |= ((1<<12) | (1<<13)); //Prescale Timer2 CCLK/8 LPC_TIM2->TCR |= (1<<0); //Enable Timer2 LPC_TIM2->MR0 = 11999; // 1msec LPC_TIM2->MCR |= (1<<0); - - /* + Nested Vectored Interrupt Controller (NVIC) NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) @@ -256,7 +247,7 @@ Enable external interrupt (in this case, the TIMER2_IRQHandler(void) function above gets called every time Timer2 generates an interrupt signal) - */ + NVIC_SetPriority(TIMER0_IRQn,200); //IMD Capture Interrupt NVIC_SetPriority(TIMER1_IRQn,200); //IMD 1msec sampling Interrupt NVIC_SetPriority(TIMER2_IRQn,1); //Battery 1msec sampling Interrupt @@ -266,34 +257,112 @@ NVIC_EnableIRQ(TIMER2_IRQn); //Enable TIMER2 IRQ CAN_SysM.mode(CAN::GlobalTest); + + //NVIC_EnableIRQ(CAN_IRQn); //NVIC_EnableIRQ(CANActivity_IRQn); } -/* + Main Loop: Currently reads CANMessages from Can interface (Pins: rd = p30, td = p29) Send CANMessage data through XBee radio transmitters +*/ + + +CANBuffer rxBuffer(CAN1, MEDIUM); +XBee250x XbeeTx; + +int sysCANId = 0; // change this later + +char fanId = 0; // first byte of CANData, last byte states the duty cycle, second-last states which fan to control + +Thread *fan_Threads[4] = {NULL, NULL, NULL, NULL}; // Threads currently running on the 4 output pins + +/* + Pins for Fans and Pumps + PUMP_PWM = p2.0 + FAN1_PWM = p2.1 + FAN2_PWM = p2.2 + FAN3_PWM = p2.3 +*/ + +PwmOut pump_pwm(P2_0); +PwmOut fan1_pwm(P2_1); +PwmOut fan2_pwm(P2_2); +PwmOut fan3_pwm(P2_3); + +// duty goes from 0 to 100 +void rampFans(void const *arg){ -*/ + static int fan1_duty = 0; + static int fan2_duty = 0; + static int fan3_duty = 0; + static int pump_duty = 0; + + int *data = (int *)arg; + + int duty = data[7]; + if(duty < 0 || duty > 100) + return; + + int pin_ID = data[6]; + int *cur_duty; + PwmOut *out_pin; + + switch(pin_ID){ + case 0: cur_duty = &pump_duty; + out_pin = &pump_pwm; + break; + case 1: cur_duty = &fan1_duty; + out_pin = &fan1_pwm; + break; + case 2: cur_duty = &fan2_duty; + out_pin = &fan2_pwm; + break; + case 3: cur_duty = &fan3_duty; + out_pin = &fan3_pwm; + break; + default: + return; + } + + while(*cur_duty != duty){ + + if(duty > *cur_duty){ + *cur_duty += 10; + + if(*cur_duty > duty) + *cur_duty = duty; + } else if(duty < *cur_duty){ + *cur_duty -= 10; + + if(*cur_duty < duty) + *cur_duty = duty; + } + + out_pin->write((*cur_duty)*0.01); + + Thread::wait(10); + } +} + int main() { - CANMessage Rxmsg; - - Init(); - + CANMessage rx_msg; + while(1) { - if(CAN_SysM.read(Rxmsg)) - RecieveBuffer.txWrite(Rxmsg); - while(RecieveBuffer.rxRead(Rxmsg)) - { - XbeeTx.send(Rxmsg); - } - - //Poll(); - //Temp(); - //Battery(); + if(rxBuffer.rxRead(rx_msg) && rx_msg.id == sysCANId){ + if(rx_msg.data[0] == fanId){ + + int pin_id = (int)rx_msg.data[6]; + + if(pin_id < 4 && pin_id >= 0 && fan_Threads[pin_id] != NULL){ + fan_Threads[pin_id]->terminate(); + free(fan_Threads[pin_id]); + } + + fan_Threads[pin_id] = new Thread(rampFans, rx_msg.data); + } + } } - //Temp(); - //IMD(); - //Battery(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Sun Oct 05 17:35:20 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_error.h Sun Oct 05 17:35:20 2014 +0000 @@ -0,0 +1,70 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_ERROR_H +#define MBED_ERROR_H + +/** To generate a fatal compile-time error, you can use the pre-processor #error directive. + * + * @code + * #error "That shouldn't have happened!" + * @endcode + * + * If the compiler evaluates this line, it will report the error and stop the compile. + * + * For example, you could use this to check some user-defined compile-time variables: + * + * @code + * #define NUM_PORTS 7 + * #if (NUM_PORTS > 4) + * #error "NUM_PORTS must be less than 4" + * #endif + * @endcode + * + * Reporting Run-Time Errors: + * To generate a fatal run-time error, you can use the mbed error() function. + * + * @code + * error("That shouldn't have happened!"); + * @endcode + * + * If the mbed running the program executes this function, it will print the + * message via the USB serial port, and then die with the blue lights of death! + * + * The message can use printf-style formatting, so you can report variables in the + * message too. For example, you could use this to check a run-time condition: + * + * @code + * if(x >= 5) { + * error("expected x to be less than 5, but got %d", x); + * } + * #endcode + */ + +// for some reason, the rtos library can't access mbed_error.h from the mbed library +// This is a pretty recent bug as of Oct 5, 2014 +// If they fix the bug, remove this file + +#ifdef __cplusplus +extern "C" { +#endif + +void error(const char* format, ...); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file