Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * HW06-RobotRC.xc
- *
- * Created on: Oct 27, 2016
- * Author: harrison
- */
- #include <xs1.h>
- #include <print.h>
- #include <string.h>
- #include <stdio.h>
- //defines
- #define BAUD_RATE 9600
- #define TICKS_PER_US (XS1_TIMER_HZ/1000000)
- #define TICKS_PER_SEC (XS1_TIMER_HZ)
- #define TICKS_PER_MS (XS1_TIMER_HZ/1000)
- #define PWM_FRAME_TICKS (TICKS_PER_MS)
- #define MESSAGE_SIZE 128
- #define BIN1_ON 0b0010 //BIN1 -> D17 -> P4D1
- #define BIN2_ON 0b1000 //BIN2 -> D19 -> P4D3
- #define AIN1_ON 0b0100 //AIN1 -> D18 -> P4D2 //CCW
- #define AIN2_ON 0b0001 //AIN2 -> D16 -> P4D0 //CW
- //Ports
- in port iButton = XS1_PORT_1C;
- out port oButtonDriver = XS1_PORT_1B;
- out port oLED = XS1_PORT_1A;
- out port oSTB = XS1_PORT_1O;
- out port oWiFiRX = XS1_PORT_1F;
- in port iWiFiTX = XS1_PORT_1H;
- out port oMotorPWMA = XS1_PORT_1P; //PWMA -> D39 -> XS1_PORT_1P
- out port oMotorPWMB = XS1_PORT_1I; //PWMB -> D24 -> P1I
- out port oMotorControl = XS1_PORT_4D;
- //out port oLED = XS1_PORT_1A;
- //out port oSTB = XS1_PORT_1O; //stanby, you need to send a 1 or else IT WONT WORK
- in port iEncoders = XS1_PORT_4C;
- //out port oLEDOne = XS1_PORT_1A;
- out port oLEDTwo = XS1_PORT_1D;
- //Prototypes
- void multi_motor_task(out port oLeftPWM, out port oRightPWM, out port oMotorControl, chanend in_motor_cmd_chan);
- void uart_transmit_byte(out port oPort, char value, unsigned int baudrate);
- char uart_receive_byte(in port iPort, unsigned int baudrate);
- void toggle_port(out port oLED, unsigned int hz);
- void uart_transmit_bytes(out port oPort, const char values[], unsigned int baudrate);
- void uart_to_console_task(chanend trigger_chan, chanend in_motor_cmd_chan);
- void line(const char buffer[]);
- void send_hello_world_program();
- void output_task(chanend trigger_chan);
- void send_wifi_setup();
- void full_speed(chanend in_motor_cmd_chan);
- void half_speed(chanend in_motor_cmd_chan);
- void full_reverse(chanend in_motor_cmd_chan);
- void half_reverse(chanend in_motor_cmd_chan);
- void leftTurn(chanend in_motor_cmd_chan);
- void rightTurn(chanend in_motor_cmd_chan);
- void stop(chanend in_motor_cmd_chan);
- void computeDifference();
- typedef struct {
- char data[MESSAGE_SIZE];
- } message_t;
- typedef struct {
- int left_duty_cycle;
- int right_duty_cycle;
- } motor_cmd_t;
- int main_single();
- int main_array();
- int main()
- {
- chan trigger;
- chan motor_cmd_chan;
- oWiFiRX <: 1;
- par
- {
- uart_to_console_task(trigger,motor_cmd_chan);
- multi_motor_task(oMotorPWMA, oMotorPWMB, oMotorControl, motor_cmd_chan);
- output_task(trigger);
- }
- }
- void full_speed(chanend in_motor_cmd_chan)
- {
- motor_cmd_t full_speed;
- full_speed.left_duty_cycle = 100;
- full_speed.right_duty_cycle = 100;
- in_motor_cmd_chan <: full_speed;
- }
- void half_speed(chanend in_motor_cmd_chan)
- {
- motor_cmd_t half_speed;
- half_speed.left_duty_cycle = 50;
- half_speed.right_duty_cycle = 50;
- in_motor_cmd_chan <: half_speed;
- }
- void full_reverse(chanend in_motor_cmd_chan)
- {
- motor_cmd_t full_reverse;
- full_reverse.left_duty_cycle = -100;
- full_reverse.right_duty_cycle = -100;
- in_motor_cmd_chan <: full_reverse;
- }
- void half_reverse(chanend in_motor_cmd_chan)
- {
- motor_cmd_t half_reverse;
- half_reverse.left_duty_cycle = -50;
- half_reverse.right_duty_cycle = -50;
- in_motor_cmd_chan <: half_reverse;
- }
- void stop(chanend in_motor_cmd_chan)
- {
- motor_cmd_t stop;
- stop.left_duty_cycle = 0;
- stop.right_duty_cycle = 0;
- in_motor_cmd_chan <: stop;
- }
- void rightTurn(chanend in_motor_cmd_chan)
- {
- motor_cmd_t rightTurn;
- rightTurn.left_duty_cycle = 50;
- rightTurn.right_duty_cycle = 75;
- in_motor_cmd_chan <: rightTurn;
- }
- void leftTurn(chanend in_motor_cmd_chan)
- {
- motor_cmd_t leftTurn;
- leftTurn.left_duty_cycle = 75;
- leftTurn.right_duty_cycle = 50;
- in_motor_cmd_chan <: leftTurn;
- }
- void output_task(chanend trigger_chan)
- {
- while(1)
- {
- select
- {
- case trigger_chan :> message_t value :
- if(strcmp(value.data, "send_wifi_setup") == 0)
- {
- send_wifi_setup();
- }
- else
- {
- line(value.data);
- }
- break;
- }
- }
- }
- void send_wifi_setup()
- {
- line("wifi.setmode(wifi.SOFTAP)");
- line("cfg={}");
- line("cfg.ssid=\"MydadisObama\"");
- line("cfg.pwd=\"trump\"");
- line("cfg.ip=\"192.168.0.1\"");
- line("cfg.netmask=\"255.255.255.0\"");
- line("cfg.gateway=\"192.168.0.1\"");
- line("port = 9876");
- line("wifi.ap.setip(cfg)");
- line("wifi.ap.config(cfg)");
- line("print(\"ESP8266 TCP to Serial Bridge v1.0 by RoboRemo\")");
- line("tmr.alarm(0,200,0,function() -- run after a delay ");
- //line("uart.setup(0, 9600, 8, 0, 1, 1)");
- line("srv=net.createServer(net.TCP, 28800)");
- line("srv:listen(port,function(conn)");
- line("uart.on(\"data\", 0, function(data)");
- line("conn:send(data) end, 0)");
- line("conn:on(\"receive\",function(conn,payload)");
- line("uart.write(0, payload)");
- line("end)");
- line("conn:on(\"disconnection\",function(c)");
- line(" uart.on(\"data\")");
- line("end)");
- line("end)");
- line("end)");
- //line("");
- //line("");
- //line("");
- }
- void send_hello_world_program()
- {
- // printstr("running...");
- line("gpio.mode(3, gpio.OUTPUT)");
- line("while 1 do");
- line("gpio.write(3, gpio.HIGH)");
- line("tmr.delay(1000000)");
- line("gpio.write(3, gpio.LOW)");
- line("tmr.delay(1000000)");
- line("end");
- }
- void line(const char buffer[])
- {
- timer tmr;
- unsigned int time;
- tmr :> time;
- time += (TICKS_PER_SEC/8);
- tmr when timerafter(time) :> void;
- uart_transmit_bytes(oWiFiRX, buffer, BAUD_RATE);
- uart_transmit_bytes(oWiFiRX, "\r\n", BAUD_RATE);
- //printstr("hey");
- }
- void uart_to_console_task(chanend trigger_chan, chanend in_motor_cmd_chan)
- {
- char currChar;
- char buffer[64];
- int currElement = 0;
- message_t m;
- strncpy(m.data, "send_wifi_setup", 20);
- while(1)
- {
- currChar = uart_receive_byte(iWiFiTX, BAUD_RATE);
- buffer[currElement] = currChar;
- currElement++;
- if(currElement == 63 || currChar == '\r' || currChar == '\n')
- {
- //printstrln(buffer);
- buffer[currElement] = '\0';
- if(strcmp(buffer, "lua: cannot open init.lua\r") == 0)
- {
- //printstrln(buffer);
- trigger_chan <: m;
- }
- else if(strcmp(buffer, "F\r") == 0)
- {
- full_speed(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, "f\r") == 0)
- {
- half_speed(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, "R\r") == 0)
- {
- full_reverse(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, "r\r") == 0)
- {
- half_reverse(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, "<\r") == 0)
- {
- leftTurn(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, ">\r") == 0)
- {
- rightTurn(in_motor_cmd_chan);
- }
- else if(strcmp(buffer, "x\r") == 0)
- {
- stop(in_motor_cmd_chan);
- }
- // else if(strcmp(buffer, "?\r") == 0)
- // {
- // computeDifference();
- // }
- currElement = 0;
- }
- }
- }
- void toggle_port(out port oLED, unsigned int hz)
- {
- timer tmr;
- unsigned int t;
- unsigned pattern = 0b1;
- unsigned int delay = XS1_TIMER_HZ / hz;
- oLED <: pattern;
- tmr :> t;
- while (1)
- {
- oLED <: pattern;
- t += delay;
- tmr when timerafter(t) :> void;
- pattern = ~pattern;
- }
- return 0;
- }
- void uart_transmit_bytes(out port oPort, const char values[], unsigned int baudrate)
- {
- //for(int i = 0; i != '\0'; i++)
- //{
- char val;
- int i = 0;
- while(values[i] != '\0')
- {
- uart_transmit_byte(oPort, values[i], baudrate);
- i++;
- //printstr("hey");
- }
- // }
- }
- //Function Defnitions
- void uart_transmit_byte(out port oPort, char value, unsigned int baudrate)
- {
- timer tmr;
- unsigned int time;
- const unsigned int bitTime = XS1_TIMER_HZ / baudrate;
- unsigned int val = value;
- tmr :> time;
- //output start bit
- oPort <: 0;
- time += bitTime;
- tmr when timerafter(time) :> void;
- //output byte
- for(int i = 0; i < 8; i++)
- {
- oPort <: (val & 0x1);
- val >>= 1;
- //might need to do shift in seperate statement
- time += bitTime;
- tmr when timerafter(time) :> void;
- }
- //output stop bit
- oPort <: 1;
- time += bitTime;
- tmr when timerafter(time) :> void;
- }
- char uart_receive_byte(in port iPort, unsigned int baudrate)
- {
- timer tmr;
- unsigned int time, value;
- const unsigned int bitTime = XS1_TIMER_HZ / baudrate;
- iPort when pinseq(1) :> void;
- iPort when pinseq(0) :> void;
- tmr :> time;
- time += bitTime/2;
- //input data bits
- value = 0;
- for(int i = 0; i < 8; i++)
- {
- time += bitTime;
- tmr when timerafter(time) :> void;
- iPort :> >> value;
- }
- // input stop bit //
- time += bitTime;
- tmr when timerafter(time) :> void;
- return (char) (value >> 24);
- }
- void multi_motor_task(out port oLeftPWM, out port oRightPWM, out port oMotorControl, chanend in_motor_cmd_chan)
- {
- int lastLeft = 0;
- int lastRight = 0;
- int timeout = 0;
- unsigned int leftTicks;
- unsigned int rightTicks;
- unsigned int delayLeft;
- unsigned int delayRight;
- unsigned int delayPWM;
- unsigned int mask;
- timer timerLeft;
- timer timerRight;
- timer timerPWM;
- while(1){
- leftTicks = lastLeft * (PWM_FRAME_TICKS / 100);
- rightTicks = lastRight * (PWM_FRAME_TICKS / 100);
- oMotorControl <: mask;
- timeout = 0;
- oLeftPWM <: 1;
- oRightPWM <: 1;
- timerLeft :> delayLeft;
- timerRight :> delayRight;
- timerPWM :> delayPWM;
- delayLeft += leftTicks;
- delayRight += rightTicks;
- delayPWM += PWM_FRAME_TICKS;
- while(timeout == 0){
- select{
- case timerLeft when timerafter(delayLeft) :> void:
- oLeftPWM <: 0;
- delayLeft += XS1_TIMER_HZ;
- break;
- case timerRight when timerafter(delayRight) :> void:
- oRightPWM <: 0;
- delayRight += XS1_TIMER_HZ;
- break;
- case timerPWM when timerafter(delayPWM) :> void:
- timeout = 1;
- break;
- case in_motor_cmd_chan :> motor_cmd_t currentDuty:
- lastLeft = currentDuty.left_duty_cycle;
- lastRight = currentDuty.right_duty_cycle;
- if(currentDuty.left_duty_cycle < 0){
- lastLeft = currentDuty.left_duty_cycle * -1;
- mask = AIN1_ON;
- }else{
- mask = AIN2_ON;
- }
- if(currentDuty.right_duty_cycle < 0){
- lastRight = currentDuty.right_duty_cycle * -1;
- mask = (BIN2_ON | mask);
- }else{
- mask = (BIN1_ON | mask);
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement